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.
$email[] = $row['email'];$email = $row['email'];to$email .= $row['email'];