gpt4 book ai didi

PHP Loop 跳过 if 语句

转载 作者:行者123 更新时间:2023-11-29 06:04:25 25 4
gpt4 key购买 nike

我通过个人项目学习 PHP。我花了最后 2 个小时试图弄清楚为什么我的 IF 语句在我的循环中被跳过,我是一个更有经验的编码员可以在这里为我提供一些帮助,因为我目前处于僵局。

我想做什么

用户在体育赛事中进行一轮选择,轮次结束后根据正确选择的数量计算用户名次。

除了我(极其)难以计算正确选择的数量外,一切都按预期进行。

IMAGE1:代码应包含以下内容 enter image description here

IMAGE2:代码当前显示以下内容(错误)

enter image description here

我的代码

$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 语句被跳过/忽略。 关于原因和/的任何建议/帮助非常欢迎我如何解决这个问题。

最佳答案

希望这可能有所帮助:

代码 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>";

关于PHP Loop 跳过 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com