Welcome Guest, Not a member yet? Register   Sign In
Inserting with arrays > A Better Way?
#1

[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


Messages In This Thread
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 04:44 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 04:53 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 05:15 PM
Inserting with arrays > A Better Way? - by El Forum - 08-12-2007, 05:30 PM



Theme © iAndrew 2016 - Forum software by © MyBB