Hey all,
I'm not sure where I'm going wrong here - here's the code:
<?php
function session_init()
{
if (!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
}
function productlist ()
{
$query='SELECT pr.prId AS prId, pr.name AS name, pr.price AS price, pr.shortdesc AS shortdesc, g.image AS image, c.cId AS cId, c.desc AS colourname, c.colour AS colour FROM products AS pr, colour AS c, productstocolour AS prtoc LEFT JOIN gallery AS g ON g.gId = prtoc.FK_gId WHERE pr.prId = prtoc.FK_prId AND prtoc.FK_cId = c.cId';
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
echo '<div class="productlist">';
echo '<div class="prodimg">';
if ($row['image'] != '')
{
$image = 'gallery/' . $row['image'];
}
else
{
$image = 'images/noimg.gif';
}
echo '<img src="' . $image . '" alt="' . $row['image'] . '">';
echo '</div>';
echo '<div class="prodtext">';
echo '<h4>' . $row['name'] . '</h4><h5><div style="color:' . $row['colour'] . ';">' . $row['colourname'] . '</div></h5><p>' . $row['shortdesc'] . '</p>';
echo 'Price: £' . $row['price'] . ' each <br>Size <select name="a_size[' . $row['prId'] . ':' . $row['cId'] . ']">';
$sizequery = 'SELECT s.sId AS sId, s.size AS size FROM sizes AS s, productstosizes AS prtos WHERE prtos.FK_prId = ' . $row['prId'] . ' AND s.sId = prtos.FK_sId';
$sizeres = mysql_query($sizequery);
while ($srow = mysql_fetch_assoc($sizeres))
{
echo '<option value="' . $srow['sId'] . '">' . $srow['size'] . '</option>';
}
echo '</select> | <label>Quantity: <input type="text" name="a_qty[' . $row['prId'] . ':' . $row['cId'] . ']" id="quantity"size="3" value="0" /></label>';
echo '</div>';
echo '</div>';
}
}
function form_check()
{
// check to see if the form has been submitted
// and which submit button was clicked
// if this is an add operation
// add to already existing quantities in shopping cart
if ($_POST['add'])
{
foreach ($_POST['a_qty'] as $k => $v)
{
// if the value is 0 or negative
// don't bother changing the cart
if ($v > 0)
{
$k .= ':' . $_POST['a_size'][$k];
$_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
}
}
}
// if this is an update operation
// replace quantities in shopping cart with values entered
else if ($_POST['update'])
{
foreach ($_POST['u_qty'] as $k => $v)
{
// if the value is empty, 0 or negative
// don't bother changing the cart
if ($v != "" && $v >= 0)
{
// $_SESSION['cart'][$k] = $v;
$k .= ':' . $_POST['u_size'][$k];
$_SESSION['cart'][$k] = $_SESSION['cart'][$k] + $v;
}
}
}
// if this is a clear operation
// reset the session and the cart
// destroy all session data
else if ($_POST['clear'])
{
$_SESSION = array();
session_destroy();
}
}
function display_basket()
{
// initialize a variable to hold total cost
$total = 0;
// check the shopping cart
// if it contains values
// look up the SKUs in the $CATALOG array
// get the cost and calculate subtotals and totals
if (is_array($_SESSION['cart']))
{
foreach ($_SESSION['cart'] as $k => $v)
{
if ($v > 0)
{
// echo $k . ' - ' . $v . '<br>';
// echo $_POST['a_size'][$k];
// $k .= ':' . $_POST['a_size'][$k];
// echo '<br>' . $k;
$id = explode(":", $k);
$query='SELECT pr.prId AS prId, pr.name AS name, pr.price AS price, g.image AS image, c.cId AS cId, c.desc AS colourname, c.colour AS colour FROM products AS pr, colour AS c, productstocolour AS prtoc LEFT JOIN gallery AS g ON g.gId = prtoc.FK_gId WHERE pr.prId = prtoc.FK_prId AND prtoc.FK_cId = c.cId AND pr.prId = ' . $id[0] . ' AND prtoc.FK_cId = ' . $id[1];
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
echo '<div class="productlist">';
echo '<div class="prodimg">';
if ($row['image'] != '')
{
$image = 'gallery/' . $row['image'];
}
else
{
$image = 'images/noimg.gif';
}
echo '<img src="' . $image . '" alt="' . $row['image'] . '">';
echo '</div>';
echo '<div class="prodtext">';
echo '<h4>' . $row['name'] . '</h4><h5><div style="color:' . $row['colour'] . ';">' . $row['colourname'] . '</div></h5>';
echo 'Price: £' . $row['price'] . ' each <br>Size <select name="u_size[' . $row['prId'] . ':' . $row['cId'] . ']">';
$sizequery = 'SELECT s.sId AS sId, s.size AS size FROM sizes AS s, productstosizes AS prtos WHERE prtos.FK_prId = ' . $row['prId'] . ' AND s.sId = prtos.FK_sId';
$sizeres = mysql_query($sizequery);
while ($srow = mysql_fetch_assoc($sizeres))
{
echo '<option value="' . $srow['sId'] . '"';
if ($srow['sId'] == $id[2])
{
echo ' selected="selected"';
}
echo '>' . $srow['size'] . '</option>';
}
echo '</select> | <label>Quantity: <input type="text" name="u_qty[' . $row['prId'] . ':' . $row['cId'] . ']" id="quantity" size="3" value="' . $v . '" /></label>';
echo '</div>';
echo '</div>';
$subtotal = $row['price'] * $v;
$total += $subtotal;
}
}
}
}
return $total;
}
?>
and the html:
<html>
<head>
<link href="window.css" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />
<?php
require ('includes/widgets.php');
require ('includes/database.php');
db_connect();
session_init();
?>
</head>
<body>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="wrapper">
<div class="content">
<div class="header">
<div class="headerleft">
<img src="images/JSCwebmauve.jpg" height="70" /><img src="images/jsclogo.jpg" height="50" />
</div>
<div class="headerright">
<img src="images/MistressCollection.jpg" height="50" /><img src="images/JHS-.jpg" height="70"/>
</div>
</div>
<div class="window" style="float:left;">
<div class="top">
<h1>Products</h1>
</div>
<div class="middle">
<div class="scrollbox">
<?php
form_check();
productlist();
?>
</div>
</div>
<div class="bottom">
</div>
</div>
<div class="submit">
<input type="image" src="images/add.png" value="submit"
alt="submit" name="add">
</div>
<div class="window" style="float:right;">
<div class="top">
<h1>Basket</h1>
</div>
<div class="middle">
<div class="scrollbox">
<?php
$total = display_basket();
?>
</div>
</div>
<div class="bottom">
<div style="text-align:center; margin-right:20px; margin-left:10px; padding-top:1px; width:365px; border-top:1px dotted;">Total: £<?php echo sprintf("%0.2f", $total); ?> | <input name="clear"
style="margin: 0;
padding: 0;
border: 0;
background-color: transparent;
font-size:10px;
"
type="submit" value="Clear Basket" class="submit-button" /> | <input name="update"
style="margin: 0;
padding: 0;
border: 0;
background-color: transparent;
font-size:10px;
"
type="submit" value="Update Total" class="submit-button" /> | Checkout
</div>
</div>
</div>
</div>
<div class="bottominfo">
For wedding dresses and corsets (made to order) telephone 07887 482326 or email <a href="mailto:juliahsmith@mac.com">juliahsmith@mac.com </a>
</div>
</div>
</form>
</body>
</html>
- What's supposed to happen is that when you enter quantities into the catalog and then submit (name ="add") then it adds items to the session['cart'] and displays them in a basket. When you do the same process again with another item it should add the item to the basket in the same way, so that now there are two items. However, as it stands, each time you "add" the basket only displays the most recently added item, and I'm pretty sure that means that the $_SESSION['cart'] array has been wiped and started again.
Any ideas how to make this work?
Cheers
Edd
