Forum Moderators: coopster

Message Too Old, No Replies

shopping cart works but need to add options through $ POST variable

         

craigj1303

10:58 am on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi All
I am in desperate need of help with a shopping cart script i've been working on. Here is the background.
Basically, I have been working on a site for a furniture company. I have added some products to a database and am now at the stage where I want to add the product to a shopping basket. Here is the script which is called when I add a product to the basket or view the cart:


<?php
include ('functions.php');
session_start();
@ $new = $_GET['new'];

if($new)
{
//new item selected
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] ='0.00';
}
if(isset($_SESSION['cart'][$new]))
$_SESSION['cart'][$new]++;
else
$_SESSION['cart'][$new] = 1;
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
if(isset($_POST['save']))
{
foreach ($_SESSION['cart'] as $prodcode => $qty)
{
if($_POST[$prodcode]=='0')
unset($_SESSION['cart'][$prodcode]);
else
$_SESSION['cart'][$prodcode] = $_POST[$prodcode];
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}

if($_SESSION['cart']&&array_count_values($_SESSION['cart']))
display_cart($_SESSION['cart']);
else
{
echo '<p>There are no items in your cart</p>';
}
$target = 'index.php';
// if we have just added an item to the cart, continue shopping in that category
if($new)
{
$details = get_product_details($new);
if($details['category_id'])
$target = 'index.php?page=show_category&category_id='.$details['category_id'].'&sort='.$_GET['sort'];
}
?>



This script is working great with standard products. The variable $_GET['new'] is carried over to the script from the product page and the function "display_cart" displays each product with a little thumbnail of the item and some details in a nice table row where you can alter the qty, delete the item etc. Great.....no problem.
Now this is the curve ball! Certain items have "optional finishes". i.e. product number 1234 is available in white, black and red. So I thought, OK, before the user adds that item to the cart they select a finish from a drop down menu and this information can be carried across to the cart script by way of $_POST['finish']. But for the life of me I cannot work out how to display this information in my cart table row alongside the product information. Obviously I can echo the $_POST['finish'] variable which is great for the first item but if I add another product to the cart in a different finish it changes the finish for the first item and every other item as well!
Here is the code for the function display_cart:


function display_cart($cart, $change = true, $images = 1)
{
// display items in shopping cart
// optionally allow changes (true or false)
// optionally include images (1 - yes, 0 - no)
echo '<table style="margin-top: 20px;" border = "0" width = "95%" cellspacing = 0>
<form action = "index.php?page=show_cart" method = "post">
<tr><th colspan = '. (1+$images) .' bgcolor="#905f36">Item</th>
<th bgcolor="#905f36">Finish</th><th bgcolor="#905f36">Price</th><th bgcolor="#905f36">Quantity</th>
<th bgcolor="#905f36">Total</th></tr>';
//display each item as a table row
foreach ($cart as $prodcode => $qty)
{
$item = get_product_details($prodcode);
echo '<tr>';
if($images ==true)
{
echo '<td align = left>';
if (file_exists('images/products/small/'.$item['sm_image']))
{
$size = GetImageSize('images/products/small/'.$item['sm_image']);
if($size[0]>0 && $size[1]>0)
{
echo '<img src="images/products/small/'.$item['sm_image'].'" ';
echo 'width ="'. $size[0]/3 .'" height = "' .$size[1]/3 . '" />';
}
}
else
echo '&nbsp;';
echo '</td>';
}
echo '<td align = "left">';
echo '<a href = "index.php?page=show_product&product='.$prodcode.'">'.$item['description'].'</a>';
echo '</td>';
[b]
echo '<td></td>';
[/b]
echo '<td align = "center">$'.number_format($item['price'], 2);
echo '</td><td align = "center">';
// if we allow changes, quantities are in text boxes
if ($change == true)
echo "<input type = 'text' name = \"$prodcode\" value = \"$qty\" size = 3>";
else
echo $qty;
echo '</td><td align = "center">$'.number_format($item['price']*$qty,2)."</td></tr>\n";
}
// display total row
echo "<tr>
<th colspan = ". (3+$images) ." bgcolor=\"#905f36\">&nbsp;</td>
<th align = \"center\" bgcolor=\"#905f36\">
".$_SESSION['items']."
</th>
<th align = \"center\" bgcolor=\"#905f36\">
\$".number_format($_SESSION['total_price'], 2).
'</th>
</tr>';
// display save change button
if($change == true)
{
echo '<tr>
<td colspan = '. (2+$images) .'>&nbsp;</td>
<td align = "center">
<input type = "hidden" name = "save" value = true>
<input type = "image" src = "images/save.png"
border = 0 alt = "Save Changes">
</td>
<td>&nbsp;</td>
</tr>';
}
echo '</form></table>';

}
?>


It is between the <td></td> cells I have highlighted between bold tags in the above function that I need to contain the optional finish if one has been selected by the client.
Would really appreciate your help with this one guys. I've been trying to program a solution for a week now but after much deliberation with associative arrays, loops and $_SESSION variables i'm at the stage where it has taken over my life and I can think of nothing else until I get it resolved and I can't do it by myself! I think the answer lies with $_SESSION variables but i'm at that "staring at the computer screen with a blank expression on my face" stage and I can no longer see the wood for the trees!
HELP! Many thanks
Craig

Matthew1980

11:38 am on Jul 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there craigj1303,

Welcome to WebmasterWorld :) [webmasterworld.com ]

Could you post the few lines of code that you are struggling with as the formatting for the bold tags doesn't seem to have translated from edit to post :)

Though I will ask a question, I'm not sure why you are suppressing the $_GET at the start, you need to check to see if its set first before actioning the rest of the function.

Not picking fault either, but your encasing '{' and '}' on some of the if/else clauses seem to be missing, you really should try using error_reporting(E_ALL); to see if there are any warning's or errors that are being masked anywhere.

WRT your issue: if you are passing the info through the URL you can use the $_GET['finish'] and do it that way or just use $_SESSION to store the information whilst pages are cycled through.

Hope this helps a little, but without knowing exactly the piece of code your struggling with it's kinda difficult, and many people find sifting through huge chunks of code kinda off putting. Just thought I would mention that for future posts :)

Cheers,
MRb