wwfc_barmy_army Posted October 24, 2006 Share Posted October 24, 2006 Hello.Currently i have a a field in a database which contains a link such as 'http://www.google.com', i then display this using:[code=php:0] <td height="23" colspan="3"><strong>Link:</strong> <?php if ($qry[link] == ""){ print "Link not available yet"; } else { print "<a href=$qry[link] target=_blank>$qry[link]</a>"; } ?></td>[/code]So basically just using $qry[link] to display it.But i am looking to change it slightly so many links can be added seperated by commas so eg:"http://www.google.com,http://www.msn.com,http://www.yahoo.com", although i am unsure how i can display these seperately, so it says: Link 1: http://www.google.comLink 2: http://www.msn.comLink 3: http://www.yahoo.comEtc.... depending on how many there areI'm 95% sure i will never need more than 4 links.Is it possible to do this, and how?Any links/code/tutorials will be welcomed :)Thanks. Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/ Share on other sites More sharing options...
Orio Posted October 24, 2006 Share Posted October 24, 2006 You should use the function [url=http://www.php.net/manual/en/function.explode.php]explode()[/url] :)Orio. Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/#findComment-113832 Share on other sites More sharing options...
wwfc_barmy_army Posted October 24, 2006 Author Share Posted October 24, 2006 Thanks.Hmmm, i'm close now, i have this:[code=php:0] <?php if ($qry[link] == ""){ print "Link not available yet"; } else { $pieces = explode(",", $qry[link]); echo "Link 1: <a href=$pieces[0]>$pieces[0]</a> <br>"; // piece1 echo "Link 2: <a href=$pieces[1]>$pieces[1]</a> <br>"; // piece2 echo "Link 3: <a href=$pieces[2]>$pieces[2]</a> <br>"; // piece3 echo "Link 4: <a href=$pieces[3]>$pieces[3]</a> <br>"; // piece4 //print "<a href=$qry[link] target=_blank>$qry[link]</a>"; } ?>[/code]Although it will say 'link 3' and 'link 4' even if there isn't one. How can i get around this?Thanks. Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/#findComment-113879 Share on other sites More sharing options...
wwfc_barmy_army Posted October 25, 2006 Author Share Posted October 25, 2006 bump :) Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/#findComment-114167 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 Use a foreach loop...[code]<?phpif ($qry['link'] == ""){ // If the link is empty, output message echo "Link not available yet";}else { $links = explode(",", $qry['link']); // Put the links into an array split on a comma $count = 1; foreach ($links as $link){ echo "Link $count: <a href=$link>$link</a><br>"; // Output the link including the link number $count++; }} ?>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/#findComment-114211 Share on other sites More sharing options...
wwfc_barmy_army Posted October 25, 2006 Author Share Posted October 25, 2006 Spot on. Thanks! :) Link to comment https://forums.phpfreaks.com/topic/24976-how-can-i-display-this-field/#findComment-114283 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.