0

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)

2
  • 1
    Yes it is, but why are you even using $i? You're doing foreach($_POST['form'] as $key => $form), so just access $form directly. Commented Oct 12, 2013 at 13:06
  • Add if (!isset($_POST['form'][$i])) continue; at the very start inside your loop. This is what you want but it's not what you should be doing. Read up on foreach loop Commented Oct 12, 2013 at 13:08

2 Answers 2

3

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'];

    #...
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.