1

Im learning PHP by working on a personal project. Ive spend the last 2-hours trying to figure out why my IF statment is getting skipped inside my loop, Im a more experienced coder could provide me with a bit of help here, as I am currently at a stalemate.

What Im Trying To do

User makes picks for a round in sports tournament, when round is completed calculate the user standings based on number of correct picks.

Everything works as expected except Im having an (extremely) hard time calculating the number of correct picks.

IMAGE1: THE CODE SHOULD PUT OUT THE FOLLOWING enter image description here

IMAGE2: THE CODE CURRENTLY PUTS OUT THE FOLLOWING (WRONG)

enter image description here

MY Code

$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>';

As can be seen on image 2, and the code above the IF statement which should print the total correct games picked by each user is getting skipped / ignored. Any advise / help on why and / how I can fix this very welcome.

3
  • echo $output = 'Total Correct Picks'. $totalCorrectPicks.' / '.$i; Try just making it echo 'Total Correct Picks'. $totalCorrectPicks.' / '.$i; ? Commented Mar 8, 2017 at 3:15
  • 1
    You're doing something a bit weird here.. First you do $user = $pick['firstname'];, then you check if($user != $pick['firstname']){ - this will always be false, because they are always equal. Commented Mar 8, 2017 at 3:17
  • @CynePhoba NO didnt do the trick Commented Mar 8, 2017 at 3:18

2 Answers 2

1

Hope this might help:

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 (Display Points on the last row only)

// Assign variable
$maxGame = 3;

// Last Column
if ($nrGame == $maxGame)
    echo "<td>".$points." / " .$nrGame."</td>";
else
    echo "<td></td>";
Sign up to request clarification or add additional context in comments.

3 Comments

THANK YOU SO MUCH Nyc2X its the best result Ive received yet only problem is I only want the last Column result to display at the lastrow of each user column Please see current and desired output here imgur.com/a/XqvCg
@TimothyCoetzee For that, I think you need to declare number of game.
Yeah the only problem is number of games can vary. Perhaps I can solve it by adding a count() query in my sql statment
1

You have:

foreach($picks as $pick){
    $user = $pick['firstname'];

Then later on - in the same foreach you check for this (which you say is the problem):

if($user != $pick['firstname']){

But at no point do you update $user between the first assignment of $user = $pick['firstname']; and the if($user != $pick['firstname']) statement.

This means that the if($user != $pick['firstname']) will always be skipped because you assigned $user = $pick['firstname'] at the start of the loop.

Edit: In answer to your question - if I understand it correctly. You can simply do something like:

Get the first user name.

// Before the foreach call, need the 'first' user name.
$previousUser = $picks[0]['firstname'];

foreach($picks as $pick){

Check for mis-match:

// For the statement check, where the "problem" is
if($previousUser != $pick['firstname'])

And place this at the end of the loop.

// At the end of the loop
$previousUser = $user;
$i++; //keep track of number games 

3 Comments

Hi Tigger Thank you very much for your answer, I suspected that however... the million dollar question for me is how, do I get and keep track of a change in firstname ($user) then..., inorder for the if() to get executed?
See update edit. I think I understand what you want to do.
Thank you tiger gimmie 5-min to check and implement, will provide feedback and accept

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.