Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


230


The next thing you need to do is to delete any color and size mappings already in the products_colors
and products_sizes tables for a given product and then repopulate those data from the newly checked
color and size choices.

Why do it that way? Because it ’ s so much easier (not to mention more efficient) to clean the slate and add
new values than to keep track of every item, doing updates as needed or deleting extraneous items that
don ’ t match the previous state.

You can delete a given set of color and size settings with the following commands:

$this- > db- > where(‘product_id’, $_POST[‘id’]);
$this- > db- > delete(‘products_colors’);
$this- > db- > where(‘product_id’, $_POST[‘id’]);
$this- > db- > delete(‘products_sizes’);

Now that you ’ ve deleted all the existing settings, repopulate from the incoming checkboxes. Please note
that if there are no checkboxes for color or size, you ’ re OK! Since you ’ ve deleted all the previous entries
made, it is theoretically possible to have a product without color or size attributes.

if (count($_POST[‘colors’])){
foreach ($_POST[‘colors’] as $value){
$data = array(‘product_id’ = > $_POST[‘id’],
‘color_id’ = > $value);
$this- > db- > insert(‘products_colors’,$data);
}
}

if (count($_POST[‘sizes’])){
foreach ($_POST[‘sizes’] as $value){
$data = array(‘product_id’ = > $_POST[‘id’],
‘size_id’ = > $value);
$this- > db- > insert(‘products_sizes’,$data);
}
}

Your work on the admin side is now complete. All that ’ s left to do is to add color and size assignments to
the public pages.

Displaying Color and Size Information on Public Pages


To display color and size information on the public pages, all you have to do is open the welcome
controller in an editor and make some small changes to the product() function. These changes are
pretty much the same lines you added to the edit() function of the admin/products controller.

function product($productid){
$product = $this- > MProducts- > getProduct($productid);
if (!count($product)){
redirect(‘welcome/index’,’refresh’);
}
$data[‘grouplist’] = $this- > MProducts- > getProductsByGroup(3,$product[‘grouping’],
$productid);
$data[‘product’] = $product;
Free download pdf