Jump to content

Put a While statement into a variable


Bman900

Recommended Posts

<?php 
$result = mysql_query("SELECT email FROM newsletter ORDER BY id");


while($row = mysql_fetch_assoc( $result ))
{
    echo "".$row['email'].", ";

} 
?>

 

This prints a list like [email protected], [email protected], [email protected], which is perfect as I want to send out emails to those people but I need them in a variable such as $email to use with the mailer function. So how would I populate that list into a single variable?

Link to comment
https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/
Share on other sites

<?php 
$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

$list = array();	// start the array
while($row = mysql_fetch_assoc( $result ))
{
    $list[] = $row['email'];	// put it into the list
echo "".$row['email'].", ";

}
// list the array
foreach($list as $email) {
echo $email.'<br>';
}
?>

Ok am making this a bit more complicated. It worked as mentioned above but I want more. The emails won't send out when I add the following:

 

<?php
if($category == "All") {

$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

}
else {

$result = mysql_query("SELECT email FROM newsletter WHERE category ='$category' ORDER BY id");

}
?>

 

Instead of this:

 

 

 

<?php

$result = mysql_query("SELECT email FROM newsletter ORDER BY id");

?>

 

Also here is my email code:

 


<?php

foreach($list as $email) {

      
        mail($email, $subject, $message,"From: $from_email\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1"); 

       
        print "<br>$email\n";
       
        flush();
}

?>

 

 

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.