Jump to content

Loop through a Script


airpirate007

Recommended Posts

I am trying to come up with a script that sends my clients their tracking number when they order from me. I upload their info into a MySQL DB and then I want to run a script that sends an email to the client with their tracking and order information.  Then I want it to update that row to show that the email has been sent.

 

I came up with a script, but it only sends an email to, and updates, the last row in the DB. So I need it to loop somehow through the script for every row in the DB that hasn't already been updated.

 

Any help would be appreciated.

 

<?php
include_once('config.php');

$send_db = 'db';
$send_table= 'table';

//query to pull names and email addresses from the db
$result = mysql_query("SELECT distinct(order_num),email,name,tracking,id,date FROM $send_db.$send_table where email_sent is NULL") or die ('Error: '.mysql_error());

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to send, so I am exiting";
    exit;
}

while ($row = mysql_fetch_array($result)) { 
    $email_id = $row['id'];
        $order_num = $row['order_num'];
        $name = $row['name'];
        $email = $row['email'];
	$date = $row['date'];
        $tracking = $row['tracking'];
}


$to = "$email";
$headers = "From: donotreply@website.com\r\n";
$headers .= "Reply-To: donotreply@website.com\r\n";
$subject = "Your Order Number $order_num Has Shipped";
$body = "\n\n
---------------------------------------------\n
DO NOT REPLY TO THIS MESSAGE \n
---------------------------------------------\n
\n
$name\n
\n
Thank you for Ordering our product!\n
\n
Your Order Number $order_num has shipped on $date and should arrive to you shortly.\n
Your Order Shipped via the USPS First Class Mail. The tracking number for your package is $tracking \n
\n
For Customer Service please call phone number. For Returns, please contact Customer Service at the above phone number for Return Authorization. \n
\n
Thank you! \n
Company \n
\n
\n
\n
\n
";
mail($to, $subject, $body, $headers);
  
  //update email_sent to Y
  $result1 = mysql_query("UPDATE $send_db.$send_table set email_sent = 'Y' where order_num = '$order_num' and email = '$email';");

if (!$result1) {
    echo "Could not successfully run query against the DB: " . mysql_error();
    exit;
}

  ?>

 

Link to comment
Share on other sites

Figured it out. I had a } in the wrong place, it needed to be on the end instead of after the while statement

 

<?php
include_once('config.php');

$send_db = 'db';
$send_table= 'table';

//query to pull names and email addresses from the db
$result = mysql_query("SELECT distinct(order_num),email,name,tracking,id,date FROM $send_db.$send_table where email_sent is NULL") or die ('Error: '.mysql_error());

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to send, so I am exiting";
    exit;
}

while ($row = mysql_fetch_array($result)) { 
    $email_id = $row['id'];
        $order_num = $row['order_num'];
        $name = $row['name'];
        $email = $row['email'];
	$date = $row['date'];
        $tracking = $row['tracking'];



$to = "$email";
$headers = "From: donotreply@website.com\r\n";
$headers .= "Reply-To: donotreply@website.com\r\n";
$subject = "Your Order Number $order_num Has Shipped";
$body = "\n\n
---------------------------------------------\n
DO NOT REPLY TO THIS MESSAGE \n
---------------------------------------------\n
\n
$name\n
\n
Thank you for Ordering our product!\n
\n
Your Order Number $order_num has shipped on $date and should arrive to you shortly.\n
Your Order Shipped via the USPS First Class Mail. The tracking number for your package is $tracking \n
\n
For Customer Service please call phone number. For Returns, please contact Customer Service at the above phone number for Return Authorization. \n
\n
Thank you! \n
Company \n
\n
\n
\n
\n
";
mail($to, $subject, $body, $headers);
  
  //update email_sent to Y
  $result1 = mysql_query("UPDATE $send_db.$send_table set email_sent = 'Y' where order_num = '$order_num' and email = '$email';");

if (!$result1) {
    echo "Could not successfully run query against the DB: " . mysql_error();
    exit;
}
}
  ?>

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.