Creating a shopping basket
Posted: Sat Oct 07, 2006 9:06 am
Most of my shopping site is complete now. I'm struggling on the shopping basket a bit though. I want to create an array ($product[$i]) where $i is equal to the product ID which is in a MySQL DB. To do this I've used:
So now I have an array and each element in the array is equal to a product in the DB.
Then I have a text box with a submit button next to each item, each entry has a hidden field and the code looks like this:
So I'm able to output what is entered in the text field and link it to what product the user wants to purchase. So now all I need is to create a session variable to carry these products across to each page the user views without loosing the data, so I have:
But I'm stuck here. My brain just can't seem to understand how it's going to work from here. Can somebody point me in the right direction please?
Stephen,
Code: Select all
$i = 1;
while ($results = mysql_fetch_array($query)) {
$product[$i] = 1;
// Other code to output the prodcuts in the DB //
$i++;
}Then I have a text box with a submit button next to each item, each entry has a hidden field and the code looks like this:
Code: Select all
<form method="post" action="books.php">
<input type="hidden" name="boxID" value="<?php echo $res['id']; ?>"><br><br>
<input type="submit" name="add" value="Add to cart">
Quantity <input type="text" name="quantity" value="0">Code: Select all
if (isset($_REQUEST['add'])) { # Add to cart
$quantity = $_REQUEST['quantity'];
$boxID = $_REQUEST['boxID'];
$_SESSION['brought'] = $product[$boxID] += 1;
echo $_SESSION['brought'];
}Stephen,