Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get post data into an array in CodeIgniter? post items are arrays

In CodeIgniter, I'm trying to accomplish a batch update from form inputs that share the same name. But don't know how to get post data into an array. A simplified view of the form is as follows:

<input name="id[]" value="1"/><input name = title[] value="some-title"/><input name     ="sort_order[]" value="1"/>

<input name="id[]" value="2"/><input name = title[] value="some-tuttle"/><input name="sort_order[]" value="2"/>

<input name="id[]" value="3"/><input name = title[] value="some-turtle"/><input name="sort_order[]" value="3"/>

In my controller I have this for now:

function set_sort_order(){
    $data = array(
        array('id' => 1,'sort_order' => 14),
        array('id' => 2,'sort_order' => 5),
        array('id' => 3,'sort_order' => 9)
    );
    $this->db->update_batch('press_releases', $data, 'id');//works!
    $this->load->view(pr_listing);
}

The array is hard-wired to test in the input_batch function, which is working. So how can I get the post data into an array?

like image 604
TopTomato Avatar asked Nov 29 '13 20:11

TopTomato


People also ask

How do I get post data in CI?

you can simply get all post data using $this->input->post() and $_POST using simple php stuff.

Is $_ POST an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

What is getVar in CodeIgniter?

The getVar() method will pull from $_REQUEST, so will return any data from $_GET, $POST, or $_COOKIE. While this is convenient, you will often need to use a more specific method, like: $request->getGet()


1 Answers

$id = $this->input->post('id');
$sort_order = $this->input->post('sort_order');
$data = array();
foreach($id as $key=>$val)
{
  $data[] = array('id'=>$val,'sort_order'=>$sort_order[$key]);
}
like image 127
Naveed Hasan Avatar answered Sep 19 '22 15:09

Naveed Hasan