Chapter 7: Improving the Dashboard
192
Here ’ s a simple batchmode() function that uses print_r() to reveal the structure and content of the
incoming POST data:
function batchmode(){
echo “ < pre > ”;
print_r($_POST);
echo “ < /pre > ”;
}
If you go back to the form, enter some test data, check a few boxes, and click the Submit button, you
might see the following array on the screen:
Array
(
[category_id] = > 1
[grouping] = > testing
[submit] = > batch update
[p_id] = > Array
(
[0] = > 1
[1] = > 2
[2] = > 3
)
)
In other words, when you eventually pass along this array to the MProducts model, you ’ re going to give
it a distinct category ID, a grouping, and an array of product IDs.
Keeping all of that in mind, rewrite the batchmode() function like so:
function batchmode(){
$this- > MProducts- > batchUpdate();
redirect(‘admin/products/index’,’refresh’);
}
All you have to do now is create the batchUpdate() function in the MProducts model. The first thing
this function must do is make sure that there are actually products to update. You can do that easily
enough by using count() against the incoming p_id array.
If your count comes up 0, then use flashdata() to set an appropriate message, but don ’ t actually
perform any database updates.
function batchUpdate(){
if (count($this- > input- > post(‘p_id’))){
}else{
$this- > session- > set_flashdata(‘message’, ‘Nothing to update!’);
}
}
If there are incoming product IDs to work with, you want to create a $data array that will hold the
category_id and grouping data from the form. Then you want to extract the values of the p_id array