Welcome Guest, Not a member yet? Register   Sign In
Form With Multiple Fields [Array] Query
#1

(This post was last modified: 07-11-2016, 03:38 AM by MoFish.)

Hi All,

I have some form fields which can be repeated multiple multiple times within my form.

For example, the same two fields, which are repeating 5 times within my form.

I'm a little unclear on the best way to process multiple form fields data so they can be saved back into the DB.

If anyone could shed anylight on this? it would be much appriciated.

My form has a couple fields.

View:

PHP Code:
$data = array('name'=>'keyword_meta_title[]''id' => 'keyword_meta_title');
echo 
form_input($data);

$data = array('name'=>'keyword_meta_description[]''id' => 'keyword_meta_description');
echo 
form_input($data); 

I'm trying to loop around the form fields and create a $data array with the relevant information for each of the fields which i will then pass into my update statement.

Controller

PHP Code:
if($this->form_validation->run() === TRUE){ 
 
   // not sure if this is correct, as its only looking at one field, but should get the correct array count?
 
   $array $this->input->post('keyword_meta_title');
 
   foreach($array as $arr => $k){
 
       $data = array(
 
            'keyword_meta_title' => $this->input->post('keyword_meta_title'),
 
            'keyword_meta_description' => $this->input->post('keyword_meta_description')
 
       ); 
 
       $this->website_model->update_keywords($id$data);
 
   
 
   $this->alert("Website updated successfully""success");
 
   redirect('websites/index''refresh');


Thanks,

MoFish
Reply
#2

If you have multiple title/description pairs which you want to loop through and pass as an array to your update_keywords method, you probably need to do something like this:

PHP Code:
// Note: you should probably make sure the $titles and $descriptions contain the
// same number of items, or handle the conditions in which either is larger than
// the other, but we won't do that here for the sake of simplicity.

$titles $this->input->post('keyword_meta_title');
$descriptions $this->input->post('keyword_meta_description');
$max count($titles);
for (
$count 0$count $max$count++) {
    
$data = array(
        
'keyword_meta_title' => $titles[$count],
        
'keyword_meta_description' => $descriptions[$count],
    );
    
$this->website_model->update_keywords($id$data);


As a further optimization, I would normally do the update outside of the loop, but that requires creating an array of entries that can be passed to a batch update method in the model, which requires some more significant changes, including a method in the model to handle a batch update.
Reply
#3

Thank you for the reply mwhitney, i'll give this a go later on tonight and let you know how I get on.
Reply
#4

Hi,

It's working to some extent, but i dont think i have the correct ID for each individual form field im retreiving from the db.

Is there a way I can pass the ID from the view back into my controller?

e.g 'name'=>'keyword_meta_title[][ID]?

Then in my controller use this ID inside my update statement?

See my code below, and a comment on the area which i beleive is the issue.

PHP Code:
if($this->form_validation->run() === TRUE){ 
 
 
$keyword_meta_title $this->input->post('keyword_meta_title');
 
$keyword_meta_description $this->input->post('keyword_meta_description');
 
$keyword_meta_keywords $this->input->post('keyword_meta_description');
 
$keyword_slug $this->input->post('keyword_meta_description');
 
$keyword_heading $this->input->post('keyword_meta_description');
 
$keyword_content $this->input->post('keyword_meta_description');
 
 
$max count($keyword_meta_title);

 for (
$count=0$count $max$count++) {
 
$data = array(
 
'keyword_meta_title' => $keyword_meta_title[$count],
 
   'keyword_meta_description' => $keyword_meta_description[$count],
 
'keyword_meta_keywords' => $keyword_meta_keywords[$count],
 
'keyword_slug' => $keyword_slug[$count],
 
'keyword_heading' => $keyword_heading[$count],
 
'keyword_content' => $keyword_content[$count]
 ); 
 
                               // this id i think is incorrect
 
                              // i think i need to pull the id from the input field if possible?
 
$this->website_model->update_keywords($id$data);
 }
 
   $this->alert("Website updated successfully""success");
 } 
Reply
#5

Hi,

I used a hidden field to pass the ID back and this worked OK. Not the ideal solution but works fine. @mwhitney, i'm always for futher optimization! If you wouldn't mind, would you be able to give me an example of how I would go about creating an array of entries which i can then pass into the batch_update inside my model?

Regards,

MoFish
Reply
#6

To do the update as a batch, you would do something like this:

PHP Code:
$data = array();

for (
$count 0$count $max$count++) {
    
$data[] = array(
        
'id' => $id[$count],
        
'keyword_meta_title' => $keyword_meta_title[$count],
        
'keyword_meta_description' => $keyword_meta_description[$count],
        
'keyword_meta_keywords' => $keyword_meta_keywords[$count],
        
'keyword_slug' => $keyword_slug[$count],
        
'keyword_heading' => $keyword_heading[$count],
        
'keyword_content' => $keyword_content[$count]
    );
}

$this->website_model->batch_update_keywords($data); 

This assumes that $id is an array of ID values which can be used to identify the data to be updated in the batch update. Essentially, as you indicated, this would be a hidden field using an array for the name just like the other fields in the form.

As you can see, $data is defined as an empty array before the for loop starts, then each iteration of the loop appends an array to $data (rather than redefining $data), so you have a 2-dimensional array, similar to the output of result_array() when you're working with a database query. Then the batch_update_keywords() method in the model just has to perform a batch update, which is relatively easy using query builder's update_batch() method (especially since we've just created $data in the format required by this method). Query builder's update_batch() just needs the table name, $data, and the name of the key ('id' in the example above).
Reply
#7

Thank you very much mwhitney for taking the time to explain.

It worked a treat and is much cleaner than before! thanks again!

MoFish
Reply




Theme © iAndrew 2016 - Forum software by © MyBB