Jump to content

mysql_num_rows count rows and echo row number?


xenzy

Recommended Posts

I have a html table displaying data from mysql database. I can count rows properly with mysql_num_rows but is there a way to echo which row number it is. What I want to do is count the rows ordered by cities and echo row number. Thanks for any help.

Link to comment
Share on other sites

Can you clarify what you're trying to do? Do you want each row that is echoed to be numbered consecutively?

 

1 row

2 row

3 row

4 row, etc.

 

Yes, that is what I am trying to do. Have each row echoed its own number consecutively but ordered

Link to comment
Share on other sites

Not sure what you mean by "but ordered", however to prepend the number to the row, all you have to do is initialize a variable with the value of 1 before the loop, echo it and increment it with each iteration.

 

$row_num = 1;
while( $array = mysql_fetch_assoc($result) ) {
     echo "$row_num " ;
     echo // all your other stuff.
     $row_num++;
}

Link to comment
Share on other sites

Not sure what you mean by "but ordered", however to prepend the number to the row, all you have to do is initialize a variable with the value of 1 before the loop, echo it and increment it with each iteration.

 

$row_num = 1;
while( $array = mysql_fetch_assoc($result) ) {
     echo "$row_num " ;
     echo // all your other stuff.
     $row_num++;
}

Thank you for the reply I will try to explain myself a little more. I have a table as such:

 

User ID:|

User Rank:|

User Level:|

User Name:|

Tickets Bought:|

 

For the user rank, I want to use php/mysql to count all the rows in the table user_id and then echo them (contiously) from 1 in the html table User Rank (the count has to be ordered by user level though)

Link to comment
Share on other sites

Check to see if the user_rank value is different from the previous record, and if it is, reset the counter to 1. The code below is meant to be illustrative only. It will more than likely need to be modified to work in your script.

 

$query = "SELECT id, user_rank, name FROM users ORDER BY user_rank, name";
$result = mysql_query($query);
$rank= '';
$row_num = 1;
while( $array = mysql_fetch_assoc($result) ) {
if( $rank != $array['user_rank'] ) {
	$row_num = 1;
}
$rank = $array['user_rank'];
echo "$row_num - {$array['name']} - {$array['id']} - {$array['user_rank']}<br>\n";
$row_num++;
}

Link to comment
Share on other sites

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.