Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,685 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,265 people online right now. Registration is fast and FREE... Join Now!




Irritating issue with Foreach Loops

 
Reply to this topicStart new topic

Irritating issue with Foreach Loops, Foreach Loop issue

butangphp
post 4 Jun, 2008 - 10:14 AM
Post #1


New D.I.C Head

*
Joined: 4 Jun, 2008
Posts: 2

Basically what is happening is with each iteration of the main foreach loop (foreach ($challenges as challenge)), it is adding records from the previous loop to $all_users. For example, after the first loop, $all_users = array('bob, brett, brian'). Those are all the users who completed the first challenge. So, the second loop should show only users who completed second challenge (we'll call them Rob, Randy, Roger), however when I print the results, $all_users = array('bob, brett, brian, rob, randy, and roger'). Third iteration shows those 6 + whoever else is new. I'm not sure what I am doing wrong, I included the full code in case you wanted a better idea. Would appreciate any help. Thanks!

CODE
$a = mysql_query("SELECT id, created FROM daily_challenges WHERE (DATE_FORMAT(created, \"%Y-%m-%d\")='2008-06-03')");

while ($row = mysql_fetch_assoc($a))
    $challenges[]=array('id'=>$row['id'], 'created'=>date('Y-m-d', strtotime(''.$row['created'].'')));

foreach($challenges as $challenge)
{
    $usrs = mysql_query("SELECT user_id FROM daily_challenge_votes WHERE poll_id=".$challenge['id']."");
    
    while($row2 = mysql_fetch_assoc($usrs))
        $all_users[] = array('user_id'=>$row2['user_id']);
        
    foreach ($all_users as $user)
    {
        print "\n\n";
        print "showing user: ";
        print_r($user);
        $q1 = mysql_query("SELECT poll_response_id FROM daily_challenge_votes WHERE user_id=".$user['user_id']." AND poll_id=".$challenge['id']." LIMIT 1");
        $poll_response_id = mysql_fetch_assoc($q1);
        
        $q2 = mysql_query("SELECT correct_choice FROM daily_challenge_responses WHERE id=".$poll_response_id['poll_response_id']." LIMIT 1");
        $choice = mysql_fetch_assoc($q2);
        
        $dr = mysql_query("SELECT daily_wins, daily_losses FROM daily_challenge_users WHERE user_id=".$user['user_id']." AND created=".$challenge['created']." LIMIT 1");
            $daily_record = mysql_fetch_assoc($dr);
        $ovr = mysql_query("SELECT total_wins, total_losses FROM daily_challenge_users WHERE user_id=".$user['user_id']." AND created=0000-00-00");
            $overall_record = mysql_fetch_assoc($ovr);
        
        if ($choice['correct_choice']=='1') {
            if (empty($daily_record))
                mysql_query("INSERT INTO `daily_challenge_users` (id, user_id, daily_wins, daily_losses, created) VALUES (NULL, ".$user['user_id'].", 1, 0, ".$challenge['created'].")");
            else {
                $updated_daily_wins = $daily_record['daily_wins'] + 1;
                mysql_query("UPDATE daily_challenge_users SET daily_wins='".$updated_daily_wins."' WHERE user_id='".$user['user_id']."' AND created='".$challenge['created']."'");
            }
            if (empty($overall_record))
                mysql_query("INSERT INTO `daily_challenge_users` (id, user_id, created, total_wins, total_losses) VALUES (NULL, '".$user['user_id']."', 0000-00-00, 1, 0)");
            else {
                $updated_total_wins = $overall_record['total_wins'] + 1;
                mysql_query("UPDATE daily_challenge_users SET total_wins='".$updated_total_wins."' WHERE user_id='".$user['user_id']."' AND created=0000-00-00");
            }
        }
        else {
            if (empty($daily_record))
                mysql_query("INSERT INTO daily_challenge_users (id, user_id, daily_wins, daily_losses, created) VALUES (NULL, '".$user['user_id']."', 0, 1, '".$challenge['created']."')");
            else {
                $updated_daily_losses = $daily_record['daily_losses'] + 1;
                mysql_query("UPDATE daily_challenge_users SET daily_losses='".$updated_daily_losses."' WHERE user_id='".$user['user_id']."' AND created='".$challenge['created']."'");
            }
            if (empty($overall_record))
                mysql_query("INSERT INTO daily_challenge_users (id, user_id, created, total_wins, total_losses) VALUES (NULL, '".$user['user_id']."', 0000-00-00, 0, 1)");
              else {
                  $updated_total_losses = $overall_record['total_losses'] + 1;
                mysql_query("UPDATE daily_challenge_users SET total_losses='".$updated_total_losses."' WHERE user_id='".$user['user_id']."' AND created=0000-00-00");
               }
       }
    }
}    
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 4 Jun, 2008 - 10:22 AM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


What's happening is that you are running through each challenge, and then all the users, but you never clear the $all_users[] array. This causes all the users to go into this array. You need to clear it at the end of the $all_users foreach loop. One way to do this is to create another array that holds each $all_users[] array in a position.

You could set up a multidimensional array called something like $all_users_dump, and save the $all_users array into that array. You would then access everything by saying

$all_users_dump[0] for challenge 1
$all_users_dump[1] for challenge 2

etc.

After copying $all_users into the new array, you would just empty $all_users.

I hope that makes sense. If you need more clarity let me know. Take care.
User is offlineProfile CardPM

Go to the top of the page

butangphp
post 4 Jun, 2008 - 11:15 AM
Post #3


New D.I.C Head

*
Joined: 4 Jun, 2008
Posts: 2

Ahh I see what you're saying that definitely makes sense. Thanks! I'm still having some issues with the actual syntax though. I tried adding a counter before the first foreach loop and then incrementing it at the end, but I think my problem is I'm not sure how to clear the $all_users array at the end of the foreach loop. Here is the code I was testing out (only made changes at the top)

CODE

$i=0;
foreach($challenges as $challenge)
{
    $usrs = mysql_query("SELECT user_id FROM daily_challenge_votes WHERE poll_id=".$challenge['id']."");
    
    while($row2 = mysql_fetch_assoc($usrs))
        $all_users[] = array('user_id'=>$row2['user_id']);
    
    $all_users_dump[$i] = $all_users;
    
    print "\n\n"; print 'ALL USERS ARRAY: '.$i.'';  
    print_r($all_users_dump);
    foreach ($all_users as $user)
    {



QUOTE(akozlik @ 4 Jun, 2008 - 11:22 AM) *

What's happening is that you are running through each challenge, and then all the users, but you never clear the $all_users[] array. This causes all the users to go into this array. You need to clear it at the end of the $all_users foreach loop. One way to do this is to create another array that holds each $all_users[] array in a position.

You could set up a multidimensional array called something like $all_users_dump, and save the $all_users array into that array. You would then access everything by saying

$all_users_dump[0] for challenge 1
$all_users_dump[1] for challenge 2

etc.

After copying $all_users into the new array, you would just empty $all_users.

I hope that makes sense. If you need more clarity let me know. Take care.

User is offlineProfile CardPM

Go to the top of the page

silverblaze
post 5 Jun, 2008 - 09:07 AM
Post #4


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 51



Thanked 3 times
My Contributions


hello butangphp,

did u try this?

CODE

$usrs = mysql_query("SELECT user_id FROM daily_challenge_votes WHERE poll_id=".$challenge['id']."");
     $all_users = array(); // this will intialize the array there by removing the old values.
    while($row2 = mysql_fetch_assoc($usrs))
        $all_users[] = array('user_id'=>$row2['user_id']);
    


by intializing the array each time you can remove the old elements.

hope this will help u..

tak care.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:16AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month