What Is The Php Code To Create A Online Shopping Cart?
10 July 2009
596 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
Stuck on a college assignment, and need to get a php shopping cart, need to get one that fairly basic and not too flashy
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-…
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!
Popular Posts
- Online Shopping For A Skateboard And I Came Alon A Problem? - 2,612 views
- What Should I Look For When Shopping For A Plane? - 1,657 views
- Can I Use Visa Debit Card For Online Shopping? What Is The Difference From Visa Credit Card? - 1,133 views
- How Much Should You Charge To Do Someones Shopping And Run There Errands? - 927 views
- How Come H&m Doesnt Have Online Shopping In English? - 767 views
- What Is A Store Related To Forever 21? And What Are Some Tips For Shopping At Forever21? - 767 views
- Can You Name A Few Online Shopping Sites That Ship To Canada? - 655 views
- What Is The Php Code To Create A Online Shopping Cart? - 596 views
- When Should I Start Shopping For My Baby? - 453 views
- How Can I Tell If My Coach Bag Is A Fake? - 442 views
By blogdevRecent Posts
Tags
Categories
Recommended Tools
Ebooks!