return true/false? if else in foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jamiller
Forum Commoner
Posts: 26
Joined: Mon Mar 12, 2007 12:25 pm

return true/false? if else in foreach

Post by jamiller »

I'm in the middle of making an email program in php/mysql that sends a variable to my main emailsend.php script. This variable holds the recipients of the email. When the var is sent over it isn't an array so I make it one first:

Code: Select all

 
$emails = $_POST['emails'];
$email_ar = split('[;,]', $emails); 
 
I now need to send each individual email to the mailing function I've made, sendMail with the email address as a param, sendMail($emailaddy); But in that var above I have a bunch of groups that contain many email addresses. So I first need to select the group from my database and then all emails under that group. So if I echoed out the $emails var it could look like: group1; group2; test@testing.com.

I create a foreach loop and an if/else statement to decifer between "group" emailing lists and single addresses like so:

Code: Select all

 
foreach($email_ar as $email) {
   if(!stristr($email, "@")) {  //GROUP MAILING LIST
      $result = mysql_query("SELECT * FROM `newsalert` WHERE `group`='$email'");
      while($row = mysql_fetch_array($result)) {
         sendMail($row['email']);
      }
   } else { //SINGLE ADDRESS
      sendMail($email);
   }
}
 
My sendMail function looks like so:

Code: Select all

 
function sendMail($emailaddy) {
   echo $emailaddy . "<br />";
}
 
For testing purposes all sendMail does for now is echo out the param.

Wierd thing is that the foreach loop only processes the first array element. If it is a group it will correctly echo out all emails associated with the group and nothing more. Same if the first array element is a single address. It echoes only one address and then stops.

There is no other php code on the page other than a db connection script. The array split works fine. I've used print_r($email_ar) which prints: Array ( [0] => group1 [1] => group2 [2] => test@testing.com ).

So I'm thinking, and I could be very wrong here, that it's a return true/false thing (???). But after hours and days working on this script I'm about ready to throw my computer out the window!!

Please Help!! My computer is too young to go now, she still has a lot in her...

THANKS! -Jeff
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: return true/false? if else in foreach

Post by www.WeAnswer.IT »

I have had some weird issues with PHP and stristr() before. Sometimes it will return an integer, sometimes it will return a string. The documentation in the manual is either wrong, or it misses some bug.

Your other problem is that you are trying to access $row['email'] but you got row with mysql_fetch_array().

mysql_fetch_array() will return an array with numerical indexes. You want to use mysql_fetch_assoc(), which will return an associative array, with each index being the column name, and each element being the column value.

Try this:

Code: Select all

foreach($email_ar as $email) {
    if(strpos($email, "@") == -1) {  //GROUP MAILING LIST
       $result = mysql_query("SELECT * FROM `newsalert` WHERE `group`='$email'");
       while($row = mysql_fetch_assoc($result)) {
          sendMail($row['email']);
       }
    } else { //SINGLE ADDRESS
       sendMail($email);
    }
 }
Last edited by www.WeAnswer.IT on Mon Mar 24, 2008 2:36 pm, edited 1 time in total.
User avatar
jamiller
Forum Commoner
Posts: 26
Joined: Mon Mar 12, 2007 12:25 pm

Re: return true/false? if else in foreach

Post by jamiller »

Thanks for your reply!

When I used your code, however, I got the same results.

When I plugged in:

Code: Select all

 
if(strpos($email, "@") == -1)
 
it read all elements as a single email entry. So I echoed out the strpos of the elements and changed the code to this:

Code: Select all

 
$emails = $_POST['emails];
$email_ar = split('[;,:]', $emails);
foreach($email_ar as $email) {
   echo "strpos: " . strpos($email, "@") . "<br />";
   if(strpos($email, "@") == '') {
      $result = mysql_query("SELECT * FROM `newsalert` WHERE `group`='$email'");
      while($row = mysql_fetch_assoc($result)) {
         echo "group:" . $email . " " . $row['email'] . "<br />";
      }
   } else {
      echo "single: " . $email . "<br />";
   }
}
 
I'm echoing out the while loop so I can see exactly what is going on, what group the emails are pulling from, etc. And this is what I get:

Code: Select all

 
strpos:
group:group1 testing@group1.com
strpos:
strpos: 5
single: test@testing.com
 
Should note, this is the $_POST['emails'] which is $emails:
group1; group2; test@testing.com

Where group1 only consists of one email, testing@group1.com

So the code is definitely seeing the second array element (group2) but is not doing anything with it. Weird...
User avatar
jamiller
Forum Commoner
Posts: 26
Joined: Mon Mar 12, 2007 12:25 pm

Re: return true/false? if else in foreach

Post by jamiller »

I should also note that the code is successful with multiple single email addresses. Only with group emails is it quitting after the first array element that the if statement processes as true.
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: return true/false? if else in foreach

Post by www.WeAnswer.IT »

strpos() does not appear to be returning anything because I made a mistake! Doh!

In PHP, strpos() returns false if it does not find what you're asking it to look for. Can you try this code?

Code: Select all

 
$emails = $_POST['emails'];
$email_ar = split('[;,:]', $emails);
foreach($email_ar as $email) {
   $pos = strpos($email, "@");
   echo "strpos: " . ( $pos ? $pos : "@ not found" ) . "<br />";
   if($pos == false) {
      $result = mysql_query("SELECT * FROM `newsalert` WHERE `group`='{$email}'");
      while($row = mysql_fetch_assoc($result)) {
         echo "group: " . $email . ", addresses: " . $row['email'] . "<br />";
      }
   } else {
      echo "address: " . $email . "<br />";
   }
}
 
 
Post Reply