Professional CodeIgniter

(singke) #1

Chapter 5: Building a Shopping Cart


133


Figure 5-10

Fixing the Side Navigation


The side navigation work will be similar to the global navigation. You need to rework the CSS to show
two levels of category, but in order to do that, you need to extract two layers of category from your
model and process them all correctly in your view.

Updating the model isn ’ t a problem. You already have a good working function in the MCats model
called getCategoriesNav(). All you have to do to make it output the categories properly is add a new
order by clause (making the list come out in asc parentid order) as well as an asc name order clause, and
add a group by clause to group the database output by parentid and ID.

As you loop through each record, check to see if the parentid extracted is greater than 0. If it is, then save
the resulting ID and name in an array that is a child of that parentid. If the parentid is not greater than 0,
save just the name, using the ID as the parentid. Key the entire array by 0 (which is the topmost number
allowed in the hierarchy), and you have a multidimensional array that represents the category tree.
Free download pdf