我正在学习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没有窍门 –