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?
you can simply get all post data using $this->input->post() and $_POST using simple php stuff.
The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.
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()
$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]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With