SchweppesAle Posted September 29, 2009 Share Posted September 29, 2009 Not sure why the last if statement keeps executing even after I set $Add_this_user to false within the second "foreach" loop. How would I correct this? foreach($users as $user) { $Add_this_user = TRUE; foreach($stream_users as $stream_user) { $todays_date = date("Y-m-d G"); if(($user[1] == $stream_user[4]) && ($stream_user[3] == 'active')) { if(((preg_match('/A/', $user[8])) == 0) || ($user[9] < $todays_date) || ($user[7] == 1) || ($user[5] == 0) || ($user[6] == 0) || ($user[10] == 1)) { $Add_this_user = FALSE; } } $Add_this_user = FALSE; } if($Add_this_user) { //Do Something } } Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/ Share on other sites More sharing options...
mikesta707 Posted September 29, 2009 Share Posted September 29, 2009 I don't see how the last if statement wouldn't execute with what you have there? What is the logic that would cause the second if statement to not run? if you want to exit a loop, use break like for ($i = 0; $i < 10; $i++){ //i want to exit this loop at 5 if ($i == 5){ break; } } Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927225 Share on other sites More sharing options...
cags Posted September 29, 2009 Share Posted September 29, 2009 That's true but somewhat irrelevant mikesta707. Assuming I understand the OP correctly. Regardless of the code inside the inner foreach loop, when the code exits this loop the value of $Add_this_user would always be false. The OP seems to indicate that the // do something code is still being performed (which doesn't sound right to me). Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927233 Share on other sites More sharing options...
SchweppesAle Posted September 29, 2009 Author Share Posted September 29, 2009 I don't see how the last if statement wouldn't execute with what you have there? What is the logic that would cause the second if statement to not run? if you want to exit a loop, use break like for ($i = 0; $i < 10; $i++){ //i want to exit this loop at 5 if ($i == 5){ break; } } If you look at the second foreach statement "foreach($stream_users as $stream_user)" you'll notice that in every iteration I'm setting the value of $Add_this_user to FALSE in order to figure out why the last if statement always executes. "if($Add_this_user)" //$Add_this_user has been set to FALSE repeatedly in the above foreach statement. It should only be reset to TRUE after the first foreach statement "foreach($users as $user)" completes its iteration Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927236 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 The only reason that I can think of is a situation where $stream_users has no elements so that foreach loop isn't preformed at all. For debugging I'd suggest placing an echo statement right after or before $Add_this_user is set to false within the foreach statement to see if it's even being executed. That or just print_r (or var_dump()) $stream_users to confirm that it has contents. Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927237 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 if NOT add this user.............. if(!$Add_this_user) { //Do Something } } try Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927238 Share on other sites More sharing options...
abazoskib Posted September 29, 2009 Share Posted September 29, 2009 perhaps there is nothing that makes $Add_this_user = FALSE. Since it is being set as TRUE at each iteration of the foreach loop, it is expected that the statements inside the while loop are going to run. Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927240 Share on other sites More sharing options...
cags Posted September 29, 2009 Share Posted September 29, 2009 As the OP pointed out it's coded in such a way that every loop of the inner loop should set it to false. The only explanation I can see is AlexWD's suggestion that $stream_users is empty so the loop isn't running. Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927242 Share on other sites More sharing options...
SchweppesAle Posted September 29, 2009 Author Share Posted September 29, 2009 The only reason that I can think of is a situation where $stream_users has no elements so that foreach loop isn't preformed at all. For debugging I'd suggest placing an echo statement right after or before $Add_this_user is set to false within the foreach statement to see if it's even being executed. That or just print_r (or var_dump()) $stream_users to confirm that it has contents. I actually tried that already. $stream_users is not null. Setting $Add_this_user = FALSE; outside the second foreach statement stops the if statement from executing. Here's the complete function. public function add($users, $stream_users, $config) { $login = $config->get_username(); $password = $config->get_password(); $listid = $config->get_listid(); $add_list = NULL; $request = NULL; /*checks to see if any users need to be added to the list*/ $r = 0; foreach($users as $user) { $Add_this_user = TRUE; foreach($stream_users as $stream_user) { $todays_date = date("Y-m-d G"); if(($user[1] == $stream_user[4]) && ($stream_user[3] == 'active')) { if(((preg_match('/A/', $user[8])) == 0) || ($user[9] < $todays_date) || ($user[7] == 1) || ($user[5] == 0) || ($user[6] == 0) || ($user[10] == 1)) { $Add_this_user = FALSE; } } $Add_this_user = FALSE; } if($Add_this_user) { if($user[1] != NULL) { $add_list[$r][1] = " <parameter id='{emailaddress}'><value>".$user[1]."</value></parameter>"; } if($user[2] != NULL) { $add_list[$r][2] = " <parameter id='{firstname}'><value>".$user[2]."</value></parameter>"; } if($user[3] != NULL) { $add_list[$r][3] = " <parameter id='{lastname}'><value>".$user[3]."</value></parameter>"; } if($user[4] != NULL) { $add_list[$r][4] = " <parameter id='{title}'><value>".$user[4]."</value></parameter>"; } $r++; } } if($add_list != NULL) { // Build the XML for the Request(s) $b = 0; foreach($add_list as $user) { $request[$b] = "<system>". "<action>addsubscriber</action>". "<authorization>". "<username>".$login."</username>". "<password>".$password."</password>". "</authorization>". "<parameterlist>". "<parameter id='List ID'><value>$listid</value></parameter>". "[<parameter id='Auto-Activate'><value>true</value></parameter>]". "<parameterarr>"; foreach($user as $attribute) { $request[$b] .= $attribute; } $request[$b] .= "</parameterarr>". "</parameterlist>". "</system>"; $b++; } } else{$request = NULL;} return $this->_xml = $request; } Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927244 Share on other sites More sharing options...
SchweppesAle Posted September 29, 2009 Author Share Posted September 29, 2009 grrr. return $this->_xml = $stream_users; is showing that the $steam_users variable is in fact passing as a parameter correctly. Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927245 Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 So what do you get as output when you run this? public function add($users, $stream_users, $config) { $login = $config->get_username(); $password = $config->get_password(); $listid = $config->get_listid(); $add_list = NULL; $request = NULL; /*checks to see if any users need to be added to the list*/ $r = 0; foreach($users as $user) { $Add_this_user = TRUE; foreach($stream_users as $stream_user) { $todays_date = date("Y-m-d G"); if(($user[1] == $stream_user[4]) && ($stream_user[3] == 'active')) { if(((preg_match('/A/', $user[8])) == 0) || ($user[9] < $todays_date) || ($user[7] == 1) || ($user[5] == 0) || ($user[6] == 0) || ($user[10] == 1)) { $Add_this_user = FALSE; } } $Add_this_user = FALSE; echo "Add_this_user is FALSE"; } if($Add_this_user) { if($user[1] != NULL) { $add_list[$r][1] = " <parameter id='{emailaddress}'><value>".$user[1]."</value></parameter>"; } if($user[2] != NULL) { $add_list[$r][2] = " <parameter id='{firstname}'><value>".$user[2]."</value></parameter>"; } if($user[3] != NULL) { $add_list[$r][3] = " <parameter id='{lastname}'><value>".$user[3]."</value></parameter>"; } if($user[4] != NULL) { $add_list[$r][4] = " <parameter id='{title}'><value>".$user[4]."</value></parameter>"; } $r++; } } if($add_list != NULL) { // Build the XML for the Request(s) $b = 0; foreach($add_list as $user) { $request[$b] = "<system>". "<action>addsubscriber</action>". "<authorization>". "<username>".$login."</username>". "<password>".$password."</password>". "</authorization>". "<parameterlist>". "<parameter id='List ID'><value>$listid</value></parameter>". "[<parameter id='Auto-Activate'><value>true</value></parameter>]". "<parameterarr>"; foreach($user as $attribute) { $request[$b] .= $attribute; } $request[$b] .= "</parameterarr>". "</parameterlist>". "</system>"; $b++; } } else{$request = NULL;} return $this->_xml = $request; } Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927246 Share on other sites More sharing options...
SchweppesAle Posted September 29, 2009 Author Share Posted September 29, 2009 output: Add_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEAdd_this_user is FALSEadd output:: Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927269 Share on other sites More sharing options...
cags Posted September 29, 2009 Share Posted September 29, 2009 What about if you use this? Do you get HIT printing out alot? foreach($users as $user) { $Add_this_user = TRUE; foreach($stream_users as $stream_user) { $todays_date = date("Y-m-d G"); if(($user[1] == $stream_user[4]) && ($stream_user[3] == 'active')) { if(((preg_match('/A/', $user[8])) == 0) || ($user[9] < $todays_date) || ($user[7] == 1) || ($user[5] == 0) || ($user[6] == 0) || ($user[10] == 1)) { $Add_this_user = FALSE; } } $Add_this_user = FALSE; } if($Add_this_user) { echo "HIT"; if($user[1] != NULL) { $add_list[$r][1] = "<parameter id='{emailaddress}'><value>".$user[1]."</value></parameter>"; } if($user[2] != NULL) { $add_list[$r][2] = "<parameter id='{firstname}'><value>".$user[2]."</value></parameter>"; } if($user[3] != NULL) { $add_list[$r][3] = "<parameter id='{lastname}'><value>".$user[3]."</value></parameter>"; } if($user[4] != NULL) { $add_list[$r][4] = "<parameter id='{title}'><value>".$user[4]."</value></parameter>"; } $r++; } } Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927286 Share on other sites More sharing options...
SchweppesAle Posted September 29, 2009 Author Share Posted September 29, 2009 This is really weird I did a var_dump of the streamsend user variable after returning it via the function and it was in fact not NULL. I tried the code given to me by Alex and the echo function was executing with every iteration However, when testing that same variable before passing it off as a parameter I found that it was NULL. Really can't wrap my head around this; not that it matters. I had to redo the algorithm anyway. foreach($stream_users as $k => $stream_user) { $todays_date = date("Y-m-d G"); if(($user[1] == $stream_user[4]) && ($stream_user[3] == 'active')) { $Add_this_user = FALSE; } } if(((preg_match('/A/', $user[8])) == 0) || ($user[9] < $todays_date) || ($user[7] == 1) || ($user[5] == 0) || ($user[6] == 0) || ($user[10] == 1)) { $Add_this_user = FALSE; } Problem solved. Thanks for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/175967-solved-scope-within-foreach/#findComment-927301 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.