[eluser]Vasi[/eluser]
Hi guys,
I am pretty new to CodeIgniter, I need to extract a category->subcategory tree from my db, the table looks like this:
id
name
parent_id
My code looks like this:
Code:
class categories extends Model{
function getCats(){
$this->db->select('id,name');
$this->db->from('categories');
$this->db->where('parent =', 0);
$q =$this->db->get();
if ($q->num_rows()>0) {
foreach($q->result() as $row) {
$id = $row->id;
$name_cat = $row->name;
echo "<h4>" . $name_cat . "<h4><ul>\n";
$sq = $this->db->query('SELECT name FROM categories WHERE parent = '.$id);
if ($sq->num_rows()>0) {
foreach($sq->result() as $row_c) {
$name_subcat = $row_c->name;
echo "<li><a href='categorie/".str_replace(" ", "-", strtolower($name_subcat))."'>".$name_subcat."</a></li>\n";
}
}
echo "</ul> \n";
$data[] = $row;
}
return $data;
}
}
}
The problem is when I echo the $data in a view, I get the code posted at the beginning of the html document, so I can't integrate it in a template.
This is my first post and I feel like I haven't explained very well, I am sorry, please let me know if you need to know tell you anything else and if you can help me.
Thank you!