Jump to content

If statements & Multidimensional Arrays


RichiT81

Recommended Posts

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

 

Link to comment
Share on other sites

<?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)) {

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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")

Link to comment
Share on other sites

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)/

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.