Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


228


$Q- > free_result();
return $data;
}

function getAssignedSizes($id){
$data = array();
$this- > db- > select(‘size_id’);
$this- > db- > where(‘product_id’,$id);
$Q = $this- > db- > get(‘products_sizes’);
if ($Q- > num_rows() > 0){
foreach ($Q- > result_array() as $row){
$data[] = $row[‘size_id’];
}
}
$Q- > free_result();
return $data;
}

Next, update the edit() function of the admin/products controller. Don ’ t forget to grab all the available
colors and sizes as well as the assigned ones. Remember that you will need both sets of data, as the first will
give you the available checkboxes, and the second set will tell you which checkboxes are actually checked.

function edit($id=0){
if ($this- > input- > post(‘name’)){
$this- > MProducts- > updateProduct();
$this- > session- > set_flashdata(‘message’,’Product updated’);
redirect(‘admin/products/index’,’refresh’);
}else{
$data[‘title’] = “Edit Product”;
$data[‘main’] = ‘admin_product_edit’;
$data[‘product’] = $this- > MProducts- > getProduct($id);
$data[‘categories’] = $this- > MCats- > getCategoriesDropDown();
$data[‘assigned_colors’] = $this- > MProducts- > getAssignedColors($id);
$data[‘assigned_sizes’] = $this- > MProducts- > getAssignedSizes($id);
$data[‘colors’] = $this- > MColors- > getActiveColors();
$data[‘sizes’] = $this- > MSizes- > getActiveSizes();
$this- > load- > vars($data);
$this- > load- > view(‘dashboard’);
}
}

Next, open the admin_product_edit view and add your fieldsets and checkboxes, just as you did on the
admin_product_create view. This time, however, you ’ re going to check for the presence of a color or size
ID in the assigned arrays you ’ ve built.

echo form_fieldset(‘Colors’);
foreach ($colors as $key = > $value){
if (in_array($key,$assigned_colors)){
$checked = TRUE;
}else{
$checked = FALSE;
}
Free download pdf