2013-03-08 83 views
0

如果有人能夠幫助我,請幫助我,我真的不知道如何解決它,我一直在絞盡腦汁(和書籍和互聯網)有一段時間沒有真正理解我需要做什麼。我不是一個純粹自學的程序員,所以如果看起來容易,請接受我的道歉。我爲幻想聯盟足球網站創建了一個數據庫,並且已經獲得了結果和表格的正確查詢,我只想添加一個查詢,以顯示經理的每週結果,並從中得出哪些球員已經爲這些經理打分(沒有經理的團隊有相同的播放器),這樣一個例子:在php中使用多維數組嵌套的foreach循環mysql mysql

(表頭:周>管理>有關>反對>射手) 2 Manager1 3-0(玩家1,玩家b 2) 2 Manager2 1-2(player f 1) 2 Manager3 4-1(player g 2,player x 2)

我使用大量連接來獲得我的結果,並且一直在試驗嵌套的foreach循環,我需要做一個單獨的foreach循環遍歷任何玩家每個進球得分的球隊(即從進球數大於0的球員名單中抽取),但結果好壞參半 - 我沒有任何需要的!

這裏是我的代碼,從索引控制器到顯示foreach循環的html。如果有人能夠幫助或有任何想法,我真的很感激。

if (isset($_GET['action']) and $_GET['action'] == 'scorers') 
    { 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; 

    $sql = "SELECT Wk.week as ptsweeks, managers.managerid as ID, managers.manager as Manager, CASE when sum(Wk.goals)>GA.goals then '3' when sum(Wk.goals)=GA.goals then '1' when sum(Wk.goals)<GA.goals then '0' END as PTS, sum(Wk.goals) as F, GA.goals as A, case when Wk.goals>'0' then concat(Wk.name,' ',Wk.goals) end as goalscs 
from weeks2012 as Wk 
    inner join goalsAgainst2012 as GA 
    inner join strikers2012 as ST 
    inner join defences2012 as DEF 
    inner join managers 
    on 
    Wk.week = GA.week and 
    DEF.managerid = ST.managerid and 
    ST.playerid = Wk.playerid and 
    DEF.defenceid = GA.defenceid and 
    managers.managerid = ST.managerid 
    where Wk.week = '$weekWK' 
    group by managers.managerid"; 
    $result = mysqli_query($link, $sql); 
    if (!$result) 
    { 
    $error = 'Error updating submitted article.'; 
     include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
     exit(); 
    } 

    while ($row = mysqli_fetch_array($result)) 
    { 
    $weeks[] = array(
    'table' => array('ptsweeks' => $row['ptsweeks'], 'ID' => $row['ID'], 'Manager' => $row ['Manager'], 'F' => $row['F'], 'A' => $row['A'], 'PTS' => $row['PTS']), 
'gs' => array('goalscs' => $row['goalscs']) 
    ); 

    } 


    include 'scorers_html.php'; 

} 

** from scorers_html.php : 
    <table> 
    <?php foreach($weeks as $key => $gls): ?> 
    <tr> 
     <td> <?php echo $gls['table']['ptsweeks']; ?> </td> 
     <td> <?php echo $gls['table']['Manager']; ?> </td> 
     <td> <?php echo $gls['table']['F']; ?> </td> 
     <td> <?php echo $gls['table']['A']; ?> </td> 
     <td> <?php echo $gls['table']['PTS']; ?> </td> 
    </tr> 

     <?php foreach($weeks as $key => $gls): ?> 
    <tr> 
     <td> <?php echo $gls['gs']['goalscs']; ?> </td> 
     </tr> 
     <?php endforeach; ?>  
    <?php endforeach; ?> 

    </table> 

回答

0

首先,您有一個語法錯誤,可以通過查看Stackoverflow的語法高亮顯示來查看。你的第三行必須

include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; 

經進一步審查,下面的線也是不正確的:

include $_SERVER['DOCUMENT_ROOT'] . /includes/error.html.php'; 

應該

include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 

您需要封裝報價或內的所有字符串撇號。

我會推薦使用代碼編輯器,其中包括語言突出顯示爲您的語言,如KomodoEdit或Netbeans;你會節省很多頭痛。

如果這沒有幫助,請發佈您遇到的具體錯誤/問題。

+0

非常感謝你看着它戴夫。我遇到的主要問題是查詢的結果顯示每個經理的每個得分手。目前,這段代碼給出了每個經理人名單的相同列表,而不僅僅是那些只得到了單個經理人的得分者。所以我不知道是否我的選擇查詢是不正確的(這一點:concat(weeks2012.name,'',weeks2012.goals)作爲Scorers),如果它是查詢的結尾(我試過GROUP BY和ORDER BY),或者如果它是HTML文件中的foreach循環 - 或者三者的組合。這有任何意義嗎? – AK31 2013-03-11 02:41:58