I’m writing sales software that will be walking through a set of pages and on certain pages there are items listed to sell and when you click buy it basically just passes a hidden variable to the next page to be set as a session variable, and then when you get to the end it call gets reported to a database.
However my employer wanted me to include a shopping cart, and this shopping cart should display the item name, sku, and price of whatever you’re buying, as well as a remove button so the person doing the script doesn’t need to go back through the entire thing to remove one item.
At the moment I have the cart set to display everything, which was fairly simple. But I can’t figure out how to get the remove button to work. Here is the code for the shopping cart:
$total = 0;
//TEST CODE:
$_SESSION['itemname-addon'] = "Test addon";
$_SESSION ['price-addon'] = 10.00;
$_SESSION ['sku-addon'] = "1234h";
$_SESSION['itemname-addon1'] = "Test addon1";
$_SESSION ['price-addon1'] = 99.90;
$_SESSION ['sku-addon1'] = "1111";
$_SESSION['itemname-addon2'] = "Test addon2";
$_SESSION ['price-addon2'] = 19.10;
$_SESSION ['sku-addon2'] = "123";
//end test code
$items = Array
(
"0"=> Array
(
"name" => $_SESSION['itemname-mo'],
"price" => $_SESSION ['price-mo'],
"sku" => $_SESSION ['sku-mo']
),
"1" => Array
(
"name" => $_SESSION['itemname-addon'],
"price" => $_SESSION ['price-addon'],
"sku" => $_SESSION ['sku-addon']
),
"2" => Array
(
"name" => $_SESSION['itemname-addon1'],
"price" => $_SESSION ['price-addon1'],
"sku" => $_SESSION ['sku-addon1']
),
"3" => Array
(
"name" => $_SESSION['itemname-addon2'],
"price" => $_SESSION ['price-addon2'],
"sku" => $_SESSION ['sku-addon2']
)
);
$a_length = count($items);
for($x = 0; $x<$a_length; $x++){
$total +=$items[$x]['price'];
}
$formattedtotal = number_format($total,2,'.','');
for($i = 0; $i < $a_length; $i++){
$name = $items[$i]['name'];
$price = $items[$i]['price'];
$sku = $items[$i]['sku'];
displaycart($name,$price,$sku);
}
echo "<br />
<b>Sub Total:</b>
$$formattedtotal";
function displaycart($name,$price,$sku){
if($name != null || $price != null || $sku != null){
if ($name == "no sale" || $price == "no sale" || $sku == "no sale"){
echo "";
}
else{
$formattedprice = number_format($price,2,'.','');
echo "$name: $$formattedprice ($sku)";
echo "<form action=\"\" method=\"post\">";
echo "<button type=\"submit\" />Remove</button><br />";
echo "</form>";
}
}
}
So at this point I’m not sure where to go from here for the remove button.
You can use arrays on session variables so you could change your format to
in your
function displaycart()add the elementthen when you delete you simply: