Please, need help with a mail function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
bassically i am buiding a simple shopping cart type feature and am using
sessions to store the cart contents and then when teh user checks out and
submits the order it will email me the cart contents.. however right now im
having a problem with it sending the proper data.. Insead of inserting each
item in the cart in teh msg o fthe email it only inserts one item... Below
are the two main functions i am using to accomplish this...I have also tried
it with the return $title; within the foreach loop as well the only thing
that came close was when i had echo $title; within the foreach loop,
unfortunately all this did was upon submitting print the cart contents to
the screen and totally leave them out of the boday  of the email...  I hope
all that makes sense

function mail_order($name,$email,$address) {
        global $admin_email;
  $cart = mail_format_cart();

        $subject = "DVD ORDER FOR $name";

        $msg = "Order Details\n\n"
                        ."Name: $name\n"
                        ."Email: $email\n\n"

       mail("$admin_email", "$subject", "$msg", "From: $email \nReply-To:


function mail_format_cart() {

foreach ($_SESSION["cart"] as $key => $session_data) {
  list($ses_id) = $session_data;
  if(isset($ses_id)) {
  $sel_products = mysql_query("SELECT * FROM moviedb WHERE id=".$ses_id."");
  $item = mysql_fetch_array($sel_products);
  $title = $item['title'];

  return $title;

Re: Please, need help with a mail function


mail_format_cart() function is returning $title, which is basically
giving you only one item right? Well the way function is executed it
will return the last index of the array $_SESSION['cart']. WHY? Because
in your foreach() you are running query, fetching data and assigning
$item['title'] to $title. So what is basically happening here is as

+ Starts Loop Cycle 1
   + $title is assigned with first value found in $item['title'], e.g.
Item One
+ Starts Loop Cycle 2
   + $title is assigned AGAIN with the second value found in
$item['title'], e.g. Item Two
+ Start the Last Loop Cycle
   + $title is assigned with the LAST value found in $item['title'],

So returning $title from the function will give your mail ITEM LAST
only. If you write a code like:

$a = "My";
...some more code
$a = "Your";
...some more code
$a = "Our";

echo $a will give us Our. Not My nor Your.

How to get the proper list of items? You can simply make $title an
array to assign values to it. That is, rather than writing $title =
$item['title']; you write:

$title[] = $item['title'];

If you do the above then you will have to run another foreach loop in
mail_order() function. To avoid this you can do this instead, which I
think is very good. Don't even assign it to an array as mentioned
above. Try writing:

$title .= $item['title'] . "\n\n";

Hope this will help. Sorry for the long and big explanation. Thought it
will help.

Thanks and God Bless!!


Re: Please, need help with a mail function

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: Please, need help with a mail function

Why it shows All 4 messages in topic? I see only 3 messages but it says
4???? This one is 4th but dont know how many it will show after post.
Lets see!!

Site Timeline