Jump to content

help with loops?


MattEldridge

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.