简体   繁体   English

从PHP中的MySQL排序单独计算的结果

[英]Sorting individually calculated results from mysql in php

Currently for a football league, standings are calculated with their own table so I have to manually input/edit Wins, losses, ties, PF and PA. 目前,对于一个足球联赛来说,积分排名是用自己的表格计算出来的,因此我必须手动输入/编辑赢,输,平,PF和PA。 I want to generate this dynamically based on the results of the schedule table which is where scores are recorded. 我想根据进度表的结果动态生成此数据,在该表中记录分数。

So I have the following ugly code: 所以我有以下丑陋的代码:

    $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++;
}

Which currently generates the following html: 当前生成以下html:

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

My question is how can I sort it? 我的问题是如何排序? The old table was easy, but I have no idea how I would take this individual data I'm calculating and then sort it via wins, losses, ties, pf then pa. 旧表很简单,但我不知道如何获取正在计算的单个数据,然后按获胜,输失,平局,pf和pa排序。

datatables is the fastest solution to this problem. 数据表是解决此问题的最快方法。 Give the table a class, call datatabes in the onload, and it will render javascript sorting based on the contents of the html. 给表一个类,在onload中调用datatabes,它将根据html的内容呈现javascript排序。

http://datatables.net/ http://datatables.net/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM