Jump to content

loop problem...


dd_gamer

Recommended Posts

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>'; 
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.