Bman900 Posted October 11, 2009 Share Posted October 11, 2009 <?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 More sharing options...
Philip Posted October 11, 2009 Share Posted October 11, 2009 <?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>'; } ?> Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935041 Share on other sites More sharing options...
Dorky Posted October 11, 2009 Share Posted October 11, 2009 not needed, $list = array(); // start the array [] indicates array Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935049 Share on other sites More sharing options...
Philip Posted October 11, 2009 Share Posted October 11, 2009 You're correct, but it's a good habit to get into, because if you try to do: $list['someIndex'] = 'foo'; you'll get a warning. Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935051 Share on other sites More sharing options...
Dorky Posted October 11, 2009 Share Posted October 11, 2009 that would be reassignment. apples and oranges. Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935058 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 Ok now am confused. You guys are talking about the [] after the $list right? Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935070 Share on other sites More sharing options...
Dorky Posted October 11, 2009 Share Posted October 11, 2009 Ok now am confused. You guys are talking about the [] after the $list right? lol, yes. just follow the kings advice. sorry Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935080 Share on other sites More sharing options...
Philip Posted October 11, 2009 Share Posted October 11, 2009 @Dorky - Oops, sorry I was thinking of something else. @Bman900 - ignore the conversation that Dorky & I are having Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935104 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935134 Share on other sites More sharing options...
Bman900 Posted October 11, 2009 Author Share Posted October 11, 2009 I solved it why. When I send the emails it losses the list because it refreshes the screen. If I put this in a session and call for it in the same page so when it refreshes, will it solve my problem? Link to comment https://forums.phpfreaks.com/topic/177339-put-a-while-statement-into-a-variable/#findComment-935138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.