Jump to content

mysql_num_rows count rows and echo row number?


xenzy

Recommended Posts

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

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++;
}

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)

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++;
}

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.