need simple shopping cart in php/Mysql

hello anybody,
any one help me to make a shopping cart script in php/mysql. i want shopping cart, add, remove item, total amount, then mail to ordered person. i do not need any payment,processing,like cridet card. in the back end database is mysql.thank in advance.

Comments

  • : hello anybody,
    : any one help me to make a shopping cart script in php/mysql. i want shopping cart, add, remove item, total amount, then mail to ordered person. i do not need any payment,processing,like cridet card. in the back end database is mysql.thank in advance.
    :


    very simple...

    just use sessions..

    [code]session_start();
    $cartlist = array();
    if(isset($HTTP_SESSION_VARS['cart'])){
    $cartlist = explode(';', $HTTP_SESSION_VARS['cart']);
    }
    else{
    $HTTP_SESSION_VARS['cart'] = '';
    }

    TO ADD STUFF:

    function addItem($itemnum){
    $global $cartlist;
    $found = 0;
    foreach($cartlist as $cartitem){
    if($cartitem == $itemnum){$found = 1;}
    }
    if(!$found){
    $cartlist[] = '$itemnumber';
    $HTTP_SESSION_VARS['cart'] = implode(';', $cartlist);
    return true;
    }
    return false;
    }

    TO REMOVE STUFF:

    function removeItem($itemnum){
    global $cartlist;
    $newcart = array();
    $found = 0;
    foreach($cartlist as $cartitem){
    if($cartitem != $itemnum){
    $newcart[] = $itemnum;
    }
    else{
    $found = 1;
    }
    }
    if($found){
    // If the item was found and removed, update the session and return true
    $HTTP_SESSION_VARS['cart'] = implode(';', $newcart);
    return true;
    }
    // Otherwise return false
    return false;
    }


    TO FIND THE TOTAL ITEMS: This ones pretty simple :)

    $totalitems = count($cartlist);


    [/code]

    *note: $_SESSIONS['cart'] does the exact same thing but not all installations are equipped or allow it (mine don't dag-nabbit) $HTTP_SESSION_VARS is your safest bet.




    This will only last while the person has his/her browser open tho.
    If you want the cart to last for a long time you will need to store cart details in a table

    cart -> cartid, userid, products

    in the products column i recommend just storing data as long string with each product number sepparated by a semi-colon or colon.


    Then just load the details for each page.. you do have user details stored in a session don't you? It's not the best idea to use normal cookies to store that kind of information. People can tamper with them as well as view their details (what if someone is away from they're computer when some unsuspecting individual decides to have a look at that person's cookies?)


    Storing the data in a table is kind of stupid tho unless you don't mind pruneing out old carts every couple of days. (So use it only if you need carts to last longer than one browser session)



    As for the email messageing system, the mail() function is what you need.

    [code]

    $itemDetails = array(); // You could also pull this from a database
    $itemDetails['name'][1] = 'Product Name 1';
    $itemDetails['name'][2] = 'Product Name 2';
    $itemDetails['name'][3] = 'Product Name 3';
    $itemDetails['name'][4] = 'Product Name 4';

    $itemDetails['cost'][1] = '12.35';
    $itemDetails['cost'][2] = '$53.34';
    $itemDetails['cost'][3] = '$14.53';
    $itemDetails['cost'][4] = '$73.22';

    $itemDetails['description'][1] = 'Description for item 1';
    $itemDetails['description'][2] = 'Description for item 2';
    $itemDetails['description'][3] = 'Description for item 3';
    $itemDetails['description'][4] = 'Description for item 4';


    function cartOrder(){
    global $cartlist, $itemDetails;
    $itemcount = count($cartlist);
    $str = array();
    $from = 'webmaster@{$_SERVER['SERVER_NAME']}'; // Fill your own in
    $to = 'useremail@mailserver.com';
    $company = 'My Company INC';
    $subject = 'Company Name - $itemcount items ordered';

    $count = 1;
    foreach($cartlist as $cartitem){
    $str[] = <<<eof
    Item $count: $itemDetails['name'][$cartitem]

    Item Number: $cartitem

    Item Cost: $itemDetails['cost'][$cartitem]

    Item Description: $itemDetails['description'][$cartitem]

    eof;
    $count++;
    }

    $str = implode('
    ', $str);

    $mail_top = <<<eof
    Thankyou for buying this stuff.. below is a list of the items you ordered.


    eof;
    $mail_bottom = <<<eof
    If you have any questions reply to this message.. blah blah
    eof;

    $message = <<<eof
    $mail_top
    $str

    $itemcount Total Items


    $mail_bottom
    eof;

    $headers = <<<eof
    "From: $from
    " .
    "Reply-To: webmaster@{$_SERVER['SERVER_NAME']}
    " .
    eof;

    // note: 'Reply-To' field is not necessary

    if(mail($to, $subject, $message, $headers)){
    return true;
    }
    return false;
    }[/code]

    mail() reference: http://us2.php.net/manual/en/function.mail.php

    I realise thats a little bit confusing but if you really need some reference it will probably help you a lot.

    Anywayz, it's not as difficult as you might expect

    Good luck, if you need any further help feel free to post again.
    [b][blue]Snoochie Boochies[/blue][/b]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion