1

i am trying to create a variable that will contain an email list created from a foreach loop.

Currently the PHP is like so:

  // select all the emails from the users that are subscribed the email list
  $query = "SELECT * FROM users WHERE email_list = 0";
  $query_params = array(); 
  try { 
  $stmt = $db->prepare($query); 
  $result = $stmt->execute($query_params);
  } 
    catch(PDOException $ex) 
  { 
    die("Failed to run query: " . $ex->getMessage()); 
  } 
  $rows = $stmt->fetchAll();
  foreach($rows as $row):
    $email = $row['email'];
    echo "$email, ";
  endforeach;

That echo creates e.g.

email1, email2, email3 etc.

But what i want is to be able to set a variable that i can use after the foreach which will contain all of the emails.

I have tried using a function and then setting a variable with the function being called.

Something like this:

  function createEmailList() {
    $rows = $stmt->fetchAll();
    foreach($rows as $row):
      $email = $row['email'];
      echo "$email, ";
    endforeach;
  }
  echo createEmailList();

But as you can imagine that doesn't work.

Thanks, Craig.

2
  • You want an array containg the emails ? If so, just: $email[] = $row['email']; Commented Aug 18, 2014 at 23:24
  • 1
    If all you want is a variable that contains the same as the echo result change $email = $row['email']; to $email .= $row['email']; Commented Aug 18, 2014 at 23:27

3 Answers 3

2

First, you have to pass your $stmt into the function. Otherwise, $stmt doesn't exist as far as the function knows.

Secondly, echoing out email in a loop is not going to put it into a variable as a list. You need to concatenate it into a string variable (or something) not just echo it out.

Third, if you want the function to be called like echo createEmailList(...); then it needs to return a value.

Fourth, I hate vb-style syntax for a foreach, so I changed it to normal syntax below.

 function createEmailList($stmt) 
 {
    $rows = $stmt->fetchAll();
    $emailS = '';
    foreach($rows as $row)
    {
      if($emailS!='')
      {
         //so it doesn't add the comma before first email 
         //nor after last email
         $emailS .= ', ';
      }
      $emailS .= $row['email'];
    }
    return $emailS;
  }
  echo createEmailList($stmt);
Sign up to request clarification or add additional context in comments.

Comments

2
 function createEmailList($stmt) {
     $emails = array();
     foreach($stmt->fetchAll() as $row) {
         $emails[] = $row['email'];
     }
     return implode(', ', $emails);
 }
 echo createEmailList($stmt);

1 Comment

I like the use of implode.
2

Here's the trick

function createEmailList() {
// fetch rows from DB
    foreach($rows as $row){
        $emails[] = $row['email'];
    }
return $emails;
}

2 Comments

One more issue with syntax. You've got the vb-style syntax and normal syntax combined by keeping the : but changing the endforeach; to }.
my bad, that was a typo! Just corrected it. Can I get an upvote now? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.