Hi guys, this codes works perfectly to me, except that i would like to add pricetotal in session variable. my problem is pricetotal does not display values when i use print_r.

Heres the output when i use print_r to display session variable:
Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => ) )

It should be like this:
Array ( [0] => Array ( [item_id] => 15 [quantity] => 56 [pricetotal] => 5500) )

please help me to fix my problem: thanks

the code use:

    <?php 

    //Section 1 (if user attempts to add something to the cart from the product page)
    if (isset($_POST['pid'])) {
        $pid = $_POST['pid'];
        $wasFound = false;
        $i = 0;
        // If the cart session variable is not set or cart array is empty
        if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
            // RUN IF THE CART IS EMPTY OR NOT SET
            $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1,"pricetotal" => $pricetotal)); 
        } else {
            // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
            foreach ($_SESSION["cart_array"] as $each_item) { 
                  $i++;
                  while (list($key, $value) = each($each_item)) {
                      if ($key == "item_id" && $value == $pid) {
                          // That item is in cart already so let's adjust its quantity using array_splice()
                          array_splice($_SESSION["cart_array"], $i-1, 1, 
                          array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1,"pricetotal" => $pricetotal))); 
                          $wasFound = true;                   
                      } // close if condition
                  } // close while loop
               } // close foreach loop
               if ($wasFound == false) {
                   array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1, "pricetotal" => $pricetotal));
               }
        }

        redirect_to("cart.php"); 
        exit();
    }
    ?>
    <?php 

    // Section 2 (if user chooses to empty their shopping cart)
    if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
        unset($_SESSION["cart_array"]);
    }
    ?>

    <?php 
    //  Section 3 (if user chooses to adjust item quantity)
    if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
        // execute some code
        $item_to_adjust = $_POST['item_to_adjust'];
        $quantity = $_POST['quantity'];
        $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
        if ($quantity >= 100) { $quantity = 99; }
        if ($quantity < 1) { $quantity = 1; }
        if ($quantity == "") { $quantity = 1; }
        $i = 0;
        foreach ($_SESSION["cart_array"] as $each_item) { 
                  $i++;
                  while (list($key, $value) = each($each_item)) {
                      if ($key == "item_id" && $value == $item_to_adjust) {
                          // That item is in cart already so let's adjust its quantity using array_splice()
                          array_splice($_SESSION["cart_array"], $i-1, 1, 
                          array(array("item_id" => $item_to_adjust, "quantity" => $quantity,"pricetotal" => $pricetotal))); 
                      } // close if condition
                  } // close while loop
        } // close foreach loop
    }

    ?>
    <?php 
    //       Section 4 (if user wants to remove an item from cart)

    if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
        // Access the array and run code to remove that array index
        $key_to_remove = $_POST['index_to_remove'];
        if (count($_SESSION["cart_array"]) <= 1) {
            unset($_SESSION["cart_array"]);
        } else {
            unset($_SESSION["cart_array"]["$key_to_remove"]);
            sort($_SESSION["cart_array"]);
        }
    }
    ?>
    <?php //     Section 5  (render the cart for the user to view on the page)

    $pp_checkout_btn ="";
    $cartOutput = "";
    $cartTotal = "";
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
        $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
    } else {
            $i = 0;
        foreach ($_SESSION["cart_array"] as $each_item) { 
            $item_id = $each_item['item_id'];

            $sql = mysql_query("SELECT * FROM product WHERE id='$item_id' LIMIT 1");
            while ($row = mysql_fetch_array($sql)) {
                $product_name = $row["name"];
                $price = $row["price"];
                $details = $row["details"];
            }

            $pricetotal = $price * $each_item['quantity'];
            $cartTotal = $pricetotal + $cartTotal;
            $pricetotal = number_format($pricetotal,2);
            // Dynamic Checkout Btn Assembly
            $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">

            <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
            <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
            // Create the product array variable
            // Dynamic table row assembly
            $cartOutput .= "<tr>";
            $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br />
            <img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
            $cartOutput .= '<td>' . $details . '</td>';
            $cartOutput .= '<td>Php ' . $price . '</td>';
            $cartOutput .= '<td><form action="cart.php" method="post">
            <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
            <input name="adjustBtn' . $item_id . '" type="submit" value="Update" />
            <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
            </form></td>';
            //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
            $cartOutput .= '<td>' . $pricetotal . '</td>';
            $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" />
            <input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
            $cartOutput .= '</tr>';
            $i++; 
        } 
        $cartTotal = number_format($cartTotal,2);
        $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." Php</div>";

        }

    ?>
Member Avatar for LastMitch

@diether.silverious

Hi guys, this codes works perfectly to me, except that i would like to add pricetotal in session variable. my problem is pricetotal does not display values when i use print_r.

You mention it works but the issue you are having is the pricetotal appearing?

So another words it doesn't work.

If it doesn't work I would look at on line 99 which is this line:

$pricetotal = $price * $each_item['quantity'];

I'm also not sure why you comment line 120:

//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';

You are trying to assign $pricetotal to the $_SESSION["cart_array"] array several times on the beginning but $pricetotal variable is not existing yet at that point. It is getting created on line 99. Maybe you should assign it to a hidden input if you want to use it after POSTing the form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.