Please, need help with a mail function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris H

    #1

    Please, need help with a mail function

    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($nam e,$email,$addre ss) {
    global $admin_email;
    $cart = mail_format_car t();

    $subject = "DVD ORDER FOR $name";

    $msg = "Order Details\n\n"
    ."$cart\n\n"
    ."-----------------------------------------------------------------\n"
    ."Name: $name\n"
    ."Email: $email\n\n"
    ."Address:\n "
    ."$address\n "
    ."\n\n";


    mail("$admin_em ail", "$subject", "$msg", "From: $email \nReply-To:
    $email");

    }

    function mail_format_car t() {

    foreach ($_SESSION["cart"] as $key => $session_data) {
    list($ses_id) = $session_data;
    if(isset($ses_i d)) {
    $sel_products = mysql_query("SE LECT * FROM moviedb WHERE id=".$ses_id."" );
    $item = mysql_fetch_arr ay($sel_product s);
    $title = $item['title'];

    }
    }
    return $title;
    }
    =============== =============== =========



  • Ehsan

    #2
    Re: Please, need help with a mail function

    Hi,

    mail_format_car t() 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
    follows:

    + 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'],
    e.g. ITEM LAST

    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!!

    Ehsan


    Comment

    • Jerry Stuckle

      #3
      Re: Please, need help with a mail function

      Chris H wrote:[color=blue]
      > 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($nam e,$email,$addre ss) {
      > global $admin_email;
      > $cart = mail_format_car t();
      >
      > $subject = "DVD ORDER FOR $name";
      >
      > $msg = "Order Details\n\n"
      > ."$cart\n\n"
      > ."-----------------------------------------------------------------\n"
      > ."Name: $name\n"
      > ."Email: $email\n\n"
      > ."Address:\n "
      > ."$address\n "
      > ."\n\n";
      >
      >
      > mail("$admin_em ail", "$subject", "$msg", "From: $email \nReply-To:
      > $email");
      >
      > }
      >
      > function mail_format_car t() {
      >
      > foreach ($_SESSION["cart"] as $key => $session_data) {
      > list($ses_id) = $session_data;
      > if(isset($ses_i d)) {
      > $sel_products = mysql_query("SE LECT * FROM moviedb WHERE id=".$ses_id."" );
      > $item = mysql_fetch_arr ay($sel_product s);
      > $title = $item['title'];
      >
      > }
      > }
      > return $title;
      > }
      > =============== =============== =========
      >
      >
      >[/color]

      Since I have no idea what's in your $_SESSION variable, I have no idea what
      might be wrong with your code.


      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      • Ehsan

        #4
        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!!

        Comment

        Working...