makamo66

  • Jan 12, 2020
  • Joined Dec 31, 2013
  • The var_dump of the $cart_items array is supposed to show an array of $cart_items with the quantities added together for the same id but it doesn't even show an array, it just shows one value.

    session_start();
    $cart_items = $_SESSION["cart_items"];
    if ( $cart_items == null ) {
    	$cart_items = array();
    }
    if ( isset($_REQUEST["element_id"]) ) {
    	$id = $_REQUEST["element_id"];
    }
    
    if ( isset($_REQUEST["quantity"]) ) {
    	$quantity = $_REQUEST["quantity"];
    }
    
    if ( isset($id)  && isset($quantity) ) {
    if (isset($cart_items[$id])) {
    	  	$cart_items[$id] += $quantity;
    
    } else {
        $cart_items[$id] = $quantity;
    }
    }
    var_dump($cart_items);
    • The value isn't there in the page source.

      • There's a delete button in the form that I removed in order to make the code easier to read.

        • <table border='1'><tr><th>Remove&nbsp;&nbsp;&nbsp;</th><th width='100'>Your Product&nbsp;&nbsp;</th><th>Quantity</th><th>Description</th><th>Price per item</th><th>Sub Total</th></tr>
          
          <?php
          
          echo "<form method='post' name='yourForm'>";
          
          echo "<tr><td></td>";
          
          echo "<td><img src=" . $thumbnail .   "/></td>";
          
          echo "<td>" . $cart_product_quantity . "</td>";
          
          echo "<td> " . $product . "(s)&nbsp;&nbsp;</td>";
          
          echo "<td> \$" . $price . "</td><td> \$" . $number .  "</td></tr>";
          
          echo "</form>";
          
          echo "</table>";
          `
          ````
          • When I echo out my variable in the php file it works fine but when I put the variable in a table cell it doesn't echo out.

            • ThanX, laserlight. That's a lot to digest and I've been working on this all day so I'm all tuckered out. I will implement your code tomorrow morning when I get a fresh start.

              • How do I get my session to be unset?

                My form looks like this:

                foreach ($joined as $i => $qty){
                echo "<form action='' method='get' name='yourForm'>";
                echo "<button type='submit' name='remove_" . $i . "' value='remove_" . $i . "' class='deletebtn' >X</button>";
                echo "</form>";
                }

                My SESSION is defined like this:

                for($i=0; $i<=5; $i++){
                if (isset($_REQUEST["element_id_$i"]) ) {
                $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
                $id = $_SESSION["element_id_$i"];	
                array_push($_SESSION["element_id"],$id);
                }
                $id = $_SESSION["element_id"];
                }

                and the form submits to:

                if (isset($_REQUEST["remove_$i"]) ){
                unset($_SESSION["quantity[$i]"]);
                unset($_SESSION["element_id[$i]"]);
                var_dump($_SESSION["element_id"]);
                var_dump($_SESSION["quantity"]);
                echo "Received variable " . $_REQUEST["remove_$i"];
                echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id[$i]"] . '<br><br>';
                }

                The output is:

                array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } Received variable remove_1
                Notice: Undefined index: element_id[1] in C:\xampp\htdocs\TopView\cart5.php on line 147
                TARGET INDEX TO BE REMOVED:

                • When I use the following, it still doesn't work:

                  <?php

                  for($i=0; $i<=5; $i++){

                  echo "<form action='' method='get' name='yourForm'>";

                  echo "<button type='submit' value='remove_" . $i . "' name='delete' class='deletebtn'>X</button>";
                  echo "</form>";

                  }
                  for($i=0; $i<=5; $i++){
                  if (isset($REQUEST["remove$i"])){
                  echo "delete";
                  echo "Received Value: " . $REQUEST["remove$i"];
                  }
                  }

                  ?>

                  • When I remove exit it still doesn't work.

                    • The file test2.php consists of these simple forms:

                      <?php
                      
                      for($i=0; $i<=5; $i++){
                      
                      echo "<form action='reset4.php' method='get' name='yourForm'>";
                      
                      echo "<button type='submit' value='delete' name='remove_" . $i . "' class='deletebtn'>X</button>";
                      
                      echo "</form>";
                      
                      }
                      
                      ?>

                      It submits to reset4.php which is this simple code:

                      <?php
                      header("Location: test2.php");
                      exit;
                      
                      
                      for($i=0; $i<=5; $i++){
                      if (isset($_REQUEST["remove_$i"])){
                      echo "Deleted";
                      }}
                      
                      ?>

                      But it doesn't work. The $_REQUEST doesn't populate the address field and it apparently never gets submitted to reset4.php like it should. This is such a simple program, I can't imagine why it doesn't work.

                      • This is the cart array:
                        $cart = array( "1" => "2", "3" => "4", "5" => "6");
                        var_dump($cart);
                        array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" }
                        This is the cart items array:
                        $cart_items = array();
                        $cart_items["id"] = $id;
                        $cart_items["quantity"] = $quantity;
                        var_dump($cart_items);
                        array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } }

                        I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?

                        • I'm doing something like that. My quantity is updating when I add the same product but I need to delete the product that's the last item in the SESSION array. So far I've tried array_pop and unset but I can't get rid of the non-updated product.

                          These two don't work to remove the last product added to the session:

                          unset($SESSION["cart_item"]["element_id"]);
                          array_pop($
                          SESSION["cart_item"]);

                          • If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?

                            • This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen.

                              $cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $REQUEST id and the $REQUEST quantity. So the two $REQUESTs are popped off with array_pop and then the remaining $SESSIONs are compared to the $_REQUEST id with

                              $j = array_search($REQUEST["element_id$i"], $temp_array);
                              $j is now the key that corresponds to the id in $temp_array where the $_REQUEST id has been found.

                              if( $j==$temp_array[$i])

                              If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together.

                              There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly,
                              I get the error "Undefined offset" for the $i variable in this line of the code: if( $j==$temp_array[$i])

                              
                              foreach($cart_items as $item) {
                              foreach($item["id"] as $key => $val) {
                              foreach($item["quantity"] as $i => $value) {
                              	if ($key == $i){
                              	
                              $temp_array = $cart_items;
                              $array = array_pop($temp_array);
                              var_dump($temp_array);
                              
                              if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){
                              	
                              $j = array_search($_REQUEST["element_id_$i"], $temp_array);
                              if( $j==$temp_array[$i]) {
                              echo "<b>SAME PRODUCT ADDED</b>";
                              $value += $_REQUEST["quantity_$i"];
                              unset($_REQUEST["element_id_$i"]);
                              }}}}}}
                              • I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post.

                                • The solution can't include nested foreach loops because I will be adding 3 more inner arrays to the multidimensional array and when I do this with nested foreach loops, I need 6 of them.

                                  • <?php
                                    $SESSION["cart_item"] = array(
                                    'cart_item' => array(
                                    'id' => $id,
                                    'product_name' => $product_name
                                    ));
                                    }
                                    $cart_items = $
                                    SESSION["cart_item"];

                                    foreach ($cart_items as $cart_item) {
                                    echo $cart_item["id"] . $cart_item["product_name"];
                                    }
                                    ?>
                                    I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use:
                                    var_dump($cart_items);

                                    I get the following output:

                                    array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } }

                                    • I don't understand how array_pop is supposed to work on a SESSION array

                                      I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don't understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items.

                                      • Thank you, NogDog. That solved the problem perfectly.