[eluser]roadie[/eluser]
Hi guys!
I've been trying for a while to figure out what's best way to retrieve a Category /Subcategory/ Items tree.
After a long search on the forum I found this:
categories_model:
Code:
function get_category($id) //get the Main Category or the root of the tree
{
$query = $this->db->get_where('categories', array('id'=>$id));
$result = $query->row();
return $result;
}
function get_overview($iStart)// get the Top Categories for a selected Main Category
{
$this->db->where('parent_id', $iStart);
$query = $this->db->get('categories');
return $query->result();
}
function get_category_array($iStart)
{
// Init
$categories = array();
// Call database
$category_overview = $this->get_overview($iStart);
// For all categories found
foreach ( $category_overview as $key => $value ) {
// create new category item and fill it up
$category = array();
$category['id'] = $category_overview[$key]->id;
$category['name'] = $category_overview[$key]->name;
//$aCategory['parentid'] = $aCategorieOverview[$sKey]->parent_id;
// Get childs if there are any (recusive) and add them to this level
$sub_categories = $this->get_category_array($category['id']);
if ( !empty($sub_categories) ) {
$category['sub_categories'] = $sub_categories;
}
// Add category to the categories array
$categories[] = $category;
}
// Return the results
return $categories;
}
controller:
Code:
function categories($id)
{
$data['main'] = 'category_view';
$category = $this->categories_model->get_category($id);
$data['title'] = $category->name;
$data['category'] = $category;
$data['tree'] = $this->categories_model->get_category_array($id);
$this->load->view('main_template_view', $data);
}
category_view:
Code:
<div>
<h2><?php echo $category->name;?></h2>
<?php echo $category->description;?>
<pre><?php print_r($tree);?></pre>
</div>
The print_r($tree)gives me a very nice tree:
Code:
<h2>Main Category</h2>
<p>Description of Main Category</p>
Array
(
[0] => Array
(
[id] => 6
[name] => Top Category 1
[sub_categories] => Array
(
[0] => Array
(
[id] => 7
[name] => Sub category
)
)
)
[1] => Array
(
[id] => 8
[name] => Top Category 2
)
[2] => Array
(
[id] => 9
[name] => Top Category 3
)
)
My db tables are:
Code:
categories:
|id|name|description|type|parent_id|status| //type = enum('business','general')
articles:
|id|title|content|parent_id|status|
companies:
|id|name|description|content|parent_id|status|
A leaf category of type='business' is parent of "companies"
A leaf category of type='general' is parent of "articles"
My problems are:
1. How can I show the companies/ articles in the tree also?
2. Some categories don't have subcategories. How can I retrieve the 'articles'? There is only one category type='business' and only this one has 'companies' children.
3. How can I pass the $tree to the view?
This is what I want to achieve:
Code:
<h2>Main Category</h2>
<p>Main Category description....</p>
//the leafs are subcategories
<div>
<h3><a href=".../categories"><?php echo top_category1->name;?></a></h3>
<ul>
<li><a href=".../categories"><?php echo sub_category1->name;?></a></li>
<li><a href=".../categories"><?php echo sub_category2->name;?></a></li>
<li><a href=".../categories"><?php echo sub_category3->name;?></a></li>
</ul>
</div>
<div>
<h3><a href=".../categories"><?php echo top_categoryX->name;?></a></h3>
<ul>
<li><a href=".../categories"><?php echo sub_category1->name;?></a></li>
<li><a href=".../categories"><?php echo sub_category2->name;?></a></li>
<li><a href=".../categories"><?php echo sub_category3->name;?></a></li>
</ul>
</div>
And then:
Code:
<h2>Top Category 1</h2>
<p>Top Category1 description....</p>
//the leafs are "companies"
<div>
<h3><a href=".../categories"><?php echo sub_category1->name;?></a></h3>
<ul>
<li><a href=".../categories"><?php echo company1->name;?></a></li>
<li><a href=".../categories"><?php echo company2->name;?></a></li>
<li><a href=".../categories"><?php echo company3->name;?></a></li>
</ul>
</div>
<div>
<h3><a href=".../categories"><?php echo sub_categoryX->name;?></a></h3>
<ul>
<li><a href=".../categories"><?php echo company1->name;?></a></li>
<li><a href=".../categories"><?php echo company2->name;?></a></li>
<li><a href=".../categories"><?php echo company3->name;?></a></li>
</ul>
</div>
And respectively:
Code:
<h2>Main Category X</h2>
<p>Main Category X description....</p>
//Its a Main Category type="general" without subcategories. The leafs are "articles"
<div>
<ul>
<li><a href=".../articles"><?php echo articles1->title;?></a></li>
<li><a href=".../articles"><?php echo articles2->title;?></a></li>
<li><a href=".../articles"><?php echo articles3->title;?></a></li>
</ul>
</div>
Sorry guys, there are lots of questions but I'd really appreciate your help!
Roadie