2013-07-20 16 views
-1

我一直在努力與以下代碼很長一段時間了。我是PHP新手。這個mysql的代碼是100%在phpmyadmin中工作。我正在使用<?php echo $row['Rank'];?>,但我只給所有學生1分。但是sql代碼工作正常。我已經在這裏做了小提琴http://sqlfiddle.com/#!2/24855/1請幫助我。獲取此工作的任何替代解決方案? PHP代碼不能正常工作,但MySQL代碼正在工作

Select Distinct regd, Roll_no, Name_of_Student, Test_date, 
English, f_eng, Mizo, f_mz, Hindi, f_hn, Mathematics, f_maths, SS, f_ss, Science, 
f_sc, score, fmscore, perc, Rank FROM (SELECT *, IF(@marks = (@marks := score), @auto, 
@auto := @auto + 1) AS Rank FROM 
(SELECT regd, Roll_no, Name_of_Student, Test_date, 
SUM(IF(`Subject` = 'English', Mark_score, 0)) AS English, 
SUM(IF(`Subject` = 'English', Full_mark, 0)) AS f_eng, 
SUM(IF(`Subject` = 'Mizo', Mark_score, 0)) AS Mizo, 
SUM(IF(`Subject` = 'Mizo', Full_mark, 0)) AS f_mz, 
SUM(IF(`Subject` = 'Hindi', Mark_score, 0)) AS Hindi, 
SUM(IF(`Subject` = 'Hindi', Full_mark, 0)) AS f_hn, 
SUM(IF(`Subject` = 'Mathematics', Mark_score, 0)) AS Mathematics, 
SUM(IF(`Subject` = 'Mathematics', Full_mark, 0)) AS f_maths, 
SUM(IF(`Subject` = 'SS', Mark_score, 0)) AS SS, 
SUM(IF(`Subject` = 'SS', Full_mark, 0)) AS f_ss, 
SUM(IF(`Subject` = 'Science', Mark_score, 0)) AS Science, 
SUM(IF(`Subject` = 'Science', Full_mark, 0)) AS f_sc, 
SUM(Full_mark) AS fmscore, 
SUM(Mark_score) AS score, SUM(Mark_score)/SUM(Full_mark)*100 as perc FROM cxexam, 
(SELECT @auto := 0, @marks := 0) AS init GROUP BY regd ORDER BY score DESC) t) AS result where Test_date between '2013-07-01' and '2013-07-31' 

這是PHP部分。這似乎確定。它也正確地迴應一切,只是它不能正確輸出秩。在排名行中,它只顯示1作爲所有學生的排名。你可以從下面的代碼中理解我想要達到的目標。我真的需要你的幫助。我知道你們很熟悉這樣的問題。

<table width="800" border="1" class="tablestyle" cellpadding="8" cellspacing="6"> 
<tr> 
    <th align="center" width="80">Roll No</th> 
    <th align="center" width="100">Name_of_Student</th> 
    <th align="center" width="40">English</th> 
    <th align="center" width="55">Mizo</th> 
    <th align="center" width="55">Hindi</th> 
    <th align="center" width="55">Math</th> 
    <th align="center" width="70">SS</th> 
    <th align="center" width="40">Science</th> 
    <th align="center" width="70">FM</th> 
     <th align="center" width="70">MO</th> 
     <th align="center" width="40">Percentage</th> 
     <th align="center" width="40">Rank</th> 
     <th align="center" width="40">Result</th> 

</tr> 
<?php 
    while($row=mysql_fetch_array($res)) 
    { 
?> 
<tr> 
    <td align="center"><?php echo $row['Roll_no'];?></td> 
    <td align="left"><?php echo $row['Name_of_Student'];?></td> 
    <td align="center"><?php echo $row['English'];?></td> 
    <td align="center"><?php echo $row['Mizo'];?></td> 
    <td align="center"><?php echo $row['Hindi'];?></td> 
    <td align="center"><?php echo $row['Mathematics'];?></td> 
    <td align="center"><?php echo $row['SS'];?></td> 
    <td align="center"><?php echo $row['Science'];?></td> 
    <td align="center"><?php echo $row['fmscore'];?></td> 
    <td align="center"><?php echo $row['score'];?></td> 
    <td align="center"><?php echo number_format($row['perc'],0);?>%</td> 
    <td><?php echo $row['Rank']; ?></td> 
    <td><?php 
    if ($row['English']/$row['f_eng']*100>=40 && $row['Mizo']/$row['f_mz']*100>=40 && $row['Hindi']/$row['f_hn']*100>=40 && $row['Mathematics']/$row['f_math']*100>=40 && $row['SS']/$row['f_ss']*100>=40 && $row['Science']/$row['f_sc']*100>=40) 
    { 
    echo "<font color=green>Passed</font>"; 
    } 
    else 
    { 
    echo "<font color=red>Failed</font>"; 
    } 
    ?></td> 

</tr> 
<?php 
    } 
?> 
</table> 

這是不工作的部分:rankresult。任何幫助表示讚賞。

回答

2

有幾個問題與您的SQL代碼(例如切勿混用DISTINCTGROUP BY,在內部的選擇等使用WHERE)。話雖這麼說,您的查詢應該是這個樣子

SELECT regd, Roll_no, Name_of_Student, Test_date, 
     English, f_eng, 
     Mizo, f_mz, 
     Hindi, f_hn, 
     Mathematics, f_maths, 
     SS, f_ss, 
     Science, f_sc, 
     score, fmscore, perc, Rank 
FROM 
(
    SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score 
    FROM 
    (
    SELECT regd, Roll_no, Name_of_Student, Test_date, 
      SUM(IF(Subject = 'English' , Mark_score, 0)) English, 
      SUM(IF(Subject = 'English' , Full_mark, 0)) f_eng, 
      SUM(IF(Subject = 'Mizo'  , Mark_score, 0)) Mizo, 
      SUM(IF(Subject = 'Mizo'  , Full_mark, 0)) f_mz, 
      SUM(IF(Subject = 'Hindi'  , Mark_score, 0)) Hindi, 
      SUM(IF(Subject = 'Hindi'  , Full_mark, 0)) f_hn, 
      SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
      SUM(IF(Subject = 'Mathematics', Full_mark, 0)) f_maths, 
      SUM(IF(Subject = 'SS'   , Mark_score, 0)) SS, 
      SUM(IF(Subject = 'SS'   , Full_mark, 0)) f_ss, 
      SUM(IF(Subject = 'Science' , Mark_score, 0)) Science, 
      SUM(IF(Subject = 'Science' , Full_mark, 0)) f_sc, 
      SUM(Full_mark) fmscore, 
      SUM(Mark_score) score, 
      SUM(Mark_score)/SUM(Full_mark) * 100 perc 
     FROM cxexam, (SELECT @n := 0, @p := 0) n 
    WHERE Test_date BETWEEN '2013-07-01' AND '2013-07-31' 
    GROUP BY regd 
    ORDER BY score DESC 
) t 
) r 

這裏是SQLFiddle演示

現在PHP代碼

$link = mysql_connect('localhost', 'user', 'password'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
$db_selected = mysql_select_db('dbname', $link); 
if (!$db_selected) { 
    die ('Can\'t use db : ' . mysql_error()); 
} 

$sql = " 
    SELECT regd, Roll_no, Name_of_Student, Test_date, 
      English, f_eng, 
      Mizo, f_mz, 
      Hindi, f_hn, 
      Mathematics, f_maths, 
      SS, f_ss, 
      Science, f_sc, 
      score, fmscore, perc, Rank 
    FROM 
    (
     SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score 
     FROM 
     (
     SELECT regd, Roll_no, Name_of_Student, Test_date, 
       SUM(IF(Subject = 'English' , Mark_score, 0)) English, 
       SUM(IF(Subject = 'English' , Full_mark, 0)) f_eng, 
       SUM(IF(Subject = 'Mizo'  , Mark_score, 0)) Mizo, 
       SUM(IF(Subject = 'Mizo'  , Full_mark, 0)) f_mz, 
       SUM(IF(Subject = 'Hindi'  , Mark_score, 0)) Hindi, 
       SUM(IF(Subject = 'Hindi'  , Full_mark, 0)) f_hn, 
       SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
       SUM(IF(Subject = 'Mathematics', Full_mark, 0)) f_maths, 
       SUM(IF(Subject = 'SS'   , Mark_score, 0)) SS, 
       SUM(IF(Subject = 'SS'   , Full_mark, 0)) f_ss, 
       SUM(IF(Subject = 'Science' , Mark_score, 0)) Science, 
       SUM(IF(Subject = 'Science' , Full_mark, 0)) f_sc, 
       SUM(Full_mark) fmscore, 
       SUM(Mark_score) score, 
       SUM(Mark_score)/SUM(Full_mark) * 100 perc 
      FROM cxexam, (SELECT @n := 0, @p := 0) n 
     WHERE Test_date BETWEEN '2013-07-01' AND '2013-07-31' 
     GROUP BY regd 
     ORDER BY score DESC 
    ) t 
    ) r"; 

$result = mysql_query($sql); 
if(!$result) { 
    die(mysql_error()); // TODO: better error handling 
} 

while($row = mysql_fetch_assoc($result)) { 
    echo "{$row['regd']} - {$row['Rank']}<br>"; 
} 

輸出(如預期):

 
40 - 1 
2 - 2 
3 - 2 
20 - 3 
+0

我會測試並讓你知道我想出了什麼。 –

+0

它正在工作。非常非常感謝你。你節省了我的時間。 –

+0

還有一件事。我如何格式化表格?請給我一個例子。 –

-1

的問題是在這部分代碼: - SELECT *,IF(@marks =(@marks:=分數),@auto, @auto:= @auto + 1) 您需要確認邏輯是正確的。

+0

我有雙重檢查了sql代碼,並在phpmyadmin中進行了測試。它工作完美。問題似乎是在PHP代碼,但無法弄清楚。 –

1

對於排名的一部分,這是100%的工作。似乎你不知道PHP很好。

<?php 
$rank = $prevScore = 0; 
{  
$count++; // always increment 
if ($row_dep['perc'] > $prevScore) { 
//whenever a non-tie occurs the my rank catches up 
$my_rank = $count; 
$rank = $count; 
} else { 
//whenever a tie occurs, just use the my rank 
$rank = $my_rank; 
} 
$prevScore = $row_dep['perc']; 
echo $rank; 
} 
?> 
+0

我剛剛測試過你的代碼,它不計算關係。 –