...

View Full Version : How do I sort ot these "while" loops ?



jeddi
03-16-2009, 12:02 PM
What is the best way to write the while loops
for this:

I have a MySQL table of clients that I want to send a series of 5 emails
after they sign up.

The clients are in 2 groups and they'll get two different sets of emails.

So my table is like this:

cl_name | group | eml_cnt | email_add | other stuff ...

Dave | A | 0 | Dave@email_add.com | other stuf
Stev | A | 3 | Stev@email_add.com | other stuf
John | B | 0 | John@email_add.com | other stuf
Pete | A | 1 | Pete@email_add.com | other stuf
Jeffe | B | 2 | Jeffe@email_add.com | other stuf
Jacky| A | 4 | Jacky@email_add.com | other stuf

I will run the script every 2 days and it needs to
send:

messageA1.txt to group A who are at count 0
messageA2.txt to group A who are at count 1
messageA3.txt to group A who are at count 2
messageA4.txt to group A who are at count 3
messageA5.txt to group A who are at count 4

and

messageB1.txt to group B who are at count 0
messageB2.txt to group B who are at count 1
messageB3.txt to group B who are at count 2
messageB4.txt to group B who are at count 3
messageB5.txt to group B who are at count 4

Should I index the table ? if so with which key ?

As the message is sent the eml_cnt field needs to be
incremented by one.

I am a bit lost about how to do the logic for the
while or for loops :o

So if anyone can help me out, I'd appreciate it :)

student101
03-16-2009, 01:38 PM
You need two other fields...

cl_name
group
eml_cnt
email_add
other stuff
datesent
sent (with a Y or N)Now run a query to check if anyone was sent an email on the specified date, if not send the email and set the datesent and set the sent with Y...
Logic for me is backwards, I always do the last thing first, I can only suggest the above.


$boundary = "------------";
$multipartheader = "Content-type: multipart/alternative; ";
$multipartheader .= "boundary=\"".$boundary."\"";
$textheader = "Content-Type: text/plain; charset=us-ascii\n";
$textheader .= "Content-Transfer-Encoding: 7bit";
$htmlheader = "Content-Type: text/html; charset=us-ascii\n";
$htmlheader .= "Content-Transfer-Encoding: 7bit";
$endmessage = "--\n\n-- End --";
$to = "name@example.com";
$subject = "Subject";

$textmessage = join("",file("filetosend")); //add your text file here

$from = "From: Nice Name <name@example.com>";
$replyto = "reply-to: name@example.com";
$header = $from; // set the from field in the header
$header .= "\n"; // add a line feed
$header .= $replyto; // add the reply-to header to the header
$header .= "\n"; // add a line feed
$header .= "MIME-version: 1.0\n"; //add the mime-version header
$header .= $multipartheader;
$header .= "\n";
$message = $boundary."\n".$textheader."\n\n";
$message .= $textmessage."\n\n";
$message .= "--".$boundary."\n".$htmlheader."\n\n";
$message .= $htmlmessage."\n\n";
$message .= "--".$boundary.$endmessage;
mail($to, $subject, $message, $header); // send the e-mailCheers

jeddi
03-17-2009, 05:43 PM
Thanks for this,
I was not too worried about sending the email out
(although I'll bear in mind your suggestions )

I was more concerned about ordering the database table
correctly and stepping through it to send the correct email to the correct
group. It's the looping logic that I need some help with :confused:

Thanks again.

student101
03-17-2009, 06:04 PM
I don't think I understand...


I sign up
I get 5 emails
The date gets set
eml_cnt increments by 1
Do this until eml_cnt = 5 now wait 2 days and send 5 emails again.

What are the 2 days for?
Why 5 emails?

Cheers

jeddi
03-18-2009, 07:55 AM
Thanks for your question,
maybe I wasn't clear enough.

You sign up as either group A or B (depending on which one you want)

If you are in group A when the script is run you
get sent messageA1.txt and your eml_cnt increments by 1

Two days later you get messageA2.txt
and your eml_cnt increments by 1 etc

Meanwhile your mate signs up for group B

Two days later you get messageA3.txt
Your friend gets messageB1.txt

Two days later you get messageA4.txt
Your friend gets messageB2.txt

Two days later you get messageA5.txt
Your friend gets messageB3.txt

Two days later your friend gets messageB4.txt

Two days later your friend gets messageB5.txt


Every two days the same script is run so the script needs to
increment the database table and send the correct email to the correct group.

I don't think we need to worry about the date - just send
the emails acoording to what the client has already been sent.

If the script is run a day late - it should not make any difference
to what is sent out.

Hope that helps to make it clearer what I am trying to do. :)

student101
03-18-2009, 08:17 AM
In that case I would go with switch statement (http://www.w3schools.com/PHP/php_switch.asp)

switch (expression){
case 0:
// code to be executed if expression = 0;
break;
case 1:
// code to be executed if expression = 1;
break;
default:
// code to be executed
// if expression is different from both 0 and 1;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum