Jump to content

Empty quantity error


wixil

Recommended Posts

I implemented this code

<?php
 session_start();
require_once("config.php");

//code for Cart
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
    //code for adding product in cart
    case "add":
        if(!empty($_POST["quantity"])) {
            $pid=$_GET["pid"];
            $result=mysqli_query($con,"SELECT * FROM tblproduct WHERE id='$pid'");
              while($productByCode=mysqli_fetch_array($result)){
                    $itemArray = array($productByCode["code"]=>array('name'=>$productByCode["name"], 'code'=>$productByCode["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["price"], 'image'=>$productByCode["image"]));
            if(!empty($_SESSION["cart_item"])) {
                if(in_array($productByCode["code"],array_keys($_SESSION["cart_item"]))) {
                    foreach($_SESSION["cart_item"] as $k => $v) {
                            if($productByCode["code"] == $k) {
                                if(empty($_SESSION["cart_item"][$k]["quantity"])) {
                                    $_SESSION["cart_item"][$k]["quantity"] = 0;
                                }
                                $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
                            }
                    }
                } else {
                    $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                }
            }  else {
                $_SESSION["cart_item"] = $itemArray;
            }
        }
    }
    break;

    // code for removing product from cart
    case "remove":
        if(!empty($_SESSION["cart_item"])) {
            foreach($_SESSION["cart_item"] as $k => $v) {
                    if($_GET["code"] == $k)
                        unset($_SESSION["cart_item"][$k]);
                    if(empty($_SESSION["cart_item"]))
                        unset($_SESSION["cart_item"]);
            }
        }
    break;
    // code for if cart is empty
    case "empty":
        unset($_SESSION["cart_item"]);
    break;
}
}

// Send the user to the place order page if they click the Place Order button, also the cart should not be empty
if (isset($_POST['placeorder'])) {
    header("Location: "."placeorder.php");
    exit;
}



?>

cart display code
<?php
if(isset($_SESSION["cart_item"])){
$total_quantity = 0;
$total_price = 0;
?>
<table class="tbl-cart" cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th style="text-align:left;background-color:#3092C0,color:white;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:20;">PRODUCT</th>
<th style="text-align:left;background-color:#3092C0,color:white;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:20;" width="5%">QUANTITY</th>
<th style="text-align:center;background-color:#3092C0,color:white;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:20;" width="5%">REMOVE</th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
$item_price = $item["quantity"]*$item["price"];
?>
<tr>
<td style="text-align:left;background-color:transparent,color:#303030;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:16;"><img src="<?php echo $item["image"]; ?>" class="cart-item-image" /><?php echo $item["name"]; ?></td>
<td style="text-align:left;background-color:transparent,color:#303030;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:16;"><?php echo $item["quantity"]; ?></td>
<td style="text-align:center;background-color:transparent,color:#303030;padding-top:12px;padding-bottom:12px;font-family:Saira Condensed;font-size:16;"><a href="preset_template.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td>
</tr>
<?php
$total_quantity += $item["quantity"];
$total_price += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td></td>
</tr>
</tbody>
</table>
<?php
} else {
?>
<div class="no-records">Your Shopping Cart is Empty</div>
<?php
}
?>

Am using $total_quantity += $item["quantity"]; to get total quantity. and $total_price += ($item["price"]*$item["quantity"]);

But am getting this error "Warning: Undefined variable $total_price in C:\xampp\htdocs\mart\preset_template\cart.php on line ...." when the  total price is zero and getting Warning: Undefined variable $total_quantity in C:\xampp\htdocs\mart\preset_template\cart.php when total quantity is zero but works perfectly when not empty. i want the total quantity and price to display 0 when empty instead of that error

Link to comment
Share on other sites

I'm not sure how you're getting that error. Are you sure the code you posted is exactly what you are running that creates the warnings? Because you very clearly do set those two variables with values ahead of time - they are not undefined.

Link to comment
Share on other sites

The lines are

<div style="font-size:28px; color:#3092C0; font-family:Saira Condensed;"><?php echo $total_price; ?></div></div> and

<div style="font-size:20px; color:white; font-family:Saira Condensed; margin-left:6px;"><?php echo $total_quantity; ?></div></div>

Link to comment
Share on other sites

1 hour ago, wixil said:

The lines are

<div style="font-size:28px; color:#3092C0; font-family:Saira Condensed;"><?php echo $total_price; ?></div></div> and

<div style="font-size:20px; color:white; font-family:Saira Condensed; margin-left:6px;"><?php echo $total_quantity; ?></div></div>

I don't see those lines included in your first post. Please post the full thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.