MattEldridge Posted November 2, 2007 Share Posted November 2, 2007 Hello, first time posting here. Can anyone tell me why this code... <?php include('connection.php'); $day = date(l); $date = date(jS); $month = date(F); $year = date(Y); $date_string = $day." ".$month." ".$date." ".$year; $subject = "Inside Your Business Events $date_string"; $headers = "From: doNOTreply@mydomain.net\r\n"; $query = mysql_query("SELECT * FROM member_data WHERE mailing_list = 'Yes'"); $query2 = mysql_query("SELECT * FROM iyb_events"); while($email = mysql_fetch_row($query)) { $body = "Dear $email[3],<br />See below for this week's list of exciting events!"; echo $body; while($event = mysql_fetch_array($query2)) { $events = "Event: ".$event[1]."<br />Description: ".$event[2]."<br />Date: ".$event[3]."<br />Start Time: ".$event[4]."<br />Cost: ".$event[5]; echo $events; } } ?> ...produces this output? Dear Glenn, See below for this week's list of exciting events! Event: Groundhog Day Description: We'll stand around shivering and wait for a grounghog to scared. Date: 02/01/2008 Start Time: 4:30 AM Cost: $5.00 Event: Thanksgiving Description: A traditional harvest dinner Date: 11/06/2007 Start Time: 3:30PM Cost: $10.00 Event: The Nex big Thing Description: A convention of some sort Date: 02/01/2008 Start Time: 12:02PM Cost: $10.00 Dear Chris, See below for this week's list of exciting events! Dear Lois, See below for this week's list of exciting events! Dear Peter, See below for this week's list of exciting events! ***************** Of course I want each one to be like the first one. Any help is much appreciated. Thanks. -Matt Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/ Share on other sites More sharing options...
revraz Posted November 2, 2007 Share Posted November 2, 2007 For some reason, this line equals False while($event = mysql_fetch_array($query2)) Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/#findComment-383775 Share on other sites More sharing options...
MattEldridge Posted November 2, 2007 Author Share Posted November 2, 2007 Ok, that makes sense. Something about the first iteration is setting it to FALSE so it's not running thereafter... Any suggestions about how I fix it? Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/#findComment-383782 Share on other sites More sharing options...
MattEldridge Posted November 2, 2007 Author Share Posted November 2, 2007 I got it. I needed to move $query2 = mysql_query("SELECT * FROM iyb_events"); so that it was INSIDE the outer while loop (duh!). Thanks for your input revraz. It helps to have someone say the obvious things sometimes. Gets you asking the right questions (i.e. "Why is that line evaluating to FALSE after the first time through?"). -Matt Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/#findComment-383794 Share on other sites More sharing options...
kenrbnsn Posted November 2, 2007 Share Posted November 2, 2007 That is very inefficient. I would store the list of events first and then output it with each recipient. Something like this: <?php include('connection.php'); $subject = 'Inside Your Business Events ' . date('l jS F Y'); $headers = "From: doNOTreply@mydomain.net\r\n"; $query = mysql_query("SELECT * FROM member_data WHERE mailing_list = 'Yes'"); $query2 = mysql_query("SELECT * FROM iyb_events"); $event_array = array(); while($event = mysql_fetch_assoc($query2)) { $event_array[] = "Event: ".$event['event']; $event_array[] = "Description: ".$event['description']; $event_array[] = "Date: ".$event['date']; $event_array[] = "Start Time: ".$event['time']; $event_array[] = "Cost: ".$event['cost']; $events = implode("\n",$event_array); } $bdy = array(); while($email = mysql_fetch_assoc($query)) { $bdy[] = "Dear $email['name']"; $bdy[] = "See below for this week's list of exciting events!"; echo implode("\n",$bdy) . "\n\n" . $events; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/#findComment-383801 Share on other sites More sharing options...
MattEldridge Posted November 2, 2007 Author Share Posted November 2, 2007 Thanks for the tip Ken. Honestly I'm such a n00b that I don't really understand your code. I need to brush up on how arrays work. All I need at the moment is code that does what I need it to, but I'll get there. Thanks. -Matt Quote Link to comment https://forums.phpfreaks.com/topic/75825-help-with-loops/#findComment-383834 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.