Welcome Guest, Not a member yet? Register   Sign In
Looping Around Form Array Values
#1
Information 
(This post was last modified: 04-27-2015, 02:34 PM by MoFish.)

Hi,

I'm looking for a little guidance on the best way to loop around some form input fields.

I have a form with a few input boxes. I have used the [] on the end of the names in an attempt to get these into an array.

View

Code:
<input type="text" class="form-control" id="url[]" name="url[]" placeholder="" value="<?=set_value('url[]'); ?>" />
<input type="text" class="form-control" id="price[]" name="price[]" placeholder="199" value="<?=set_value('price[]'); ?>" />

I'm now trying to get each of the values out and save them to the database.

I assume I need a foreach loop to do this, but am not sure on the best way to do this. I have the following in my controller so far.

Controller

PHP Code:
$post_array = array(
 
  'url' => $this->input->post('url[]'),
 
  'price' => $this->input->post('price[]'),
 
  'order_id' => '1'
);
$this->order_model->add($post_array); 

Any help/guidance would be much appriciated!

Thanks,

MoFish
Reply
#2

PHP Code:
$urls $this->input->post('url');
$prices $this->input->post('price');
foreach (
$urls as $key => $url)
{
 
   $post_array = array(
 
      'url' => $url,
 
      'price' => $prices[$key],
 
      'order_id' => '1'
 
   );
 
   $this->order_model->add($post_array);

Reply
#3

Thank you! worked perfect.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB