473,396 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Urgent help for quiz ranking logic

Dear All,

I have score table in which i am storing scores for online quiz
what i want is to allot rank to all candidates as per their scores
for that i applied logic that i sorted table order by net score in desc
order
and assigned variable $i++ in place rank correponding to pkey of that
row
so in this manner person who is having net score 10 would get rank 1
and so on.
but what i want is if some scorer will get same score say 5 than in
this case he should both should get rank 5 but in this point my
variable $i++ will allot different rank for same scorer viz 5 and 6 so
i want rank would remain same for same score.

Please help me urgently....

below is the test code

<? include("libFunctions.php");
connectDb();
session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body>
<?
if (!isset($submit))
{
$sql ="select pkey,total from `quiz_percentile` order by total desc"
;
$percentile = mysql_query($sql) or die (mysql_error());
$i=0;

while ($rs = mysql_fetch_array($percentile))
{
//echo $rs["total"]."This is total"."<br>";
$i++;
//echo $i."<br>";
$sql_insert = "update quiz_percentile set rank='".$i."' where
pkey='".$rs["pkey"]."' order by total desc";
//echo $sql_insert."<br>";
$arr = mysql_query($sql_insert) or
die(disp_message(mysql_error(),"javascript:history .back()"));
if($rs["total"]/$rs["total"]=1)
{
//echo "This is matching<br>";
echo ($rs["total"]=$rs["total"])."<br>";
}
}

}
?>
</body>
</html>

Feb 18 '06 #1
8 1485
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);
}
}

Feb 18 '06 #2
Hey, I posted the last solution. I changed my settings so that
hopefully my email won't be shown for all the world to see. Hope this
works.

By the way, that was my first attempt at helping someone in PHP. I am
very new. I'll be proud of myself if it works (I don't have a testing
server set up at home).

Feb 18 '06 #3
Ja*********@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.
js*******@attglobal.net
==================
Feb 18 '06 #4
Jerry Stuckle wrote:
Ja*********@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
:)
Feb 18 '06 #5
Michael Austin wrote:
Jerry Stuckle wrote:
Ja*********@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.


I'd love to see you do this in MySQL alone.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #6
I would think that you would want to re-rank all your records every
time you displayed the results. Personally, unless I was planning on
doing a join on the rank field (which seems pointless), I would keep
SQL out of the picture as much as possible to maintain maximum
dynamicness.

Feb 18 '06 #7
On 2006-02-18, Kapil Jain <ka*************@gmail.com> wrote:
Dear All,
Please help me urgently....

below is the test code
try this...

I also modified the SQL to do fewer uodates... if all who score '10' get rank
1 then all uppdates can be done in one query.

<?
if (!isset($submit))
{
$sql ="select pkey,total from `quiz_percentile` order by total desc";

percentile = mysql_query($sql) or die (mysql_error());
$i=0;
$rank='-1' ; $score=-1;

while ($rs = mysql_fetch_array($percentile))
{
//echo $rs["total"]."This is total"."<br>";
$i++;

if($score != $rs['total'])
{
$rank=$i;
$score=$rs['total'];
$sql_insert = "update quiz_percentile set rank='$rank' "
."where total='$score' ;" ;

$arr = mysql_query($sql_insert) or
die(disp_message(mysql_error(),"javascript:history .back()"));
}
} ?>
</body>
</html>

--

Bye.
Jasen
Feb 19 '06 #8
If you have the pkey at the start of this, you can just generate rank
and get the total pretty easily on the fly. Of course this require's
MySQL 5 w/ nested query support. This way you aren't messing with your
records and if you insert or update your database it will generate
everyone's rank accurately on the fly when it is requested. Index your
total field and you should be fine even for large databases.

$result = mysql_query("SELECT total, (COUNT( * ) +1) AS rank FROM
quiz_percentile WHERE total > ( SELECT total FROM quiz_percentile WHERE
pkey =$id )")'
while($myrow=mysql_fetch_array($result))
{
$myRank = $myrow["rank"];
$myTotal = $myrow["total"];
}

echo "Your total is $myTotal your rank is $myRank and your pkey is
$id";

- Clay

Feb 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Petre Huile | last post by:
I have designed a site for a client, but they have hired an internet marketing person to incrase their search engine ranking and traffic. He wants to put extra-large fonts on every page which will...
2
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
4
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
11
by: admin | last post by:
Hi all, First time poster here... I'm a webmaster and I'd like to add a simple script to my website which will allow users to fill in a brief multiple choice questionaire, and then provide a...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
1
by: bikash607 | last post by:
Dear Expert, i m from very backward place in Bhutan and i need your assistance for a quiz program. I am trying to build a quiz program in Flash 6.0 and if you have any samples, please let me...
3
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible...
1
by: AlokBajaj | last post by:
Iam having difficulty in writing the logic for Ranking. I have 3 fields namely City, Service and Score. There can be multiple records with the same value For EG : Calcutta TL 50...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.