Re: Ranks and ties

From: Date: Wed, 21 Jun 2000 04:23:35 +0000
Subject: Re: Ranks and ties
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-2435@lists.php.net to get a copy of this message
On 20 Jun, Melissa Tonelli wrote: > Hello, I have a table that contains info on big companies and the number of > employees at each. Like this: > > Table Companies > -------------------------------- > CoName > CoCity > CoEmployees > > I run a query that outputs the data by CoEmployees descending, so that the > largest companies are listed first. The output is a simple HTML > table. Now I want to add a column to the output that shows the RANK of > each company and I have two questions: > > 1. How, exactly, do I do that? I'm unsure of the best/fastest method. > > 2. How do I deal with companies that have the same number of employees? I > would like them to share the rank. > > Thanks in advance for hints/suggestions. Making a couple of assumptions: 1) CoEmployees is a numeric value 2) Ranking means biggest_num employees = 1, next_biggest=2 etc. Set a couple of counter variables $rank=0; $counter = 1; $oldnum = 0; As you loop through your table results, do something like this: echo "<TR><TD>$CoName</TD><TD>$CoCity</TD><TD>$CoEmployees</TD>"; if($CoEmployees != $oldnum): //not same rank as previous row $rank = $counter; endif; echo "<TD>$rank</TD></TR>"; $oldnum=$CoEmployees; $counter++; Very much off the top of my head and untested, but essentially what you need is to check if the current employee number is the same as the klast; if so they share the same ranking. And you presumably want the rankings to skip if there are 'equals' so you get something like: EMPNUM RANK COUNTER 500 1 1 456 2 2 456 2 3 454 4 4 450 5 5 450 5 6 450 5 7 78 8 8 Hope that makes some sort of sense ;-) Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.general (#2435) next »