mysql_fetch_array with while loop to echo list of URLs
I have a list of urls that I need to echo from my "menu" table. Here is what I have so far, but I can't seem to figure out the rest of it. The urls below are obviously there to show the format of the original HTML.
<?php
$results = mysql_query("SELECT * FROM menu WHERE level='$level'"开发者_JAVA百科);
$row = mysql_fetch_array($results);
?>
<li><a href="http://website.com/webservices/admin/achievments.php" target="ifrm">Achievments</a></li>
<li><a href="http://website.com/webservices/admin/avatar.php" target="ifrm">Avatar</a></li>
.... more urls ....
You need to do it in a loop, usually a while loop:
<?php
$results = mysql_query("SELECT * FROM menu WHERE level='$level'");
while ($row = mysql_fetch_array($results)) {
echo'<li><a href="'.$row['uri'].'" target="ifrm">'.$row['name'].'</a></li>';
}
?>
I've improvised on your column names (uri
and name
), they will probably be something different.
Close. Personally I would use put it into a while loop to read all the values. then echo them one by one.
while($row = mysql_fetch_array($results)){
echo "<a href = \"". $row['fieldtouse'] . "\" />". $row['textforlink'] . "<a/>";
}
That is the idea. Loop through results. echo results.
I believe this is a simple case of iterating over the result list. And as you already mentioned you should not use a single mysql_fetch_array
, but in a loop like this:
while ($row = mysql_fetch_array($results)) {
print "<li><a href='.../$row[0]' target='evil'>$row[1]</a></li>";
}
Now $row[0] and $row[1] will have to be adapted. Prefer mysql_fetch_assoc
to get named result columns, and then apply e.g. $row[url]
and $row[title]
instead of the numeric keys.
精彩评论