Jump to content

something is breaking my loop!! WHY!?


jamiller

Recommended Posts

I'm building an email sending program and have a variable sent to the the emailsend.php script called $emails which contains all the email addresses. Because it's not an array I make it one first:

$emails = $_POST['emails'];
$email_ar = split('[;,:]', $emails];

Now because an element in that array can be a group of many emails as well as single addresses I now need to create two new arrays. For example the $emails var could look like this (Group1; Group2; [email protected]; [email protected]) and so on.

$groups = array();
$singles = array();

foreach($email_ar as $addy) {
	if(strpos($addy, "@") == '') {
		array_push($groups, $addy);
	} else {
		array_push($singles, $addy);
	}
}

So now I have two arrays. One for group emails and one for singles. For this I want to concentrate on the groups array, the singles array works perfectly.

The foreach for the group array:

foreach($groups as $group) {
	echo $group . "<br />";
	$result = mysql_query("SELECT * FROM `newsalert` WHERE `group` = '$group'") or die(mysql_error());
	while($row = mysql_fetch_array($result)) {
		echo $row['email'] . "<br />";
	}
}

For some reason, quite possible a bug (??) the while loop breaks after the first array element. So if the original $groups array has two groups in it: Array ( [0] => Group1 [1] => Group2 ) only the first (Group1) is processed. If Group2 is key [0] and Group1 is key[1] then only Group2 is processed...

 

For some reason it does echo out Group2 because of the first line in the foreach, so I've narrowed it down to the either the while loop or the mysql query (which produces no errors...) This is what is echoed out: (group1 consists of 4 emails and group2 has 3 emails which you can't see here but SHOULD!)

Group1
[email protected]
[email protected]
[email protected]
[email protected]
Group2

 

I don't know what to do now. I've tried everything... Has anyone ever experienced anything like this before? PLEASE HELP!!

 

-Jeff

Link to comment
https://forums.phpfreaks.com/topic/97087-something-is-breaking-my-loop-why/
Share on other sites

Are you sure you have data in your database for group2?

 

 

Also, a few notes...

 

1)

if(strpos($addy, "@") == '')

Will work, but it's not the best way to check something like that. strpos() returns a int, or boolean false. Comparing it (regular comparison) with an empty string will bring you the result you are looking for, but it's better checking (using the strict comparison operator) if false was returned:

if(strpos($addy, "@") === FALSE)

 

2) From the manual:

Note:  If you use array_push() to add one element to the array it's better to use $array[] =  because in that way there is no overhead of calling a function.

Use the "$group[] = ..." syntax, it's faster.

 

 

Orio.

Yes, there is data in the database for Group2 (and Group1). Funny thing is if I make Group1 the first key[0] and Group2 the second key[1] I get num rows of 4 for Group1 and 0 for Group2. However if Group2 is key[0] and Group1 is key[1] I get num rows for Group2 of 3 and 0 for Group1. Yeah, wth!

 

Orio: can you explain the array_push[] thing a little more? I really appreciate the suggestions!

BOOO YAH!

 

Well I think I've figured it out. Looking at the source code after I did a print_r($groups) revealed this:

Array
(
    [0] => Group2
    [1] =>  Group1
)

 

HAHA!! Notice that little space in front of Group2?? Yep.

 

So how do I strip that white space? It can't strip ALL white spaces, however, because some of my groups include white space. So how would I strip the first white space in the array element?

 

Thanks!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.