0

I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, I am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).

The snippets below:

$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);

This returns only last stored array value in Citizenship.

The output array:

Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 ) 

makes the query string as:

&gender=1&minage=18&maxage=24&Citizenship=2

But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.

The output required to make query string:

Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 ) 

The query string :

&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
1

2 Answers 2

0

Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.

If you can access raw $_POST data try replacing

$this->input->post('citizenship')

with

array_map('intval',$_POST['citizenship'])

Alternativly add keys to your post data:

&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
Sign up to request clarification or add additional context in comments.

Comments

0

I fixed it myself. I just looped through the POST array and got the individual array key & pair values.

foreach($_POST['Citizenship'] as $k => $v) {
    $Citizenship[$v] = $v;
}

Hope this helps someone who face similar problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.