|
Posted by Michael Austin on 02/18/06 18:50
Jerry Stuckle wrote:
> Jacob.Lyles@gmail.com wrote:
>
>> See if this helps. Sorry if it doesn't work, I'm new at this.
>>
>> Near the bottom of the while loop, I set the current value of
>> $rs[total] to a new variable called $temp_total then I move to the next
>> record in the record set. If the total of the next record is the same
>> as the total from the previous record I will not increment $i, so the
>> rank will be the same. The result is that I display all the records
>> from the table in descending order and any records that have the same
>> total will have the same rank. Here it is:
>>
>> if (!isset($submit)) {
>>
>> $sql ="select pkey,total from `quiz_percentile` order
>> by total desc";
>> $percentile = mysql_query($sql) or die (mysql_error());
>> $rs = mysql_fetch_array($percentile);
>> $i=0;
>>
>> while ($rs) {
>>
>> if ($rs[total] != $temp_total) {
>> $i++ ;
>> }
>>
>> echo "Your total is ".$rs[total]." your rank is
>> ".$i."
>> and your pkey is ".$rs[pkey];
>>
>> $temp_total = $rs[total];
>> $rs = mysql_fetch_array($percentile);
>> }
>> }
>>
>
> Your problem here is you must still increment $i - but you must display
> an old value.
>
> For instance - if three people tie for first, all must have '1' by their
> name. But the next place would be '4', not '2'.
>
> Something like (not tested):
>
>
> if (!isset($submit)) {
> $sql ="select pkey,total from `quiz_percentile` order by total desc";
> $percentile = mysql_query($sql) or die (mysql_error());
> $i=0;
> $savedTotal = -1; // Initialize temps to impossible values
> $currentPlace = -1;
>
> while ($rs = mysql_fetch_array($percentile)) {
> $i++;
> if ($savedTotal != rs['total']) {
> savedTotal = rs['total'];
> currentPlace = $i;
> }
> $sql_insert = "update quiz_percentile set rank='".$currentPlace."'
> where pkey='".$rs["pkey"]."' order by total desc";
> $arr = mysql_query($sql_insert) or
> die(disp_message(mysql_error(),"javascript:history.back()"));
> if($rs["total"]/$rs["total"]=1) {
> echo ($rs["total"]=$rs["total"])."<br>";
> }
> }
> }
> ?>
>
>
why are you doing this in PHP... let the database handle it. it does a much
better job at things like this..
I am not going to write this for you, but you can use the CASE statement.
--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
[Back to original message]
|