如果声明,PHP循环跳过

时间:2017-03-08 03:12:10

标签: php mysql

我通过处理个人项目来学习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语句。任何关于为什么和/的建议/帮助我怎么能解决这个问题非常受欢迎。

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分:1)

希望这可能有所帮助:

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

代码2 (仅显示最后一行的点数)

// Assign variable
$maxGame = 3;

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