When you click one of categories page, it will take you to a category page. It is pretty much same as the front page except it shows all the products in the category.
Download Page
The original version has the multi-level categories menu.
I modified it to a single level. It sounds like a bit of backwards, but I started this modification with to have more than one website in mind. And each website needs categories. So when you create a new category in the back-end you will have a drop-down to choose one of the root categories. We can have one for the webshop and we can have another one for the main website etc.
If you see the Shop_controller.php you find the following line.
// left category menu // this is ugly too. Same as above. $parentid=1; $this->data['navlist'] = $this->MCats->getCatNav($parentid);

As I commented it, I know it’s ugly and I will change it soon, but for now it serves our needs.
If you see the id of webshop in the image on the left (click to enlarge), you will see ’1′. This ’1′ is used for $parentid.
Sample data
Please dump the following to your db.
INSERT INTO `omc_category` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) VALUES (102, 'Digital Downloads', '<p>Short Description</p>', '<p>Long Description</p>', 'active', 1), (101, 'Movies, Music & Games', '<p>Short Description</p>', '<p>Long Description</p>', 'active', 1), (99, 'Books', '<p>Here you can write a short description of books category.</p>', '<p>Here you can write a long description of books category.</p>', 'active', 1), (1, 'Webshop', '', '', 'active', 0), (100, 'CD', '<p>Here you can write a short description of this category.</p>', '<p>Here you can write a long description of this category.</p>', 'active', 1), (103, 'Computers & Office', '<p>Short Description</p>', '<p>Long Description</p>', 'active', 1), (104, 'Electronics', '<p>Short Description</p>', '<p>Long Description</p>', 'active', 1);
Updating controller
We add the following in module/webshop/controllers/webshop.php
function cat($id){
$cat = $this->MCats->getCategory($id);
/**
* $id is the third(3) in URI which represents the ID and any
* variables that will be passed to the controller.
*/
if (!count($cat)){
// if there is no such a category id, then redirect.
redirect( $this->lang->line('webshop_folder').'/index','refresh');
}
$data['title'] = $this->lang->line('webshop_shop_name')." | ". $cat['name'];
if ($cat['parentid'] < 1){
/**
* This must be a root category, so show children/other Categories
*/
$data['listing'] = $this->MCats->getSubCategories($id);
/**
* this will receive a series of array with id, name, shortdesc and thumbnail
* and store them in listing. Array ([0]=>array([id]=>14 [name]=>long-sleeve...))
*/
$data['level'] = 1;
}else{
// otherwise, it must be a category, so let's show products
$data['listing'] = $this->MProducts->getProductsByCategory($id);
// this will receive a series of product with array.id,name,shortdesc,thumbnail
$data['level'] = 2;
}
$data['category'] = $cat;
$data['page'] = $this->config->item('backendpro_template_shop') . 'category';
$data['module'] = $this->lang->line('webshop_folder');
$this->load->view($this->_container,$data);
}
Each category has the third segment which is an id of the category.
This line,
$cat = $this->MCats->getCategory($id);
uses this $id to get data by using MCats which is a model of categories module.
If there is no data then we redirect to index page.
$cat has category id, name, shortdesc, longdesc, status and parentid. Please have a look at the structure of omc_category.
The next line is from the original code. As I stated it before the original code had a multi-level of categories menu. $data['level'] = 1; is for a categories which have sub-categories and $data['level'] = 2; for categories which has products.
If the parentid of the category data is less than 0, which means it is one of root categories. In that case it shows all the subcategories.
Otherwise it must be one of categories, so it collects all the product data with this line.
$data['listing'] = $this->MProducts->getProductsByCategory($id);
$listing in view will have all the data from products within the category.
I am not using it for this webshop, but it is there for you to use if you wish.
Views category.php
module/webshop/views/shop/category.php is used to show this. It has
<div id='pleft'>
<?php
echo "<h1>".$category['name']."</h1>\n";
echo "<p>".$category['shortdesc'] . "</p>\n";
foreach ($listing as $key => $list){
echo "<div class='productlisting'>";
switch($level){
// category level is 1, and product is 2
// see function cat($id) in controllers/.php
case "1":
echo '<a href="' . site_url(). '/'.$this->lang->line('webshop_folder').'/cat/'.$list['id']. '">';
echo '<img src="'.base_url().$list['thumbnail'].'"'. "border='0' class='thumbnail'/>\n";
echo "</a><br />";
echo "<span class='hdrproduct'>";
echo anchor('/cat/'.$list['id'],$list['name']);
echo "</span>\n";
break;
case "2":
echo '<a href="' . site_url(). '/'. $this->lang->line('webshop_folder').'/product/'.$list['id']. '">';
echo '<img src="'.base_url().$list['thumbnail'].'"'. "border='0' class='thumbnail'/>\n";
echo "</a><br />";
echo "<span class='hdrproduct'>";
echo anchor($this->lang->line('webshop_folder').'/product/'.$list['id'],$list['name']);
echo "</span>\n";
// echo ;
break;
}
echo $list['shortdesc'];
echo "<p><b>".$this->lang->line('webshop_price')."</b>: ".$this->lang->line('webshop_currency_symbol'). $list['price']. "</p>\n";
echo '<a href="' . site_url(). '/'.$this->lang->line('webshop_folder').'/cart/'.$list['id']. '"><p class="addtocart">'.$this->lang->line('webshop_buy').'</p></a></div>';
}
?>
</div>
This view shows all of products within the category. Each product has it’s thumbnail, name, short description and price. id is used for a link to individual product and also for BUY link as well.
case 1 and case 2 is from the previous version and if you wish to show subcategories, you can use this to show them.
The controller has this function in-built.
When you click one of product image or name, it will take you to an individual product page.































[...] Codeigniter shopping cart v1.1 Part 11: category page [...]
Dude you rock!!
Thank you
[...] Codeigniter shopping cart v1.1 Part 11: category page [...]