dpalame Posted December 12, 2011 Share Posted December 12, 2011 I am trying to figure out how to assign a number rank to a set of results, but I need to reset the ranking when a certain column changes. For example: Brand Rank a 1 a 2 a 3 b 1 b 2 b 3 I set $rank=0; and then for each item in my loop I add $rank++; How do I get the rank to reset in the loop when the brand changes? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/253024-ranking-loop/ Share on other sites More sharing options...
Psycho Posted December 12, 2011 Share Posted December 12, 2011 How do I get the rank to reset in the loop when the brand changes? You just answered your own question. $last_brand = ''; while($row = mysql_fetch_assoc($result)) { //Check if this brand is the same as the last if($last_brand != $row['brand']) { $rank = 0; $last_brand = $row['brand']; } $rank++; //Display results } Quote Link to comment https://forums.phpfreaks.com/topic/253024-ranking-loop/#findComment-1297243 Share on other sites More sharing options...
dpalame Posted December 12, 2011 Author Share Posted December 12, 2011 Thank you so much works perfect! Quote Link to comment https://forums.phpfreaks.com/topic/253024-ranking-loop/#findComment-1297251 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.