You are here: Re: Shopping Cart Program « All PHP « IT news, forums, messages
Re: Shopping Cart Program

Posted by Geoff Berrow on 09/25/05 11:55

I noticed that Message-ID: <XJpZe.269622$HI.141531@edtnps84> from
Christa Lee contained the following:

>If someone can tell me where I'm going wrong or if someone has an example
>shopping cart for a clothing web site, please HELP ME!

The only thing you really need to store is the quantity and the product
id Everything else can come from the database. I've just done one
(literally, last week ) and I stored the price as well so that I could
have a running total. From an individual item page I have an add to
cart button and a text box to choose the quantity. The product id comes
from the URL. So when the form is submitted:

session_start();
if(isset($_POST['qty'])){
$_SESSION['quantity'][$_GET['product_id']]=$_POST['qty'];
$_SESSION['price'][$_GET['product_id']=$_POST['price'];
}

Then there is a link to a cart page which shows the contents of the cart
I need to create a query that will fetch back the relevant rows


foreach($_SESSION['quantity'] as $key=>$value)
{
$cartitems[]="product_id = $key";
}
$q=implode(" or ",$cartitems);
$sql="SELECT * FROM products where $q";
....


I then loop through the results ans show the products. Each one has a
form with a text box showing the quantity, and two buttons, change and
delete. At the top of the file I have:

if(isset($_POST['change'])){
$_SESSION['quantity'][$_POST[$prodkey]]=$_POST['qty'];

}
if(isset($_POST['delete'])||$_POST['qty']<1){
unset($_SESSION['quantity'][$_POST[$prodkey]]);
}

Then you just need a bit of code to do the calculations
$qty=0;
$total_cost=0;
foreach($_SESSION['quantity'] as $pid=>$no){
$qty=$qty+$no;
$total_cost=$total_cost+($no*$_SESSION['price'][$pid]);
}

Obviously there is a bit more code than this, to check there is anything
in the cart for instance, but there you have the basics.

This site is a work in progress, but you can check it out here
www.playhifi.com


--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация