2014-05-13 52 views
-3

我有一個幻想曲棍球聯盟的PHP數據庫,我想在一起添加所有的球員總數在一起,我的例子是在這裏。 http://www.cnghl.biz/cnghldb/cnghscorings.php?TeamID=1PHP變量得到一個單一的答案

所以我想舉個例子讓所有的M​​ike Richards加在一起給我一個GP,G,A,PTS等等,這樣我就可以讓一個Mike Richards把他所有的數據放在一個線。有沒有辦法輕鬆做到這一點?或者這會變得複雜嗎?有人能夠給我那個代碼來啓動它嗎?

//Get TeamID from URL 
$iTeamID = $_GET["TeamID"]; 
$iPlayerID= $_GET["PlayerID"]; 
$iSea=$_GET["Sea"]; 
$oteaminfo = mysql_query(" 
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP,   SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID 
FROM Seasonteam 
LEFT JOIN (Players 
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID) 
ON Seasonteam.TeamID = SeasonStats.TeamID 
WHERE Seasonteam.TeamID=$iTeamID; 

") or die(mysql_error()); 


    Print "<br><br><table border=1 cellpadding=2>"; 
    Print "<td><b><center>Player Name</b></td>"; 
    Print "<td><b><center>GP</center></b></td>"; 
    Print "<td><b><center>G</center></b></td>"; 
    Print "<td><b><center>A</center></b></td>"; 
    Print "<td><b><center>PTS</center></b></td>"; 
    Print "<td><b><center>PIM</center></b></td>"; 
    Print "<td><b><center>+/-</center></b></td>"; 
    Print "<td><b><center>PP</center></b></td>"; 
    Print "<td><b><center>SH</center></b></td>"; 
    Print "<td><b><center>GW</center></b></td>"; 
    Print "<td><b><center>GT</center></b></td>"; 
    Print "<td><b><center>S</center></b></td>"; 

    while($row = mysql_fetch_array($oteaminfo)) 
    { 
Print "<tr>"; 
Print '<td><a href="cnghlplayerinfo.php?PlayerID=' . $row['PlayerID'] . '" style="text-decoration:none;">' . $row['FullName'] . '</a></td>'; 
Print "<td><center>".$row['GP']."</center></td> "; 
Print "<td><center>".$row['Goals']."</center></td> "; 
Print "<td><center>".$row['Assists']."</center></td> "; 
Print "<td><center>".$row['Points']."</center></td> "; 
Print "<td><center>".$row['Pim']."</center></td> "; 
Print "<td><center>".$row['PlusMinus']."</center></td> "; 
Print "<td><center>".$row['PP']."</center></td> "; 
Print "<td><center>".$row['SH']."</center></td> "; 
Print "<td><center>".$row['GW']."</center></td> "; 
Print "<td><center>".$row['GT']."</center></td> "; 
Print "<td><center>".$row['S']."</center></td> "; 
Print "</tr>"; 
    } 
    Print "</table>"; 
+0

嗯......將它們加在一起? http://en.wikipedia.org/wiki/Addition –

+0

你想團隊總結嗎?我不明白...把它們加在一起? – fmgonzalez

+0

@fmgonzalez我想我已經在想這個了,我正在努力解決這個問題,所以我只給每個玩家一行,他的總數顯示在右邊。如果我添加它們,我會不會顯示4到5個玩家副本,因爲我有多個季節顯示? –

回答

0

看起來你應該能夠使用GROUP BY,讓每播放器1行,然後使用聚合SUM函數來獲得這些統計數據的總和。下面,我展示瞭如何將他們彙總爲目標,助手和積分。看看GROUP BY子句和聚合函數:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, 
     SUM(SeasonStats.Goals) AS totalGoals, SUM(SeasonStats.Assists) AS totalAssists, 
     SUM(SeasonStats.Points) AS totalPoints, SeasonStats.Pim, SeasonStats.PlusMinus, 
     SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, 
     Players.PlayerID 
FROM Seasonteam 
LEFT JOIN (Players 
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID) 
ON Seasonteam.TeamID = SeasonStats.TeamID 
WHERE Seasonteam.TeamID=$iTeamID 
GROUP BY Players.PlayerID; 

另外,還要確保你逃避你的輸入,以防止SQL注入:http://php.net/manual/en/security.database.sql-injection.php

我會建議你使用的mysqli預處理語句代替不推薦使用的mysql函數。 http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

+0

謝謝@ dm12dm這樣做 –

相關問題