Blaze Tama
Blaze Tama

Reputation: 10948

post an array and iterate throught it in PHP codeigniter

This is the first time i create my own webservice (someone always did it for me before), so please bear with me.

I post this array :

    $data = array(
        'user_id' => $this->post('user_id'),
        'group_id' => $this->post('group_id'),
        'child_id' => $this->post('child_id'), //will be nested array
        'custom' =>  $this->post('custom'),
        'time' => $this->post('time'),
        'date' => $this->post('date')
    );

I tried to create a nested array with this : $this->post('child_id'), because user can post multiple child_id at once.

Then i tried to iterate through the child_id, because i need to insert them to the mysql :

for($i = 0; $i < sizeof($data['child_id']); $i++)
            {
                $result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
            }

What should i do, so i can have an array of child_id in my $data array? (nested array)

And how to iterate through it?

UPDATE : I have updated the codes above.

I use advanced rest client for testing, and i tried to post something like this in the form content type :

child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2

Notice that theres two child_id (left most and right most), but only the last one (right most) is inserted.

And this is the add_trans in the model :

function add_trans($table, $data, $schedule_id) {
    $query = $this->db->insert($table, array('child_id' => $data['child_id'], 'schedule_id' => $schedule_id));
    return $query;
}

Thanks a lot for your time.

Upvotes: 1

Views: 68

Answers (3)

Lokesh Jain
Lokesh Jain

Reputation: 579

you don't need to give child[] in post method. just give only child, it will get complete array what are you sending from views

replace

 'child_id' => $this->post('child_id[]')

with

'child_id' => $this->post('child_id')

Upvotes: 1

Kevin
Kevin

Reputation: 41903

Even thought you set the name attribute as child[] on the markup,

You still need to call it as:

'child_id' => $this->post('child_id')

It will still return an array.

for($i = 0; $i < sizeof($data['child_id']); $i++) {
    $result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
}

EDIT:

Looking upon you query string, that seems to be the culprit:

child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2
 ^ same index , same index, same index, it will overwrite and you will get only `2`

If you want to get them all into an array format, you need to set them like this

child_id[]=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id[]=2
        ^ it needs to be set like this   

UPDATE:

And in your model, if you want each id per row, well you can also loop in this case:

function add_trans($table, $data, $schedule_id) {
    foreach($data['child_id'] as $child_id) {
        $query = $this->db->insert($table, array('child_id' => $child_id, 'schedule_id' => $schedule_id));  
    }

    // return $this->db->insert_id();
    return $query;
}

Upvotes: 1

DarkMukke
DarkMukke

Reputation: 2489

ofcourse that won't work, it has to be

for($i = 0; $i < sizeof($data['child_id']); $i++)
{
      $result2 = $this->schedule_m->add_trans('transaction_schedule', $data['child_id'][$i], $result_id[0]['id']);
}

because you've not set $data['child_id[]'] so it doesn't exist, the key is just a string or number, it does not validate or parse anything

Upvotes: 1

Related Questions