Jump to content


  • Content Count

  • Joined

  • Last visited

Community Reputation

0 Neutral

About makamo66

  • Rank
  • Birthday 10/23/1966

Contact Methods

  • Website URL

Profile Information

  • Gender
  1. 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:
  2. When I do 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"]; } } ?>
  3. When I remove exit it still doesn't work.
  4. 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.
  5. $id 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 $quantity is similar.
  6. When I use this: $cart_items[$id] = $quantity; or this: $cart_items = array($id => $quantity); I get the error: Illegal offset type
  7. 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?
  8. This is what I'm using and it's not working: foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ if(empty($item["quantity"])){ $item["quantity"] = $value; } elseif (isset($_REQUEST["quantity_$i"]) ) { $item["quantity"] = $value + $_REQUEST["quantity_$i"]; $value = $item["quantity"]; echo "Same product"; }}}}
  9. The last product in the SESSION array is the product with the non-updated quantity. When I update the quantity, I get the previous product and also the updated product. I have to remove the non-updated product.
  10. 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"]);
  11. 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?
  12. 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"]); }}}}}}
  13. I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post.
  14. The inner arrays of the multidimensional $_SESSION arrays aren't single variables, they're $_SESSION arrays defined like below. for($i=1; $i<=6; $i++){ if (isset($_REQUEST["element_id_$i"]) && ($_REQUEST["element_id_$i"] != NULL) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; if (isset($_REQUEST["product_$i"]) && ($_REQUEST["product_$i"] != NULL) ) { $_SESSION["product_name_$i"] = $_REQUEST["product_$i"]; $product_name = $_SESSION["product_name_$i"]; array_push($_SESSION["product_name"],$product_name); } $product_name = $_SESSION["product_name"]; }
  • 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.