You are here: Re: Urgent help for quiz ranking logic « PHP Programming Language « IT news, forums, messages
Re: Urgent help for quiz ranking logic

Posted by Jerry Stuckle on 02/18/06 17:16

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>";
}
}
}
?>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация