JP128 Posted December 11, 2006 Share Posted December 11, 2006 Okay, say I have a table called links. And inside links are two different columns called linkroute, and linkname. I want all of the rows to be put out into the format:<a href="$LINKROUTE1">$LINKNAME1</a><a href="$LINKROUTE2">$LINKNAME2</a>and so on so forth...If someone could tell me how to do this, that would be great. Thanks for your time... Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/ Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 the [/url] should be the </ a> Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/#findComment-138797 Share on other sites More sharing options...
trq Posted December 11, 2006 Share Posted December 11, 2006 [code]<?php // connect to db. $sql = "SELECT linkroute,linkname FROM links"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"{$row['linkroute']}\">{$row['linkname']}</a>"; } } }?>[/code] Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/#findComment-138799 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 hmm, that is not giving me an error, but, it wont output anything.. Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/#findComment-138805 Share on other sites More sharing options...
sanfly Posted December 11, 2006 Share Posted December 11, 2006 you could add some error checking into the code that thorpe gave you[code=php:0]// connect to db. $sql = "SELECT linkroute,linkname FROM links"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { echo "<a href=\"{$row['linkroute']}\">{$row['linkname']}</a>"; } } else{ echo "no links found"; } } else{ echo "problem with query"; }[/code]I would do it slightly differently , but thats just a difference of coding styles, nothing major[code=php:0] // put your db connection info here $r = mysql_query("SELECT * FROM links") or die(mysql_error()); if(mysql_num_rows($r) > 0){ while($row = mysql_fetch_array($r){ $linkroute = $row['linkroute']; $linkname = $row['linkname']; ?> <a href="<?=$linkroute?>"><?=$linkname?></a> <? } } else{ echo "No Links Found"; }[/code]Assuming short tags enabled, if not replace <? with <?php Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/#findComment-138817 Share on other sites More sharing options...
JP128 Posted December 11, 2006 Author Share Posted December 11, 2006 Ok, thanks I got it... I also had a problem when calling the function, but your 2nd e.g gave the short tags error.. but i fixed it ... thanks Link to comment https://forums.phpfreaks.com/topic/30185-solved-echoing-two-colums-from-one-row/#findComment-138832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.