RichiT81 Posted February 26, 2012 Share Posted February 26, 2012 Hi, I am building an online store, and have been using a tutorial to get me started. I have been adjusting the code as I require additional functionality and I have hit a brick wall... I have a multidimensional array that contains all of the information I have passed from the product page. What I want to do is write a piece of PHP code that will increase the quantity of a particular item based on 2 of 3 variables matching the data in the array. Below is the full working code which runs the adjustment based on... <?php if ($key == "item_id" && $value == $item_to_adjust) { ?> What I want to do is add to the if statement like so... <?php if (($key == "item_id" && $value == $item_to_adjust) && (($key == "custom_txt" && $value == $custom_txt) || ($key == "img" && $value == $fileName))) { ?> But this code doesn't seem to work, Is there another way to get the result I am after? <?php // Section 3 (if user chooses to adjust item quantity) if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { $item_to_adjust = $_POST['item_to_adjust']; $custom_txt = $_POST['custom_txt']; $fileName = $_POST['fileName']; $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))); } // close if condition } // close while loop } // close foreach loop } ?> Thanks in advance! Richi Quote Link to comment Share on other sites More sharing options...
freelance84 Posted February 27, 2012 Share Posted February 27, 2012 <?php if (($key == "item_id" && $value == $item_to_adjust) && (($key == "custom_txt" && $value == $custom_txt) || ($key == "img" && $value == $fileName))) { ?> It looks like you are asking if the key is equal to both item_id and custom_txt here. If you are asking if the key matches one of the three this might work: <?php if (($key == "item_id" && $value == $item_to_adjust) || ($key == "custom_txt" && $value == $custom_txt) || ($key == "img" && $value == $fileName)) { ?> Quote Link to comment Share on other sites More sharing options...
RichiT81 Posted February 27, 2012 Author Share Posted February 27, 2012 Massive thanks for your help, although it didn't solve the problem itself, it did trigger a thought which led to the answer I needed! For info, the following code has done what I was after... <?php if ($key == "item_id" && $value == $item_to_adjust && ($each_item["custom_txt"] == $custom_txt || $each_item["img"] == $fileName)) { ?> Thanks again! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 27, 2012 Share Posted February 27, 2012 I think it would help if you show an example of the values you might expect for the input data. The condition you provided would never be true. Here is your original condition but but with some emphasis added to a few 'key' parts (pun intended) if (($key == "item_id" && $value == $item_to_adjust) && (($key == "custom_txt" && $value == $custom_txt) || ($key == "img" && $value == $fileName))) { If you take the $value variable out of that condition you are left with if ($key == "item_id" && ($key == "custom_txt" || $key == "img")) { $key can never be equal to "item_id" AND also be equal to ("custom_txt" OR "img") Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 27, 2012 Share Posted February 27, 2012 OK, looking a bit more at your code, you are making this way more complicated than it needs to be. 1. Instead of all those lines to validate the quantity value, you can use the min() and max() functions to do the same thing with one line. Replace this $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; } With this $quantity = min(max($_POST['quantity'], 1), 99); //Ensure qty is between 1 and 99 2. I think your array structure for the cart_array is inefficient. I think it looks something like this 0 => array ('item_id' => 2, 'quantity'=>5), 1 => array ('item_id' => 5, 'quantity'=>10), 2 => array ('item_id' => 19, 'quantity'=>3) Instead of including the item_id as part of the sub-array the item_id should be the index and the quantity the value 2 => 5, 5 => 10, 19 => 3 Even with what you have the while() loop is completely unnecessary to find the 'item_id' element. Each record in the array should have one element by that name, so just reference it. I would post some code, but I really need to see an example of your current array (although I would personally use a different structure)/ Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 27, 2012 Share Posted February 27, 2012 After some further thought, you should completely redo this process. I assume you already have a process for adding items to your cart. You do not need a completely new process for editing quantities. Instead, create ONE process that will add or update quantities. It makes you code much more clean. //This process will ADD or UPDATE an item in the cart $update = false; foreach ($_SESSION['cart_array'] as &$item) //<==make $item a reference { //Check if item already exists in cart if(isset($item['item_id']) && $item['item_id']==$item_to_adjust) { //Item already in cart, update quantity $item['quantity'] = $quantity; $update = true; break; //Exit loop since we already found the match } } // close foreach loop if(!$update) { $_SESSION['cart_array'][] = array('item_id' => $item_to_adjust, 'quantity' => $quantity); } However, if you were to modify the cart array like I suggest above, this becomes a much, much easier process - just one line $_SESSION['cart_array'][$item_to_adjust]['quantity'] = $quantity; If the item is already in the cart it gets updated, if not it gets added! Quote Link to comment 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.