Home » Shopping

What Is The Php Code To Create A Online Shopping Cart?

10 July 2009 887 views 2 Comments

Stuck on a college assignment, and need to get a php shopping cart, need to get one that fairly basic and not too flashy

2 Comments »

  • Angela Q said:

    There is not one script for all php shopping carts. If you search, you should find a lot of possibilities, but all of them will require customization. ∠°)http://www.shop-script.com/php-shopping-…

  • michael_ said:

    hi there luckily i used php script to create a shopping cart in the past so here is what you should do…. it’s fairly easy so bare with me.
    1. first you need to represent the cart as a string and all your products as integers which are the product ids, like a 1 dimensional array of product ids separated by commas (,).
    2. When you add an item it will look like that
    if ($cart) $cart.=”,”;//if not empty then add a comma
    $cart.=strval($id);//first will be 6 then 6,31 then 6,31,192 and so on
    3. later on when you want to seperate all the items you will need to use the explode() function.
    $items = explode(‘,’,$cart);
    explode() will take each id and put it in a new array called $items and you could print them out:
    foreach ($items as $item)
    {
    echo $item;
    }
    to remove an item you would need to create a new items array, go over all items and if it matches the removed item don’t copy it to the new array.
    to empty the cart just write
    $cart=”";
    Now you will need to store the cart string in a session variable (hope you know what it means, you should if they asked you to create a shopping cart…)
    session_start();
    $_SESSION['cart']=$cart;
    and on a different page just recall it by writing
    session_start();
    $cart=$_SESSION['cart'];
    If you want my complete code then here it is…
    $action=$_GET['action'];
    //you can send an action to the file by creating a link that links to “cart.php?action=ADD&id=123″;
    $cart=$_SESSION['cart'];
    $id=strval($_GET['id']);
    if ($action==”ADD”) {
    if ($cart) $cart.=”,”;
    $cart.=strval($id);
    $_SESSION['cart']=$cart;
    }
    if ($action==”REMOVE”) {
    $items = explode(‘,’,$cart);
    $TimesFound=0;
    $newcart = ”;
    $ItemCount = 0;
    //first count the number of times the specific item appear in cart, in order to remove only the last one so the order of the items won’t change
    foreach ($items as $item) {
    if ($item==$id) $itemCount++;
    }
    foreach ($items as $item) {
    if ($id!=$item) {
    if ($newcart) $newcart.=”,”;
    $newcart.=strval($item);
    }
    if ($id==$item){
    $TimesFound++;
    if ($TimesFound!=$itemCount)
    {
    if ($newcart) $newcart.=”,”;
    $newcart.=strval($item);
    }
    }
    }
    $cart=$newcart;
    $_SESSION['cart']=$cart;
    }
    if ($action==”EMPTY”) {
    $cart=”";
    $c=0;
    $_SESSION['cart']=”";
    }
    Hope this helps…
    let me know if you need more help.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

Books!
Ebooks!