[eluser]sunnyd[/eluser]
Ok... This is the scenario I am trying to achieve. Jedd you are right in assuming that I am working with >2 levels of hierarchy.
Basically, I am wanting something like this:
Cars
------ Used Cars
------ News Cars
Models
------ Rover
------ Aston Martin
------ Mercedes Benz
This is is my current table schema:
Code:
CREATE TABLE `shop_category` (
`cat_id` int(10) unsigned not null auto_increment,
`cat_parent_id` int(11) not null default '0',
`cat_name` varchar(50) not null,
`cat_description` varchar(200) not null,
`cat_image` varchar(255) not null,
PRIMARY KEY (`cat_id`),
KEY `cat_parent_id` (`cat_parent_id`),
KEY `cat_name` (`cat_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11;
In my controller function, I am calling the model as so:
Code:
function categories()
{
$data['site_name'] = "Site Name";
$data['title'] = "Products";
$data['meta'] = array('description' => 'welcome to codeigniter',
'keywords' => 'welcome, codeigniter, ci',
);
$data['categories'] = $this->products_model->fetch_categories();
$data['content'] = $this->load->view('categories', $data , TRUE);
$this->load->view($this->template , $data);
}
In my view file, I have:
Code:
<h1>Categories</h1>
<p><?php echo anchor(base_url(). 'admin/shop/add-category', 'Add Category'); ?></p>
<p><?php echo $this->session->flashdata('message'); ?></p>
<?php foreach ($categories as $cat) { ?>
<p><?php echo $cat->cat_name; ?></p>
<?php } ?>
When trying to list the categories, I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/categories.php
Line Number: 5
When I dump the value of $categories, the result is as follows:
Code:
Array
(
[0] => Array
(
[4] => Array
(
[cat_name] => Books
[children] => Array
(
[8] => Autobiography
[7] => Fiction
[5] => Horror
[6] => Science Fiction
)
)
[1] => Array
(
[cat_name] => Cars
[children] => Array
(
[3] => Luxury
)
)
[2] => Array
(
[cat_name] => Manga
)
)
)
I am trying to setup up a drop down menu so that when a customer hovers over the main category, any subcategories will be displayed assuming there is any. But I am lost as to how write the view code to achieve the desired result. I hope I am making better sense...