Jump to content

How to create a line number on the fly


Julian

Recommended Posts

Hello guys

 

  I'm getting the results from my database using do {} while, this is working fine.  What I want to is put a number starting from 1 to each result, each record has it's unique key but I want to start from 1 with the results, example:

 

do {

echo "Number # Here" . $row_name['name'] . "<br />";

} while ($row_name = mysql_fetch_assoc($name));

 

Output

 

"Number 1 Here"  John

"Number 2 Here"  Teresa

"Number 3 Here"  Jamie

Link to comment
https://forums.phpfreaks.com/topic/199848-how-to-create-a-line-number-on-the-fly/
Share on other sites

You don't want to do a "do" loop, a normal while is needed, otherwise $row_name will be null the first time.

 

We can do it 2 ways, either use a while with a counter, or a for loop. I'll give the for loop example:

 

for($i = 1; $row_name = mysql_fetch_assoc($name); $i++)
{
    echo "Number $i Here" . $row_name['name'] . "<br />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.