2017-03-08 176 views
1

我正在學習PHP通過個人項目工作。我花了最後2個小時試圖弄清楚爲什麼我的IF語句在我的循環中跳過了,我是一個更有經驗的編程人員可以在這裏爲我提供一些幫助,因爲我目前處於僵局。PHP循環跳過,如果陳述

什麼我嘗試做

用戶進行精選在體育比賽一個回合,當輪完成計算基於正確的選秀權數量的用戶積分榜。

一切都按預期工作,除了我有一個(非常)困難時間計算正確的選擇數。

IMAGE1:該代碼應PUT了以下 enter image description here

IMAGE2:THE代碼當前推出以下(WRONG)

enter image description here

MY代碼

$sql ="SELECT picks.*, results.*, users.* 
     FROM picks 
     inner join results on picks.gameID = results.gameID 
     inner join users on picks.userID = users.userID 
     WHERE picks.tournament = 'SixNations' AND picks.weekNum = '3' 
     order by users.lastname, users.firstname"; 
     $stmnt = $db->prepare($sql); 

     //JUST DISABLING FOR DEBUGING PURPOSES 
     /* 
     $stmnt->bindValue(':tournament', $tournament); 
     $stmnt->bindValue(':weekNum', $round);*/ 
     $stmnt->execute() 
     $picks = $stmnt->fetchAll(); 
     $totalCorrectPicks = 0; 
     echo'<table class="table table-responsive table-striped">'; 
     echo'<thead>'; 
     echo' 
     <tr> 
     <th>FirstName</th> 
     <th>Picked Team</th> 
     <th>Winning Team</th> 
     <th>Margin</th> 
     <th>TOTAL CORRECT PICKS</th> 
    </tr> 
    </thead> 
    <tbody>'; 
    $i = 1; //USED TO COUNT NR GAMES 
     foreach($picks as $pick){ 
      $user = $pick['firstname']; 
      $picked = $pick['selectedTeam']; 
      $result = $pick['won']; 
      $nrGames = $i; 

      echo '<tr>'; 
      echo'<td>'; 
      echo $user; 
      echo'</td>'; 
      echo'<td>'; 
      echo $pick['selectedTeam']; 
      echo'</td>'; 
      echo'<td>'; 
      echo $pick['won']; 
      echo'</td>'; 

     if($picked == $result){ 
      //USER PICKED CORRECT TEAM 
      $totalCorrectPicks = $totalCorrectPicks +1; 
       echo'<td>'; 
        $margin = abs($pick['pts'] - $pick['points']); 
        echo $margin; 
       echo'</td>'; 
     }//if 
     else if($picked != $result){ 
      //user PICKED INCORRECT TEAM 
      echo'<td>'; 
      $totalCorrectPicks += 0; 
      $margin = '<span style="color:red">WRONG PICK</span>'; 
      echo $margin; 
      echo'</td>';  
     }//else if 
     ##THIS IF STATMENT IS GETTING IGNORED## 
     if($user != $pick['firstname']){ 
      echo'<td>'; 
     echo $output = 'Total Correct Picks'. $totalCorrectPicks.'/'.$i; 
      echo'</td>'; 
      echo'</tr>'; 
     }//if 
      $i++; //keep track of number games 
     }//foreach 
     echo'</tbody>'; 
     echo'</table>'; 

從圖2和上面的代碼可以看出,應該打印每個用戶選擇的總正確遊戲的IF語句被忽略/忽略。任何建議/幫助爲什麼和/我如何解決這個非常歡迎。

+0

'回聲$輸出(最後一排只顯示點) ='總計正確選擇'。 $ totalCorrectPicks「。 /'。$ i;' 試着讓它'回聲'總共正確挑選'。 $ totalCorrectPicks「。 /'。$ i;'? – CynePhoba12

+1

你在這裏做的有點奇怪。首先你做'$ user = $ pick ['firstname'];',然後你檢查'if($ user!= $ pick ['firstname']){' - 這永遠是錯誤的,因爲它們總是平等的。 – Qirel

+0

@CynePhoba NO沒有竅門 –

回答

1

希望這可以幫助:

CODE 1

// Assign Variable 
$user = $pick['firstname']; 
$nrGame = 1; 

// Check user are the same from previous row or not 
if($tempUser <> $user){ 
    $points = 0; 
    $nrGame = 1; 
} 

// Pick correct team 
if ($picked == $result){ 
    $points += 1; 
} 

// Last Column 
echo "<td>".$points."/" .$nrGame."</td>"; 

// Assign temporary user 
$tempUser = $user; 

$nrGame++; 

CODE 2

// Assign variable 
$maxGame = 3; 

// Last Column 
if ($nrGame == $maxGame) 
    echo "<td>".$points."/" .$nrGame."</td>"; 
else 
    echo "<td></td>"; 
+0

非常感謝Nyc2X它是我收到的最好結果但唯一的問題是我只想最後的列結果顯示**在每個lastrow用戶列**請在這裏查看當前和期望的輸出http://imgur.com/a/XqvCg –

+0

@TimothyCoetzee爲此,我認爲你需要聲明遊戲的數量。 – Nyc2x

+0

是的唯一的問題是遊戲的數量可能會有所不同。也許我可以通過在我的sql語句中添加一個'count()'查詢來解決它 –

1

您有:

foreach($picks as $pick){ 
    $user = $pick['firstname']; 

再後來就 - 在同一foreach您檢查這個(你說的是這個問題):

if($user != $pick['firstname']){ 

但在任何時候你更新$user在第一次轉讓$user = $pick['firstname'];if($user != $pick['firstname'])聲明之間。

這意味着if($user != $pick['firstname'])將始終被跳過,因爲您在循環的開始時分配了$user = $pick['firstname']

編輯: 在回答你的問題 - 如果我理解正確。你可以簡單地做這樣的事情:

獲取第一個用戶名。

// Before the foreach call, need the 'first' user name. 
$previousUser = $picks[0]['firstname']; 

foreach($picks as $pick){ 

檢查不匹配:

// For the statement check, where the "problem" is 
if($previousUser != $pick['firstname']) 

,並把這個在循環的結束。

// At the end of the loop 
$previousUser = $user; 
$i++; //keep track of number games 
+0

嗨Tigger非常感謝您的回答,但我懷疑......但對我而言,百萬美元的問題是如何獲得並跟蹤名字變化('$ user ')然後...,爲了讓'if()'得到執行? –

+0

請參閱更新編輯。我想我明白你想要做什麼。 – Tigger

+0

謝謝老虎gimmie 5分鐘檢查和執行,將提供反饋和接受 –