Page 1 of 1

Numbered database query list

Posted: Wed May 29, 2002 10:53 pm
by phice
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?

Posted: Wed May 29, 2002 11:09 pm
by hob_goblin
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++)&#123;
echo "<li>$results&#1111;$i]";
&#125;
echo "</ol>";
?>

or something close :-)

Posted: Thu May 30, 2002 12:49 am
by Tiimmy
This would be the way that I'd do it:

Code: Select all

&lt;?php

$sql = mysql_query("SELECT * FROM table_name");
$row = mysql_fetch_array($sql);

for ($i = 0; $i &lt; mysql_num_rows($sql); $i++) {
    echo("&lt;B&gt;".$i.".&lt;/B&gt;".$row&#1111;$i]."&lt;BR&gt;");
}

?&gt;
:twisted:

Posted: Thu May 30, 2002 12:33 pm
by mikeq
rather than a for loop, what about a while loop

Code: Select all

&lt;?php
 

$sql = mysql_query("SELECT * FROM table_name"); 
$i = 1;

while ($row = mysql_fetch_array($sql)) { 
    print "&lt;B&gt;$i&lt;/B&gt;$row&#1111;'COUMN_NAME']&lt;BR&gt;");
    $i++; 
} 


?&gt;
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

Posted: Thu May 30, 2002 12:50 pm
by volka
I'd take a combination of all this

Code: Select all

&lt;?php
$COLUMN_NAME = 'whatever';
$sql = mysql_query("SELECT $COLUMN_NAME FROM table_name") or die('query failed');  
print('&lt;ol&gt;');
while ($row = mysql_fetch_row($sql))
  print ("&lt;li&gt;{$row&#1111;0]}&lt;/li&gt;"); 
print('&lt;/ol&gt;');
mysql_free_result($sql);
?&gt;
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)