Page 1 of 1
Checkout section of shopping site
Posted: Sat Oct 07, 2006 12:17 pm
by impulse()
I know I've been whining about this shopping basket all day but it feels like my life ambition to get it working at the moment
I've gotten as far as being able allow the user to select how many they want to buy of a particular item. They can from there go to a checkout where it shows a table of the items names from the DB and then next to it how many they've brought and then next to that, the total amount in currency.
There are 4 products on the site at the moment
The problem is that if the user buys 3 * item number 1, it calculates fine but also calculates 3 * item number 2, 3 & 4 aswell. This not being strange enough, if they buy any of item 2 it doesn't do any calculation at all for any items. And then for item number 3, it does the same problem as item number 1. And item number 4 also does no calculation, like item number 4.
I know that sounds confusing so you may want to take a look yourself to understand.
http://stesbox.co.uk/ws
For some strange reason you have to login to be able to purchase, but I'll debug that at a later date.
The login details are
Username: admin
Password: password
Regards,[/url]
Posted: Sat Oct 07, 2006 12:26 pm
by impulse()
You'll be wanting to see the code wont you?
This is the checkout.php script
Code: Select all
mysql_connect("x", "x", "x");
mysql_select_db("shop");
session_start();
$query = mysql_query("SELECT * FROM products WHERE pType='books'");
$numRows = mysql_numrows($query);
if (isset($_REQUEST['empty'])) {
for ($i = 1; $i <= $numRows; $i++) { # Emptys shopping basket
$_SESSION['quantity'][$i] = 0;
}
}
?>
<table border>
<tr>
<td>
<b>Items in basket:</b>
</td>
</tr>
<tr>
</tr>
<? for ($i = 0; $i <= $numRows; $i++) {
if (isset($_SESSION['quantity'][$i])) {
?>
<? while ($results = mysql_fetch_array($query)) {
?>
<tr>
<td> <?php
echo $results['pName'];
$value[$v] = $results['pPrice']; # Puts the price from MySQL DB into a variable
echo " <b>", $value[$v], "</b>";
$v++; ?>
</td>
<td>
<?php
echo $_SESSION['quantity'][$j]; $j++; # Prints the quantity of each item
?>
</td>
<td> <?php
for ($z = 1; $z <= $numRows; $z++) {
$total[$z] = $results['pPrice'] * $_SESSION['quantity'][$z]; # Calculates total
echo "\$", $total[$z];
$z++; } ?>
</td>
</td>
</tr>
<? }
}
}
?>
</table>
<form method="post" action="checkout.php">
<input type="submit" name="empty" value="Empty Basket">
</form>
<form method="post" action="http://stesbox.co.uk/ws">
<input type="submit" value="Return to shop">
</form>
<?php
?>
This is the item display script
Code: Select all
mysql_connect("x", "x", "x");
mysql_select_db("shop");
if (isset($_REQUEST['logout'])) {
session_start();
$_SESSION['loggedIn'] = 0;
}
if (isset($_REQUEST['emptyBasket'])) { # Empty basket.
session_start();
$q = mysql_query("SELECT * FROM products WHERE pType='books'");
$numRows = mysql_numrows($q);
for ($i = 0; $i < $numRows; $i++) {
$_SESSION['quantity'][$i] = 0;
}
}
if (isset($_REQUEST['add'])) { # Add to cart
session_start();
$quantity = $_REQUEST['quantity']; # The quantity the user entered in the text box
$boxID = $_REQUEST['boxID']; # The box the user typed in (Hidden box)
$_SESSION['quantity'][$boxID] += $quantity; # 2D array. First element is the quantity, the second element is what box
$q = mysql_query("SELECT * FROM products WHERE pType='books'"); # Just counting rows
$numRows = mysql_numrows($q);
for ($i = 1; $i < $numRows + 1; $i++) {
echo "Item <b>$i</b> has<b> ", $_SESSION['quantity'][$i], "</b>items<br>"; #Output values of items
}
}
if (isset($_REQUEST['submitImg'])) {
$boxID = $_REQUEST["idImg"];
$imgSRC = $_REQUEST["imgSrc"];
switch ($_REQUEST['submitImg']) { # Alter image
case "Change Image":
$query = mysql_query("UPDATE products SET pImg='$imgSRC' WHERE pID='$boxID'");
echo "You have updated the image location. Please refresh to view changes<br>";
break;
default:
echo "How did this happen?";
break;
}
}
if (isset($_REQUEST['submitDesc'])) { # Alter description
$boxID = $_REQUEST["idDesc"];
$desc = $_REQUEST["desc"];
switch ($_REQUEST['submitDesc']) {
case "Change Description":
echo "You have updated the book description. Please refresh to view changes";
$query = mysql_query("UPDATE products SET pDesc='$desc' WHERE pID='$boxID'");
break;
default:
echo "How did this happen?";
break;
}
}
if (isset($_REQUEST['submitPrice'])) { # Alter price
$boxID = $_REQUEST["idPrice"];
$price = $_REQUEST["price"];
switch ($_REQUEST['submitPrice']) {
case "Change Price":
echo "You have updated the price description. Please refresh to view changes";
$query = mysql_query("UPDATE products SET pPrice='$price' WHERE pID='$boxID'");
break;
default:
echo "How did this happen?";
break;
}
}
$queryBooks = mysql_query("SELECT * FROM products WHERE pType='books' ORDER BY pID");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Stes Shop </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css"
href="http://stesbox.co.uk/ws/2.css" />
<link rel="stylesheet" type="text/css"
href="http://stesbox.co.uk/ws/3.css" />
</head>
<body>
<div id="outer">
<div id="hdr" align="center">
<font size="7"><b>Stes Shop</b><font size="3">
<div id="bar">X<span style="padding:5px;font-size:11px;"> </span></div>
<div id="bodyblock" align="right">
<div id="l-col" align="center">
<h4 align="center">Menu</h4>
<a href="http://stesbox.co.uk/ws/products/books.php"> Books </a><br />
<a href="http://stesbox.co.uk/ws/products/furniture.php"> Furniture </a><br />
<a href="http://stesbox.co.uk/ws/products/electronics.php"> Electronics </a><br />
<a href="http://stesbox.co.uk/ws/products/clothes.php"> Clothes </a><br />
<br><br>
<form method="post" action="books.php">
<input type="submit" name="emptyBasket" value="Empty Basket">
</form>
<form method="post" action="checkoutBooks.php">
<input type="submit" name="checkout" value="Checkout">
</form>
<?php session_start();
?>
<form method="post" action="login.php">
<center><br><br><font size="4"><b>Login</b><font size="3">
<br><br><b>UserName:<br></b>
<center><input type="text" name="username">
<b>Password:<br></b>
<center><input type="password" name="password">
<br><br><center><input type="submit" value="Login">
</form>
<form method="post" action="books.php">
<input type="submit" name="logout" value="Logout">
</form>
</div>
<div id="cont">
<h3 align="center">
<font size="5"> Welcome to my test shop
</h3><font size="3">
<?php
$i = 1;
while ($res = mysql_fetch_array($queryBooks)) {
$product[$i] ; #This is used to determine what the user has added to their basket.
?><table border>
<form>
<tr>
<center>
<font size="4"><b><?php echo $res['pName']; ?></b></tr>
<tr>
<td>
<center><img src="<?php echo $res['pImg'];?>" width="<? echo $res['imgWidth'];?>" height="<?php echo $res['imgHeight']; ?>"
<br><?php if ($_SESSION['loggedIn'] == 1) {
?> <table border="1" bgcolor="grey">
<tr>
<td>
<form method="post" action="books.php">
<input type="text" name="imgSrc"><br>
<input type="hidden" name="idImg" value="<?php echo $res['pID']; ?>">
<center><input type="submit" name="submitImg" value="Change Image"><br><br><br>
</form>
</td>
</tr>
</table><? } ?>
</td>
<td></b><i>
<?php echo $res['pDesc']; ?> </i>
<?php if ($_SESSION['loggedIn'] == 1) { ?>
<table border="1" bgcolor="grey">
<tr>
<td>
<form method="post" action="books.php">
<input type="text" name="desc"><br>
<input type="hidden" name="idDesc" value="<?php echo $res['pID']; ?>">
<center><input type="submit" name="submitDesc" value="Change Description"><br><br><br>
</form>
</td>
</tr>
</table><? } ?>
</td>
</tr> <br>
<tr>
<td>
<br><br>Price: <b>$<?php echo $res['pPrice']; ?> </b>
<?php if ($_SESSION['loggedIn'] == 1) { ?>
<table border="1" bgcolor="grey">
<tr>
<td>
<form method="post" action="books.php">
<input type="text" name="price"><br>
<input type="hidden" name="idPrice" value="<?php echo $res['pID']; ?>">
<center><input type="submit" name="submitPrice" value="Change Price"><br><br><brb>
</form>
</td>
</tr>
</table><? } ?>
</td>
<td>
<form method="post" action="books.php">
<input type="hidden" name="boxID" value="<?php echo $res['pID']; ?>"><br><br>
<input type="submit" name="add" value="Add to cart">
Quantity <input type="text" name="quantity" value="0">
</td>
</form>
</table><br><br><br><br>
<center><img src="http://stesbox.co.uk/ws/products/booksImg/bar.jpg" width="350"><br><br>
<?php
$i++;
}
print_r($product);
?>
<div align="center" style="color: red;"></div>
<p>
<ul style="margin: 2px 20px; padding: 2px 20px;">
<br />
</div>
</div>
<div id="ftr" align="center">Copyright (c) Ste</div>
</div>
</body>
</html>
?>
<table border>
<tr>
<td>
<b>Items in basket:</b>
</td>
</tr>
<tr>
</tr>
<? for ($i = 0; $i <= $numRows; $i++) {
if (isset($_SESSION['quantity'][$i])) {
?>
<? while ($results = mysql_fetch_array($query)) {
?>
<tr>
<td> <?php
echo $results['pName'];
$value[$v] = $results['pPrice']; # Puts the price from MySQL DB into a variable
echo " <b>", $value[$v], "</b>";
$v++; ?>
</td>
<td>
<?php
echo $_SESSION['quantity'][$j]; $j++; # Prints the quantity of each item
?>
</td>
<td> <?php
for ($z = 1; $z <= $numRows; $z++) {
$total[$z] = $results['pPrice'] * $_SESSION['quantity'][$z]; # Calculates total
echo "\$", $total[$z];
$z++; } ?>
</td>
</td>
</tr>
<? }
}
}
?>
</table>
<form method="post" action="checkout.php">
<input type="submit" name="empty" value="Empty Basket">
</form>
<form method="post" action="http://stesbox.co.uk/ws">
<input type="submit" value="Return to shop">
</form>
<?php
?>