sql - php code to give the position according to their scores (1st, 2nd, 3rd, 3rd, 5th) -


create table if not exists `jss1_prelim` ( `id` int(11) not null auto_increment, `term` varchar(30) not null, `idnumber` varchar(30) not null, `subject` varchar(30) not null, `score` varchar(30) not null, `teacher` varchar(30) not null, primary key (`id`) )   engine=myisam  default charset=latin1 auto_increment=6 ;  insert `jss1_prelim` (`id`, `term`, `idnumber`, `subject`, `score`, `teacher`) values (1, 'prelim', 'sbi-js202', 'computer', '82', 'tid273'), (2, 'prelim', 'sbi-js645', 'computer', '63', 'tid273'), (3, 'prelim', 'sbi-js300', 'computer', '65', 'tid273'), (4, 'prelim', 'sbi-js202', 'igbo', '70', 'tid015'), (5, 'prelim', 'sbi-js645', 'igbo', '65', 'tid015'); 

i need on script, script works fine sorting sql database entries. need php code give position according scores (1st, 2nd, 3rd, 3rd, 5th).

<table border = '2'> <tr> <td>student id</td> <td>score</td> <td>position</td> </tr> <?php  $con = mysql_connect("localhost", "root", ""); if(!$con) {     echo "could not establish connection database " . mysql_error(); }  $mydb = mysql_select_db("sbicenugu", $con); if (!$mydb) {     echo "error selecting database " . mysql_error(); }  $mystatement = mysql_query("select * jss1_prelim order score desc"); $dbcount = mysql_num_rows($mystatement); $selection = array(0 => ""); $nameselection = array(0 => "");  $i = 0;      while ($row = mysql_fetch_array($mystatement))     {     $selection[$i] = $row['score'];     $nameselection[$i] = $row['idnumber'];     $i++;      } arsort($selection); ksort($nameselection);  ($x = 0; $x < $dbcount; $x++) { echo "<tr> <td>$nameselection[$x]</td>  <td>$selection[$x]</td> <td>"; $grades = array($selection[$x]); $occurrences = array_count_values($grades); $grades = array_unique($grades); foreach($grades $grade)  { if($grade == end($grades))$i += $occurrences[$grade]-1; echo str_repeat('-'.($i + 1).'<br>',$occurrences[$grade]); $i+= $occurrences[$grade]; } echo"</td>  </tr>";  } ?> </table> 

this scripts not giving me expected position of student in class. gives different value position score desame. need this.

this sql code give rank column "rank" , works partial results, have right rank when showing second page of multiple pages:

select (select count(*) + 1     jss1_prelim ref     ref.score > main.score) rank,     id, term, idnumber, subject, score, teacher jss1_prelim main order score desc 

make sure put index on score better performance.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -