Jump to content

Need help updating shopping cart


phunnydoode

Recommended Posts

<?php
if (!isset($_SESSION)) session_start();
$qstr = $_SERVER['QUERY_STRING'];
parse_str ( $qstr );
?>
<body>
	<div id="wrap">
			
			<?php require 'header.php';?>
	
		<section>
		<tr><td colspan='2'><h4>Shopping Cart</td><tr>
<tr>
    <td colspan="2">
        <table border="0" padding="0" cellspacing="0">
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method ="post" 
name="cartForm" id="cartForm" onsubmit="return checkSubmit();">
            <tr class="trColor">
                <td width="200" class="nvtxt"><h4>Item Description</td>
                <td width="100" class="nvtxt"><h4>Item Price</td>
                <td width="100" class="nvtxt"><h4>Quantity</td>
                <td width="100" class="nvtxt"><h4>Item Total</td>
            </tr>
            <?php
            foreach($_SESSION['myCart'] AS $temp)  {
                $itmTot = $temp["qty"] * $temp["price"];
                ?>
                <tr>
                    <td><h4><?= $temp["item"] ?></td>
                    <td><h4>$<?= $temp["price"] ?></td>
                    <td><h4><input type="text"  size="3" value="<?= $temp['qty'] ?>" ></td>
                    <td><h4>$<?= $itmTot ?></td>
                </tr>
   
            <?php
            }
            ?>
            <tr class="trColor">
                <td colspan="2"> </td>
                <td class="nvtxt"><h4>Quantity</td>
                <td class="nvtxt"><h4>Total</td>
            </tr>
            <tr>
                <td colspan="2" />  
                <td><h4><?= $_SESSION['myToti'] ?></td>
                <td><h4>$<?= $_SESSION['myTotp'] ?></td>
            </tr>
            <tr class="trColor"><td colspan="4"> </td></tr>
            <tr>
                <td colspan="4" align="center" height="100">
                    <a href="index.php">Continue Shopping</a>
                  
                    <BR>
                    <a href=checkout.php>Checkout</a>
                    <BR>
                    <input type="submit" name="submit" id="submit" value="Clear Cart" />
                 	<input type="submit" name="save" id="save" value="Edit Card"/>
                    <?php
if(isset($_POST['submit'])){
	unset($_SESSION['myCart']);
	header("Location:index.php");
}
?>
<?php 
if(isset($_POST['save'])){
	

}
 ?>

Above is Viewing the cart which has two buttons. One which clears the cart and the other which will be used to input a new value such as 0 to change the cart and clear that item from the cart- I don't know how to go about doing this so any help is greatly appreciated. Here's the rest of my code. 

 

<!--cart.php-->
<?php
if (!isset($_SESSION)) session_start();
$qstr = $_SERVER['QUERY_STRING'];
parse_str ( $qstr );
?>
<body>
	<div id="wrap">

			
			<?php require 'header.php';?>
	
		<section><h4>
<tr><td colspan='2'>Shopping Cart</td><tr>
<tr>
    <td colspan="2">
        <table border="0" padding="0" cellspacing="0">
            <form action="cartUpdate.php" method="post" name="myForm">
                <tr class="trColor">
                    <td width="200" class="nvtxt"><h4>Item Description</td>
                    <td width="100" class="nvtxt"><h4>Item Price</td>
                    <td width="100" class="nvtxt"><h4>Quantity</td>
                </tr>
                <tr height="100">
                    <td><h4><?= $item ?></td>
                    <td><h4>$<?= $price ?></td>
                    <td>
                        <input type="text" name="qty" id="qty" size="3" placeholder="0">
                    </td>
                    <input type="hidden" name="item" value="<?= $item ?>">
                    <input type="hidden" name="price" value="<?= $price ?>">
                </tr>
                <tr class="trColor"><td colspan="3"> </td></tr>
                <tr>
                    <td colspan="3" align="center" height="100">
                        <!-- continue shopping -->
                        <a href="index.php">Continue Shopping</a>
                        <!-- update cart -->
                        <a href="javascript:void(0)" onclick="document.myForm.submit();">Add to Cart</a>
                    </td>
                </tr>
            </form>
        </table>

Script to update the cart when clicking the button "Add to Cart"

 

<?php

if (!isset($_SESSION)) session_start();

$item = $_POST['item'];
$price = $_POST['price'];
$qty = $_POST['qty'];



$_SESSION['myCart'][] = array("item"=>$item,"qty"=>$qty,"price"=>$price);
$_SESSION['myToti'] = $_SESSION['myToti'] + $qty;
$_SESSION['myTotp'] = $_SESSION['myTotp'] + ($price * $qty);
header("Location:cartView.php");


?>	
<?php
$con=mysqli_connect('localhost', 'root', '');

/* check connection */

if (mysqli_connect_errno($con)) {
	trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
}

 $query = "SELECT `inventory` FROM `bencobricks` . `product`";
 $result = mysqli_query($con, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($con), E_USER_ERROR); 
 while($rows=mysqli_fetch_array($result)){
 
 if ($qty - $rows['inventory'] < '0') {
 	echo "Unavailable";
 }
 }

 
 ?>
Link to comment
https://forums.phpfreaks.com/topic/294298-need-help-updating-shopping-cart/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.