Reputation: 5714
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
IMAGE2: THE CODE CURRENTLY PUTS OUT THE FOLLOWING (WRONG)
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.
Upvotes: 1
Views: 74
Reputation: 93
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>";
Upvotes: 1
Reputation: 9120
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
Upvotes: 1