2017-03-08 177 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分钟检查和执行,将提供反馈和接受 –