Forum Moderators: coopster

Message Too Old, No Replies

PHP & SQL Shopping cart

Problems with SQL MS ACCESS shopping cart

         

dc_duo

2:23 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



I am currently working on a site for my friend basically a revamp job. However I am struggling with a PHP shopping cart. I have searched the internet for hours on end trying to find a soultion so I am hoping you can help. If this falls outside of your forum charter then I do apologise. I fear I may be a little out of my depth here but here goes. This is what my script looks like to add to the cart.

<? ob_start();?>
<? require_once 'head.php'?>

<body>

<h1 class="hide-me">Adding to shopping cart</h1>

<div id="page-container">
<div id="site-header">

</div>

<div id="pagecontentcontainer" class="page-links">

<? require_once 'nav.php'?>

<div id="pagelinks">
<h3>Adding To Your Cart</h3>
<?

// $pid = $_GET['pid'];

if (is_numeric ($_GET['pid'])) {

// Check if shopping cart contains any of these items already

if (isset ($_SESSION['cart'][$pid])) {
$qty = $_SESSION['cart'][$pid] + 1;
} else {
$qty = 1;
}

// Add to the session cart variable

$_SESSION['cart'][$pid] = $qty;

// Display Message

echo '<p>The Item has been added to your cart</p>';

} else { // No id set

echo'<p>NUM NUTS</p>';

// header ("location: index.php");

}

?>
</div>
</div>
<? require_once 'foot.php'?>
</div>
</body>
</html>
<? ob_end_flush();?>

And this is the view cart script:-

<?

// Check the form has been submitted to update cart
if (isset ($_POST['submit'])) {
foreach ($_POST['qty'] as $key => $value) {
if (($value == 0) AND (is_numeric ($value))) {
unset ($_SESSION['cart'][$key]);
} elseif (
is_numeric ($value) AND ($value > 0) ) {
$_SESSION['cart'][$key] = $value;
}
}
}

// Check if shopping cart is empty
$empty = TRUE;
if (isset ($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $key => $value) {
if (isset($value)) {
$empty = FALSE;
}
}

}

// Display the cart if it is not empty

if (!$empty) {

require ('libs/connect.php');

// Retrive all Info for the products in the cart.

if (!$conn)
{
exit("Connection Failed: " . $conn);
}
$sql='SELECT * FROM catalouge WHERE id_no IN (';
foreach ($_SESSION['cart'] as $key => $value) {
$query .= $key . ',';
}
$query = substr ($query, 0, -1) . ')';

$rs=odbc_exec($conn,$sql);
if (!$rs)
{
echo "Query failed " .odbc_error();
}

while (odbc_fetch_array($rs))
{
// Define each result

$id = odbc_result($rs,'id_no');
$pn = odbc_result($rs,'product_name');
$po = odbc_result($rs,'product_no');
$pr = odbc_result($rs,'product_ref');
$pd = odbc_result($rs,'product_desc');
$pp = odbc_result($rs,'price');
$al = odbc_result($rs,'alt');
$im = odbc_result($rs,'image');

// Query Results Output On Screen

if ($image = @getimagesize ("cat_img/$im") {
echo "<img src=\"cat_img/$im\" width='130' height='110' border='0' />";
} else {
echo "<img src=\"cat_img/noimage.jpg\" width='75' height='60' border='1' vspace='2' />";
}

echo '
<h3>' . $pn . ' </h3>
<p>number: ' . $po . ' reference: ' . $pr . '</p>
<p><b>Description</b></p>
<p>' . $pd . '</p>
<p><b>Price</b></p>
<p>£' . $pp . '</p>
';
}
odbc_close($conn);
} else {
echo'<p>Your Cart Is empty</p>';
echo $query;
}

?>

When you add something to the cat it does say item has been added but when you go to view cart the flag $empty stays at true. This does seem to me to be a complicated probelm and like I said I have searched for hours on the net for soultions I am not sure if my approcah is correct or if indeed I have just typed a load of nonsene but some understanding in to why it does not register would be great then I can go from there.

Many Thanks

Steve C

haggul

2:41 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Hmmm

Not sure I would be trying to maintain shopping basket contents in a session variable - why not write the added items into the DB and then your cart contents script can use one nice simple query to return the data rather than opening and closing everything for each item. Also you can save cart items for returning visitors?

dc_duo

6:09 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Nice idea. I have since sorted out that session variable problem. But the idea of a database is a good one. Would I just use a common field i.e.

Customer

J.Smith

Basket

id_no customer goods
3 J.Smith 1 x thingy

Steve C