Jump to content

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 />";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.