CodeIgniter Forums
Inserting with arrays > A Better Way? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Inserting with arrays > A Better Way? (/thread-2561.html)



Inserting with arrays > A Better Way? - El Forum - 08-12-2007

[eluser]danfloun[/eluser]
Hi
I've recently, well 2 days ago.. started using CI.
I've got the following function in my model which is called by the controller to process a form insert.

Model:
Code:
$client_data = array(
                        'company' => $this->input->post('company'),
                        'title' => $this->input->post('title'),
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'street_address' => $this->input->post('street_address'),
                        'address' => $this->input->post('address'),
                        'town_city' => $this->input->post('town_city'),
                        'county' => $this->input->post('county'),
                        'postcode' => $this->input->post('postcode'),
                        'landline_tel' => $this->input->post('landline_tel'),
                        'mobile_tel' => $this->input->post('mobile_tel'),
                        'fax_tel' => $this->input->post('fax_tel'),
                        'email' => $this->input->post('email'),
                        'website' => $this->input->post('website'),
                        'category' => $this->input->post('category'),
                        'notes' => $this->input->post('notes')
                    );
                    
        $this->db->insert('table_clients', $client_data);

Now I'm wondering if theres a simpler way to call all the $_POST data from the form so I don't have to have all this code?

I noticed on the blog tutorial here that you can actually just use
Code:
$this->db->insert('table_clients', $_POST);

without having to write all the above code, this would just insert all fields that were passed from the form!

This is quite handy to save work but only useful if all fields in the form are being passed. When I use modify/update function I will need to use the above method I guess.

So is there any better way to process the above info or possibly a correct method as opposed to what I have done.

The code works fine by the way. Just looking at streamlining.

Cheers

Danny Da Noob

Cont....

Actually, thinking about this more.

Could I not just use
Code:
$this->db->insert('table_clients', $_POST);
or
Code:
$this->db->update('table_clients', $_POST);
and not worry about writing the above arrays?

Surely it doesn't matter about just updating the changed records, you can select to update the whole form data again... this will of course write the unchanged fields to the database aswell as the changed fields...

Anything wrong with that approach? Thats if anyone understands what I'm saying lol.

Cheers again.

Danny


Inserting with arrays > A Better Way? - El Forum - 08-12-2007

[eluser]coolfactor[/eluser]
PHP has a way to automatically create arrays from submitted form data.

Code:
<input type="text" name="info[name]" value="George" />
<input type="text" name="info[age]" value="32" />
<input type="text" name="info[phone]" value="222-2222" />

Submitting those 3 fields automatically creates an "info" sub-array within your $_POST array.

Code:
$_POST['info'] = array('name' => 'George', 'age' => '32', 'phone' => '222-2222');

So, you could pre-define your array, and then use array_merge to overlay the submitted form data.

Code:
// predefine array
$info = array('name' => '',
              'age' => '',
              'phone' => '');
// overlay $_POST
if (isset($_POST['info'])) {
    $info = array_merge($info, $_POST['info']);
}

Now $info contains everything in the predefined array, plus everything that was submitted. This ensures the fields you need for any database queries are present.


Inserting with arrays > A Better Way? - El Forum - 08-12-2007

[eluser]danfloun[/eluser]
Hi, thanks for that, though I'm not sure I explained clearly enough.

After I click the submit button on the form, all the form field data is stored in $_POST is it not!
So I'm wondering if CI has a built in function so rather than have to create a model like above, you simply use a function like,

Model:
Code:
function Update()
   {
         $this->db->update('db_table', $_POST');
   }

and thats all you would need! It would make sense to me, although I'm probably missing something here...

Danny


Inserting with arrays > A Better Way? - El Forum - 08-12-2007

[eluser]BravoAlpha[/eluser]
[quote author="danfloun" date="1186978506"]
Code:
function Update()
   {
         $this->db->update('db_table', $_POST');
   }
[/quote]
I don't know if $_POST is a good idea or not, but I would call the model like this:

Model
Code:
function update($whatever)
{
    $this->db->update('db_table', $whatever);
}

Controller
Code:
$this->something->update($POST);