1

I'm trying to process this array, first testing for the presence of a check, then extrapolating the data from quantity to return a valid price.

Here's the input for fixed amounts of items, with no variable quantity.

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>">
<input type="hidden" name="measure[<?=$item->id?>][quantity]" value="1" />

Here's the inputs for variable amounts of items.

<input type="checkbox" name="measure[<?=$item->id?>][checked]" value="<?=$item->id?>"> 
<input class="item_mult" value="0" type="text" name="measure[<?=$item->id?>][quantity]" />

So, the resulting array is multidimensional. Here's an output:

Array ( 
[1] => Array ( [quantity] => 1 ) 
[2] => Array ( [quantity] => 1 ) 
[3] => Array ( [quantity] => 1 ) 
...
[14] => Array ( [checked] => 14 [quantity] => 999 ) 
) 

Here's the loop I'm using to take this array and process items checked off the form in the first place. I guess the question essentially boils down to how do I structure my conditional statement to incorporate the multi-dimensional array?

foreach($field as $value):  
            if ($value['checked'] == TRUE) { 

                $query = $this->db->get_where('items', array('id' => $value['checked']))->row();

                #Test to see if quantity input is present
                if ($value['quantity'] == TRUE) {
                    $newprice = $value['quantity'] * $query->price;

                    $totals[] = $newprice;  
                }

                #Just return the base value if not
                else { 
                    $newprice = $query->price;

                    $totals[] = $newprice; 
                }

            }
            else { } ?>

            <p><?=$query->name?> - <?=money_format('%(#10n', $newprice)?></p>

        <? endforeach; ?>

My question: How can I change my loop to work with the multidimensional array?

4
  • Is it working like this? Or is there some sort of false output? Is $field the whole array or one row of it? Commented Mar 29, 2010 at 21:24
  • The foreach loop doesn't run, it breaks. I am looking for the right keys and values to 'foreach through', if I'm saying that right. Commented Mar 29, 2010 at 21:47
  • What errors/warnings do you get? The loop itself looks good to me (except a notice for checking for a not existent index $value[checked], but that does not break the loop). Commented Mar 29, 2010 at 22:04
  • Undefined index: checked Commented Mar 29, 2010 at 22:16

2 Answers 2

1

I've found at least 2 errors:

  1. put a declaration of $newprice outside the scope of the main if, otherwise it is not avaiable outside it, in the money_format call
  2. before checking the values of arrays, check if the value is set with the isset function.

So the code will be

foreach($field as $value):
            $newprice = 0;  
            if (isset($value['checked']) && $value['checked'] == TRUE) { 

                $query = $this->db->get_where('items', array('id' => $value['checked']))->row();

                #Test to see if quantity input is present
                if (isset($value['quantity']) &&  $value['quantity'] == TRUE) {
                    $newprice = $value['quantity'] * $query->price;

                    $totals[] = $newprice;  
                }

                #Just return the base value if not
                else { 
                    $newprice = $query->price;

                    $totals[] = $newprice; 
                }

            }
            else { } ?>

            <p><?=$query->name?> - <?=money_format('%(#10n', $newprice)?></p>

        <? endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

You shouldn't be comparing against true in an if statement. Simply use the bare expression and let PHP check its "trueness":

        if ($value['checked']) { 
            ....
            if ($value['quantity']) {

1 Comment

You're certainly right, but that doesn't break the loop as it is no type comparison.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.