The code below returns the 10 most recent entries to a MySQL database. 开发者_C百科That's what I want, but I also want it to display the results in an HTML table. Right now, only the most recent result is in a table, a one-row table. The rest of the results are displayed in a jumble of text. How could I get all of the results in a 10-row table?
Thanks in advance,
John
$sqlStr = "SELECT loginid, title, url, displayurl
FROM submission ORDER BY datesubmitted DESC LIMIT 10";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename2"><a href="http://www.'.$row["url"].'">'.$row["title"].'</a></td>';
echo '<td class="sitename2"><a href="http://www.'.$row["url"].'">'.$row["loginid"].'</a></td>';
echo '</tr>';
echo "</table>";
Move the }
for the while
to be above the echo "</table>";
line.
That way the loop repeats once for every row and the echo "</table>";
line happens after all rows. Without that close bracket, the table gets closed after the first row, and then another row appears but outside a table, and then another close-table tag, etc.
(Marking this as community wiki because mjv really should have just made his comment an answer. ;))
精彩评论