dd_gamer Posted December 2, 2011 Share Posted December 2, 2011 Hello, I have a loop below but the first time the person isn't counted in the if loop... I'm new to php and I know there is something easy I'm missing but I can't see it! Any help you be great. // loop from mysql reults while($row = mysql_fetch_array( $result )) { echo "Person: ".$row['id']; echo "Earned: ".$row['earned']; //set-up php var's $earnedSum = $row['earned']; $firstPerson = $row['id']; if ($lastPerson == $firstPerson){ $earnedTotal = $earnedTotal + $earnedSum ; // Add's the earn units $lastPerson = $firstPerson; }else{ $earnedTotal = 0 ; // reset $lastPerson = $firstPerson; } echo 'This is the total Earned Result = '.$earnedTotal.'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252345-loop-problem/ Share on other sites More sharing options...
dd_gamer Posted December 2, 2011 Author Share Posted December 2, 2011 Here is the "output" from the above code (I'm showing the first 2 people only). You can see that person "one" earned 2 credits but the "total Earned Results = 0" and it should be 2 this makes all the values after it wrong for person "one"! The total for all person one's credits should equal "5" but it shows "3". Person: One Earned: 2 -------->This is the total Earned Result = 0 Person: One Earned: 2 This is the total Earned Result = 2 Person: One Earned: 1 This is the total Earned Result = 3 Person: Two Earned: 1 -------->This is the total Earned Result = 0 Person: Two Earned: 1 This is the total Earned Result = 1 Person: Two Earned: 2 This is the total Earned Result = 3 Person: Two Earned: 1 This is the total Earned Result = 4 Person: Two Earned: 1 This is the total Earned Result = 5 Person: Two Earned: 2 This is the total Earned Result = 7 Person: Two Earned: 2 This is the total Earned Result = 9 Person: Two Earned: 1 This is the total Earned Result = 10 Person: Two Earned: 1 This is the total Earned Result = 11 Person: Two Earned: 1 This is the total Earned Result = 12 Person: Two Earned: 1 This is the total Earned Result = 13 Person: Three Earned: 1 -------->This is the total Earned Result = 0 Person: Three Earned: 1 Quote Link to comment https://forums.phpfreaks.com/topic/252345-loop-problem/#findComment-1293698 Share on other sites More sharing options...
Pikachu2000 Posted December 2, 2011 Share Posted December 2, 2011 I didn't completely analyze your code, but I think this is probably what you're after. if( $lastPerson == $firstPerson || !isset($lastPerson) ) { Quote Link to comment https://forums.phpfreaks.com/topic/252345-loop-problem/#findComment-1293702 Share on other sites More sharing options...
dd_gamer Posted December 2, 2011 Author Share Posted December 2, 2011 Woohoo! That's what I'm taking about... that did it! One more question... How would I go about sending the Person's total's to the database? In this case Person "one" has a total of "5". Then the loop goes to the next person. How do I save that total before I move to the next? Quote Link to comment https://forums.phpfreaks.com/topic/252345-loop-problem/#findComment-1293706 Share on other sites More sharing options...
Pikachu2000 Posted December 3, 2011 Share Posted December 3, 2011 If I was to do it, I'd build an array then run an update query after the data has been displayed. if ($lastPerson == $firstPerson){ $earnedTotal = $earnedTotal + $earnedSum ; // Add's the earn units $lastPerson = $firstPerson; $updates[$firstPerson] = $earnedTotal; Then for the update query, loop the array. Just as an example: foreach( $updates as $k => $v ) { $query = "UPDATE your_table_name SET earned = $v WHERE id = $k; $result = mysql_query($query); } Quote Link to comment https://forums.phpfreaks.com/topic/252345-loop-problem/#findComment-1293747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.