Professional CodeIgniter, Thomas Myer

Chapter 4: Creating the Main Web Site
92
Because you ' re saving the retrieved category in a variable named
$cat

, you can do a quick check of
$cat[
`
parentid
'
]

and make the right decision. If that value is less than 1, you ' re dealing with a
category page, so you have to list subcategories. If not, you ' re dealing with a subcategory page, and you
must list products.

Other things to note on this page: the
$data[
'
main
'
]

variable is set to " category. " In just a few pages,
you ' ll be creating this category view. As usual, you ' re using
$this
-
>
load
-
>
vars()

to register the
$data

array and make it available to all your views.


Creating New Model Functions

Now it is time to build some new functions. You ' ll need a function to retrieve subcategories in the MCats
model first. Here ' s one named
getSubCategories()

, accepting a category ID as an argument, which is
used to match records with the right parentid:
function getSubCategories($catid){
$data = array();
$this-
>
db-
>
where(`parentid', $catid);
$this-
>
db-
>
where(status', `active');
$this-
>
db-
>
orderby(`name','asc');
$Q = $this-
>
db-
>
get(`categories');
if ($Q-
>
num_rows()
>
0){
foreach ($Q-
>
result_array() as $row){
$data[] = $row;
}
}
$Q-
>
free_result();
return $data;
}

The second function you need to create belongs in the MProducts model. This function retrieves all
available products that match a category ID. As with the
getSubCategories()

function, you ' re going
to pass in a category ID and then order the result set by product name:
function getProductsByCategory($catid){
$data = array();
$this-
>
db-
>
select(`id,name,shortdesc,thumbnail');
$this-
>
db-
>
where(`category_id', $catid);
$this-
>
db-
>
where(status', `active');
$this-
>
db-
>
orderby(`name','asc');
$Q = $this-
>
db-
>
get(`products');
if ($Q-
>
num_rows()
>
0){
foreach ($Q-
>
result_array() as $row){
$data[] = $row;
}
}
$Q-
>
free_result();
return $data;
}
c04.indd 92
c04.indd 92
6/10/08 5:34:24 PM
6/10/08 5:34:24 PM


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

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