Numbered database query list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Numbered database query list

Post 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?
Image Image
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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 :-)
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post 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:
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
Post Reply