Numbered database query list
Moderator: General Moderators
Numbered database query list
Lets say I'm querying my database, and I get 3 results, and post it on my website, like normal. How would I add a numbered list?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
you could be like
Code: Select all
<?
//MAKE $results AN ARRAY OF THE RESULTS..
$r_n = count($results);
echo "<ol type=1>";
for($i = 0; $i <= $r_n; $i++){
echo "<li>$resultsї$i]";
}
echo "</ol>";
?>
or something close :-)This would be the way that I'd do it:

Code: Select all
<?php
$sql = mysql_query("SELECT * FROM table_name");
$row = mysql_fetch_array($sql);
for ($i = 0; $i < mysql_num_rows($sql); $i++) {
echo("<B>".$i.".</B>".$rowї$i]."<BR>");
}
?>rather than a for loop, what about a while loop
and because " are used in the print/echo statement you don't need to do the concatenation
i.e.
print "<B>".$i."</B>....etc - don't need to do it like this.
Mike
Code: Select all
<?php
$sql = mysql_query("SELECT * FROM table_name");
$i = 1;
while ($row = mysql_fetch_array($sql)) {
print "<B>$i</B>$rowї'COUMN_NAME']<BR>");
$i++;
}
?>i.e.
print "<B>".$i."</B>....etc - don't need to do it like this.
Mike
I'd take a combination of all this
because the data is encapsulated within a single tag (<ol>) that fits its meaning (it IS a list
) and the browser has to take care for the formating (keep the columns in line)
Code: Select all
<?php
$COLUMN_NAME = 'whatever';
$sql = mysql_query("SELECT $COLUMN_NAME FROM table_name") or die('query failed');
print('<ol>');
while ($row = mysql_fetch_row($sql))
print ("<li>{$rowї0]}</li>");
print('</ol>');
mysql_free_result($sql);
?>