Rath Baloth
Rath Baloth

Reputation: 139

Need foreach to skip/ignore missing array

I have this php code for inserting data from my from;

$i = 1;
foreach ( $_POST['form'] as $val => $form ){   
    $Style = $_POST['form'][$i]['style'];
    $Dim= $_POST['form'][$i]['Dim'];
    $Colour= $_POST['form'][$i]['Colour'];
    $Quantity= $_POST['form'][$i]['Quantity'];

    $stmt = $db->prepare("INSERT INTO orders(Cus_ID, Style, Dimensions, Colour, Quantity) VALUES(:Cus_ID,:Style,:Dimensions,:Colour,:Quantity)");

    $stmt->execute(array(':Cus_ID' => $Cus_ID, ':Style' => $Style, ':Dimensions' => $Dim, ':Colour' => $Colour, ':Quantity' => $Quantity));  

    $i++;
} 

When I submit my form a var_dump give me something like this;

array (size=3)
  'form' => 
    array (size=3)
      1 => 
        array (size=4)
          'style' => string '0' (length=1)
          'Dim' => string '0' (length=1)
          'Colour' => string '0' (length=1)
          'Quantity' => string '' (length=0)
      2 => 
        array (size=4)
          'style' => string '0' (length=1)
          'Dim' => string '0' (length=1)
          'Colour' => string '0' (length=1)
          'Quantity' => string '' (length=0)
      3 => 
        array (size=4)
          'style' => string '0' (length=1)
          'Dim' => string '0' (length=1)
          'Colour' => string '0' (length=1)
          'Quantity' => string '' (length=0)
  'submit' => string 'Place Order' (length=11)

Everything is fine and dandy, but when i delete part of my form the submitted aray will end up something like this;

array (size=2)
  'form' => 
    array (size=2)
      1 => 
        array (size=4)
          'style' => string '0' (length=1)
          'Dim' => string '0' (length=1)
          'Colour' => string '0' (length=1)
          'Quantity' => string '' (length=0)
      3 => 
        array (size=4)
          'style' => string '0' (length=1)
          'Dim' => string '0' (length=1)
          'Colour' => string '0' (length=1)
          'Quantity' => string '' (length=0)
  'submit' => string 'Place Order' (length=11)

To which the php trys to enter the '2nd' array which doesn't exist, is it possible to have the php 'skip/ignore' any arrays that are missing (the array are unlimited and any random number could be missing)

Upvotes: 0

Views: 161

Answers (2)

Kaarel
Kaarel

Reputation: 170

In foreach, you don't need to use $i. $val is the key of the subarray. $form IS the subarray and can be used as:

foreach ( $_POST['form'] as $val => $form ){   
    $Style = $form['style'];
    $Dim= $form['Dim'];
    $Colour= $form['Colour'];
    $Quantity= $form['Quantity'];

    $stmt = $db->prepare("INSERT INTO orders(Cus_ID, Style, Dimensions, Colour, Quantity) VALUES(:Cus_ID,:Style,:Dimensions,:Colour,:Quantity)");

    $stmt->execute(array(':Cus_ID' => $Cus_ID, ':Style' => $Style, ':Dimensions' => $Dim, ':Colour' => $Colour, ':Quantity' => $Quantity));  

}

So, to answer your question, foreach never reaches missing array. It loops through array elements and so, if the array has two elements, it will iterate two times. If the array has three elements, it will iterate 3 times and so on.

Upvotes: 2

user229044
user229044

Reputation: 239290

Yes, it's possible, simply continue if the current index isn't set.

However, there is no reason for you to be using $i at all. Your foreach loop is written to give you access to each form as $form, so just use $form:

foreach ( $_POST['form'] as $val => $form ){  
    # No! 
    $Style = $_POST['form'][$i]['style'];
    $Dim   = $_POST['form'][$i]['Dim'];

    # Yes!
    $Style = $form['style'];
    $Dim   = $form['Dim'];

    #...
}

Upvotes: 3

Related Questions