iRoot121 Posted October 22, 2013 Share Posted October 22, 2013 (edited) Hai guys, I'm busy with a category display script, but do I replace the text for an URL? Example: I've this code at the moment: <?php $query="SELECT `cat` FROM `video` WHERE `id`='".$_GET['video']."'"; $sql=mysql_query($query) or die(mysql_error()); while($line=mysql_fetch_array($sql)) { echo ''.$line['cat'].''; } ?> The output for example is: Category1,Category2,Category3. So, what do I need to let the output be like this: <a href="category.php?cat=Category1">Category1</a>, <a href="category.php?cat=Category2">Category2</a>, <a href="category.php?cat=Category3">Category3</a> Thanks for any help! Edited October 22, 2013 by iRoot121 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 echo '<a href="category.php?id='.$line['cat'].'">'.$line['cat'].'</a>'; Quote Link to comment Share on other sites More sharing options...
iRoot121 Posted October 22, 2013 Author Share Posted October 22, 2013 Well, that doesn't work . Because the there's just one output in the while, and that is 'Category1,Category2,Category3'. So, is there any way to explode every word after the ','? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 22, 2013 Share Posted October 22, 2013 How about explode(): http://php.net/manual/en/function.explode.php Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 22, 2013 Solution Share Posted October 22, 2013 Oh didn't read your post properly. I though $row['cat'] echos a category each time. $categoroes = explode(',', $line['cat']); foreach($categories as $k => $category) { $categories[$k] = '<a href="category.php?id='.$category.'">'.$category.'</a>'; } echo implode(', ', $categories); Quote Link to comment Share on other sites More sharing options...
iRoot121 Posted October 22, 2013 Author Share Posted October 22, 2013 Thanks cyberRobot and Ch0cu3r! It's working fine now! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.