[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

PHP Looped Function Will Not Send Multiple Emails

I am working on an osCommerce site which offers a few products and a variety of music classes. Each product and class is processed in the usual way, but an additional process is added which is supposed to send a separate email for each item or class purchased. The emails contain details about the product or the class. The function, doemail(), is attached. The problem is that when a user orders multiple items, only the email for the first item ordered is sent. The other items appear to be ignored.

The function first searches the database for the items contained in the order. It then enters a while loop. Inside the loop, a switch statement assigns values for various parameters associated with the item referenced in the row being examined. Next, a series of conditional routines, compose the message to be sent in the email. Finally, the email is supposed to be sent and the loop begins again with the next row.

As noted, the function works well for the first row, but fails to function after that. No error messages are generated on-screen.
customfunctions.php
0
gpinzino
Asked:
gpinzino
  • 2
2 Solutions
 
gr8gonzoConsultantCommented:
This is most likely because your second database query (the one inside the WHILE loop) is overwriting the $result variable, which contains the outer loop.

Try changing:
            $sql = "SELECT customers_email_address FROM orders WHERE orders_id='$order_id'";
            $result = mysql_query ( $sql, $$link ) or die ( "Oops, I can't look up the email address!");
            $to = mysql_result ( $result, 0 );

to:
            $sql3 = "SELECT customers_email_address FROM orders WHERE orders_id='$order_id'";
            $result3 = mysql_query ( $sql3, $$link ) or die ( "Oops, I can't look up the email address!");
            $to = mysql_result ( $result3, 0 );
0
 
Julian HansenCommented:
Further to what gr8gonzo said.

I might be missing something here but why do you keep looking up the clients email address in the loop?

It makes more sense for you to get the email address outside the main loop - save what you need and then continue with your loop. Otherwise as gr8gonzo said you are going to be wiping the $result value and performing unnecessary lookups on what should be a loop invariant value.
0
 
gpinzinoAuthor Commented:
You guys are good. I did not write this code, and I was going up a blind alley trying to debug it. I have now made the SQL change and moved it out of the loop and it works great now. Thank you both for your help.
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now