xenzy Posted December 16, 2010 Share Posted December 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
xenzy Posted December 16, 2010 Author Share Posted December 16, 2010 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 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++; } Quote Link to comment Share on other sites More sharing options...
xenzy Posted December 16, 2010 Author Share Posted December 16, 2010 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) Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 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++; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.