Professional CodeIgniter, Thomas Myer

Chapter 4: Creating the Main Web Site
93


Building out the cat() Controller Function

Now all you have to do is run the right function in the right context and then pass the result set to a
single view. In the following code listing, the essential
if

statement is bold. Notice how the simple check
for parentid transforms this controller function into a multipurpose tool that allows you to build the
appropriate listing with the same set of views.
While you ' re at it, include a few lines of code to redirect users back to the home page if the category page
being loaded is somehow bogus. You can do that very quickly with a simple PHP
count()
function:
function cat($id){
$cat = $this-
>
MCats-
>
getCategory($id);
if (!count($cat)){
redirect(`welcome/index','refresh');
}
$data[`title'] = "Claudia's Kids | ". $cat[`name'];
if ($cat[`parentid']
<
1){
//show other categories
$data[`listing'] = $this-
>
MCats-
>
getSubCategories($id);
$data[`level'] = 1;
}else{
//show products
$data[`listing'] = $this-
>
MProducts-
>
getProductsByCategory($id);
$data[`level'] = 2;
}
$data[`category'] = $cat;
$data[`main'] = `category';
$data[`navlist'] = $this-
>
MCats-
>
getCategoriesNav();
$this-
>
load-
>
vars($data);
$this-
>
load-
>
view(`template');
}


Creating the Category View

Before coding the category view, it would be worth the time to revisit the diagrams for the category and
subcategory views developed with the client. Here is the category view, illustrated in Figure 4 - 6 .
c04.indd 93
c04.indd 93
6/10/08 5:34:24 PM
6/10/08 5:34:24 PM


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.