Professional CodeIgniter

(singke) #1

Chapter 7: Improving the Dashboard


210


Reworking Colors and Sizes for Products


Reworking the way you ’ re handling colors and sizes takes quite a bit of work. Ideally, you want to avoid
these kinds of changes, but sometimes it is inevitable. This change has been left in here on purpose to
show you how to handle this kind of situation in real life.

As you ’ ll see, there ’ s no need to panic, because CodeIgniter is flexible enough to let you rework just
about any change, no matter how complex. Before you jump in, though, it ’ s definitely worth the time to
write out a list of all the changes that need to be made.

You will need to :


  1. Create two new database tables, one for colors and the other for sizes. A good suggestion is to
    keep them simple, with ID, name, and status fields. You will also need mapping tables to store
    individual settings for each product.

  2. Delete any references to the old product color/size fields. (Don ’ t forget the MProducts model!)

  3. Create two new models for your new tables, with simple functions that allow you to retrieve,
    add, edit, and deactivate colors and sizes.

  4. Create the administrative functions for both colors and sizes.

  5. Add color and size checkboxes to the product create/edit views.

  6. Use the information in the mapping tables to display color and size data on the public side of
    the Shopping Cart.


Let ’ s get to work.

Creating New Database Tables


You need to create four tables: colors, sizes, products_colors, and products_sizes. The first table, colors,
has three fields: ID, name, and status.

CREATE TABLE ‘colors’ (
‘id’ INT NOT NULL AUTO_INCREMENT ,
‘name’ VARCHAR( 32 ) NOT NULL ,
‘status’ ENUM( ‘active’, ‘inactive’ ) NOT NULL ,
PRIMARY KEY ( ‘id’ )
);

The sizes table is almost identical, except it has a different name:

CREATE TABLE ‘sizes’ (
‘id’ INT NOT NULL AUTO_INCREMENT ,
‘name’ VARCHAR( 32 ) NOT NULL ,
‘status’ ENUM( ‘active’, ‘inactive’ ) NOT NULL ,
PRIMARY KEY ( ‘id’ )
);
Free download pdf