Jump to content

Ranking Loop


dpalame

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/253024-ranking-loop/
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/253024-ranking-loop/#findComment-1297243
Share on other sites

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.