Welcome Guest, Not a member yet? Register   Sign In
How to insert multidimensional input array using Active Records?
#1

[eluser]Helmi_xisnet[/eluser]
Hi all,

This is the code in my view file:
Code:
<p>
  &lt;input type="text" name="fullname[]" id="fullname[]" /&gt;
  -
  &lt;input type="text" name="job_title[]" id="job_title[]" /&gt;
</p>
<p>
  &lt;input type="text" name="fullname[]" id="fullname[]" /&gt;
  -
  &lt;input type="text" name="job_title[]" id="job_title[]" /&gt;
</p>

In the real application, these rows are dynamically added/remove by user.

This is my code in my controller (just the sql part):
Code:
for($i = 0, $t = count($_POST['fullname']); $i < $t; $i++) {
   $this->applicant->insert_list($i);
}

and this is the model:
Code:
function insert_list($i) {
            $data = array(
                        //'fullname' => $this->input->post('fullname[]'),
                        //'job' => $this->input->post('job_title[]')
                        'fullname' => $_POST['fullname'][$i],
                        'job' => $_POST['job_title'][$i]
            );
            $this->db->insert('my_table',$data);
    }

Now in the model file; if I use $_post[][$i]; it works as expected. What is the equivalent code for $this->input->post?
#2

[eluser]Sumon[/eluser]
have you tried with
Code:
function insert_list($i) {
            $data = array(
                        'fullname' => $this->input->post("fullname[$i]"),
                        'job' => $this->input->post("job_title[$i]")
            );
            $this->db->insert('my_table',$data);
    }
#3

[eluser]Helmi_xisnet[/eluser]
Yes but not successful. The expected values became '0' instead.
#4

[eluser]Matthieu Fauveau[/eluser]
There is no equivalent code that I'm aware of.
I believe you should do it like that instead :

Code:
function insert_list($i)
{
$fullname = $this->input->post('fullname');
$job_title = $this->input->post('job_title');

$data = array(
    'fullname' => $fullname[$i],
    'job' => $job_title[$i]
);

$this->db->insert('my_table',$data);
}
#5

[eluser]Helmi_xisnet[/eluser]
Thanx Matthieu. Your code works beautifully.
#6

[eluser]opel[/eluser]
Wish I'd found this last night, spent all night trying to solve the same problem.

Did you put validation on to make sure that a job title couldn't be inserted without a fullname, if so how did you do it ?

Cheers
#7

[eluser]zool2005[/eluser]
Amazingly simplistic but worked like a charm !

I spent 3 days trying to work out how to insert similar data into my db using CI.

One of the last pieces in my puzzle....

Thank you !




Theme © iAndrew 2016 - Forum software by © MyBB