wixil Posted June 7, 2022 Share Posted June 7, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/ Share on other sites More sharing options...
requinix Posted June 7, 2022 Share Posted June 7, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597095 Share on other sites More sharing options...
wixil Posted June 8, 2022 Author Share Posted June 8, 2022 yea its the code that is producing the warning NOTE This warning clears as soon as a add an item in the cart Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597099 Share on other sites More sharing options...
requinix Posted June 8, 2022 Share Posted June 8, 2022 And what lines are PHP saying that have $total_price and $total_quantity undefined? Make sure to read the message carefully for exactly which lines it says that happens on. Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597100 Share on other sites More sharing options...
wixil Posted June 8, 2022 Author Share Posted June 8, 2022 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> Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597123 Share on other sites More sharing options...
Barand Posted June 8, 2022 Share Posted June 8, 2022 It seems to be either a problem with your error reporting or you have a problem between your chair and keyboard. Not only is it inventing errors that aren't errors, it's saying they are in lines of code that don't exist. Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597124 Share on other sites More sharing options...
requinix Posted June 8, 2022 Share Posted June 8, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314902-empty-quantity-error/#findComment-1597127 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.