2012-12-08 47 views
1

目前對於足球聯賽來說,積分榜是用自己的表計算的,所以我必須手動輸入/編輯勝利,損失,聯繫,PF和PA。我想根據記錄分數的時間表結果動態生成這個數據。從php中排序單獨計算的結果php

所以,我有以下醜陋的代碼:

$teamidsql = mysql_query("select team_id, count(team_id) as numteams from team WHERE league_id=$currentleague AND team_div='$div'"); 
$teamid = mysql_result($teamidsql,0,"team_id"); 
$numteams = mysql_result($teamidsql,0,"numteams"); 
$endteams = $teamid+$numteams; 
while($teamid < $endteams) 
{ 
$result2=mysql_query("select team_name, team_div, 
count(case when (schedule_team1_id = $teamid and schedule_team1_score > schedule_team2_score) or (schedule_team2_id = $teamid and schedule_team2_score > schedule_team1_score) then 1 else NULL end) as wins, 
count(case when (schedule_team1_id = $teamid and schedule_team1_score < schedule_team2_score) or (schedule_team2_id = $teamid and schedule_team2_score < schedule_team1_score) then 1 else NULL end) as losses, 
count(case when schedule_team1_score = schedule_team2_score then 1 else NULL end) as ties, 
sum(case when schedule_team1_id = $teamid then schedule_team1_score else schedule_team2_score end) as pf, 
sum(case when schedule_team1_id <> $teamid then schedule_team1_score else schedule_team2_score end) as pa 
from schedule, team 
where team_id = $teamid AND team.team_div='$div' AND schedule_week >= 1 and schedule_week <= 13 and schedule.league_id = $currentleague and (schedule_team1_id = $teamid or schedule_team2_id = $teamid)") or die(mysql_error()); 

    $t_n = mysql_result($result2,0,"team_name"); 
    $t_w = mysql_result($result2,0,"wins"); 
    $t_l = mysql_result($result2,0,"losses"); 
    $t_t = mysql_result($result2,0,"ties"); 
    $t_pf = mysql_result($result2,0,"pf"); 
    $t_pa = mysql_result($result2,0,"pa"); 

    echo "<tr>"; 
    echo "<td>$t_n</td><td>$t_w</td><td>$t_l</td><td>$t_t</td><td>$t_pf</td><td>$t_pa</td>"; 
    echo "</tr>"; 

    $teamid++; 
} 

目前生成以下HTML:

http://i.stack.imgur.com/sBVfM.png

我的問題是我怎麼能進行排序呢?舊桌子很簡單,但我不知道如何將這些個人數據計算在內,然後通過勝利,損失,關係,pf和pa進行排序。

+0

**注意!** PHP將被刪除*的*'mysql_'系列函數在未來的版本。如果可以,請移至[PDO](http://php.net/book.pdo)或[mysqli](http://php.net/book.mysqli)! – Charles

回答

0

datatables是解決這個問題的最快解決方案。給表格一個類,在onload中調用datatabes,並且它會根據html的內容呈現javascript排序。

http://datatables.net/