0

I have 3 input that can be added dynamically. So the name of input is array, like:

<input type="text" name="qty1[]">
<input type="text" name="qty2[]">
<input type="text" name="qty3[]">

(Each of these input texts can be generated by javascript)

In my php file, I get them and want to insert to database (my controller in codeigniter):

$address = $this->input->post("qty1");
$LocX =    $this->input->post("qty2");
$LocY =    $this->input->post("qty3");

In my db part, where I want to use foreach and add to database:

Edit

foreach ($address as $key) {
  // I want to add $LocX and $LocY to the table
  // LocX and LocY are column names.
  $query = $this->db->query("INSERT INTO address (Comp_ID, Value, LocX, LocY)
                             VALUES(?, ?)", array($Comp_ID, $key ,? ,?));
}

I want to add all of them into foreach as parameter. As I searched it's not possible. What shoud I do? Thanks.

Edit2

The result of arrays, for example for $LocX :

Array
(
  [0] => test1
  [1] => test2
  [2] => test3
)
12
  • What do you mean by "add all of them" and "as parameter"? Commented Aug 15, 2015 at 10:08
  • @VladimirSerykh $address , $LocX and $locY, all of these variables are arrays. I should use them in foreach in order to insert into database. Commented Aug 15, 2015 at 10:10
  • @MattHB yes, something like that. Commented Aug 15, 2015 at 10:12
  • What do you want to be the expected output of your array? Before inserting in your database? Commented Aug 15, 2015 at 10:13
  • 1
    array_merge() is not preferable in this case, as it will overwrite the keys Commented Aug 15, 2015 at 10:14

3 Answers 3

1

You can use the index in the foreach to get to the other elements. But you need to carefully check if the index value exists in the other arrays (isset check)

For example:

foreach ($address as $key => $value) {
  if(isset($LocX[$key], $LocY[$key]) {
      $a_LocX = $LocX[$key]; // This will be the locx of the address
      $a_LocY = $LocY[$key]; // This will be the locy of the address

      // Change your query to fit the table and fill in the locx and y.
      $query = $this->db->query("INSERT INTO address (Comp_ID, Value)
                             VALUES(?, ?)", array($Comp_ID, $key));
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use a for loop for this.

$address = $this->input->post("qty1");
$LocX =    $this->input->post("qty2");
$LocY =    $this->input->post("qty3");

$n=count($address);
for($i=0;$i<$n;$i++){
       $a=$address[$i];
       $x=$locX[$i];
       $y=$locY[$i];
       //insert here
}

Comments

1

I will suggest you to use Codeigniter syntax, and this will make your insert work,

foreach($address as $k => $v)
{
    $insert = array(
                'Comp_ID' => $Comp_ID,
                'Value' => $v,
            ); // insert array

    if(in_array($LocX[$k], $LocX)) // existance check
        $insert = array_merge($insert, array('LocX' => $LocX[$k]));

    if(in_array($LocY[$k], $LocY)) // existance check
        $insert = array_merge($insert, array('LocY' => $LocY[$k]));

    $this->db->insert('address', $insert); // No need to write full query
}

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.