我正在學習PHP通過個人項目工作。我花了最後2個小時試圖弄清楚爲什麼我的IF語句在我的循環中跳過了,我是一個更有經驗的編程人員可以在這裏爲我提供一些幫助,因爲我目前處於僵局。PHP循環跳過,如果陳述
什麼我嘗試做
用戶進行精選在體育比賽一個回合,當輪完成計算基於正確的選秀權數量的用戶積分榜。
一切都按預期工作,除了我有一個(非常)困難時間計算正確的選擇數。
IMAGE2:THE代碼當前推出以下(WRONG)
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語句被忽略/忽略。任何建議/幫助爲什麼和/我如何解決這個非常歡迎。
'回聲$輸出(最後一排只顯示點) ='總計正確選擇'。 $ totalCorrectPicks「。 /'。$ i;' 試着讓它'回聲'總共正確挑選'。 $ totalCorrectPicks「。 /'。$ i;'? – CynePhoba12
你在這裏做的有點奇怪。首先你做'$ user = $ pick ['firstname'];',然後你檢查'if($ user!= $ pick ['firstname']){' - 這永遠是錯誤的,因爲它們總是平等的。 – Qirel
@CynePhoba NO沒有竅門 –