0

Thanks for help in advance.

I am trying to make shopping cart and its almost done but only having an issue, that is when i try to add 3rd product or say more than 2 products it only show me 2 products and remove very first entry from the list. Here is my add to cart code.

<?php

session_start();
include_once './includes/conn_manager.php';
//empty cart by destroying current session
if (isset($_GET['empty_cart']) && $_GET['empty_cart'] == 1) {
    $return_url = base64_decode($_GET['return_url']); //retuen url
    unset($_SESSION['cart']);
    header("Location: " . $return_url);
}
//add item in cart
if (isset($_POST['type']) && $_POST['type'] == 'add') {

    $qtt = filter_input(INPUT_POST, 'qtt', FILTER_DEFAULT); //product quantity
    $e1 = explode(" - Rs.:", $qtt);
    $quantity = trim($e1[0]);
    $price = trim($e1[1]);
    $pid = filter_input(INPUT_POST, 'prid', FILTER_DEFAULT); // product id
    //$return_url = base64_decode($_POST['return_url']); // return url

    $result = $mysqli->query("select * from pro_data where pro_uid='$pid' limit 1");//getting product info from db
    $obj = $result->fetch_object(); //fetching product info as array of objects

    if($result)// check if $result worked
    {

        // now we have the product info

        // making product info array
        $new_product = array(array('pnm'=>$obj->pro_nm,'pcode'=>$pid,'pqtt'=>$quantity,'pprice'=>$price));

        if(isset($_SESSION['cart'])){
            $isin = false;

            foreach ($_SESSION['cart'] as $cart_item){
                if($cart_item['pcode'] == $pid){
                    $products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price));
                    $isin = true;
                }  else {
                    $products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]));
                }
            }

            if($isin == false){
                $_SESSION['cart'] = array_merge($products, $new_product);
            }  else {
                $_SESSION['cart'] = $products;
            }
        }  else {
            $_SESSION['check'] = 'reached';
            $_SESSION['cart'] = $new_product;
        }
    }
}
?>

1 Answer 1

1

The problem lies in the way you generate your $products array:

<?php
foreach ($_SESSION['cart'] as $cart_item){
    if($cart_item['pcode'] == $pid){
        $products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price));
        $isin = true;
    }  else {
        $products = array(array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]));
    }
}

The resulting $products will always be an array with only one entry, because you overwrite the $products variable for every iteration of your foreach.

You would be better off doing something such as:

<?php
$products = array();
foreach ($_SESSION['cart'] as $cart_item){
    if($cart_item['pcode'] == $pid){
        $products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price);
        $isin = true;
    }  else {
        $products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]);
    }
}

The $products[] syntax will append the cart entry to the end of your $products array for every iteration. With a quick refactoring (could still be better):

<?php
$new_product = array('pnm'=>$obj->pro_nm,'pcode'=>$pid,'pqtt'=>$quantity,'pprice'=>$price);
if (isset($_SESSION['cart'])) {
    $isin = false;
    $products = array();

    foreach ($_SESSION['cart'] as $cart_item) {
        if ($cart_item['pcode'] == $pid) {
            $products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$quantity,'pprice'=>$price);
            $isin = true;
        }  else {
            $products[] = array('pnm'=>$cart_item["pnm"],'pcode'=>$cart_item["pcode"],'pqtt'=>$cart_item["pqtt"],'pprice'=>$cart_item["pprice"]);
        }
    }

    // If the new product is not already in the list, we add it
    if ($isin == false) {
        $products[] = $new_product;
    }
    $_SESSION['cart'] = $products;
}  else {
    $_SESSION['check'] = 'reached';
    $_SESSION['cart'] = array($new_product);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.