[eluser]Unknown[/eluser]
I'm working on a shopping cart project. I have a category table and I would like to have a dropdown with like this:
Code:
-Women
--Clothing
---Dresses
---Tops
---Knitwear
---Skirts
---Lingerie
--Accessories
---Jwellery
---Beauty & Cosmetics
---Tights and socks
-Men
--Clothing
---T-shirts & Polo shirts
---Hoodies & Sweats
---Shirts
---Jeans
---Trousers & Shorts
--Accessories
---Belts
---Ties
---Sunglasses
-Teen
Category table structure:
Code:
CREATE TABLE IF NOT EXISTS `categories` (
`catid` int(11) NOT NULL auto_increment,
`catname` varchar(128) NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`catid`)
)
data is like this:
Code:
catid catname parentid
1 Women 0
2 Men 0
3 Clothing 1
4 Accessories 1
5 Clothing 2
6 Accessories 2
7 Dresses 3
8 Tops 3
9 Knitwear 3
10 Skirts 3
11 Lingerie 3
12 Jwellery 4
13 Beauty & Cosmetics 4
14 Tights and socks 4
15 T-shirts & Polo shirts 5
16 Hoodies & Sweats 5
17 Shirts 5
18 Jeans 5
19 Trousers & Shorts 5
20 Belts 6
21 Ties 6
22 Sunglasses 6
43 Teen 0
My function is as below:
Code:
function getcategorylist($parentid, $space="") {
$this->db->where('parentid', $parentid);
$query = $this->db->get('categories');
$numrows = $query->num_rows();
if ($numrows > 0) {
$space .= "-";
foreach ($query->result_array() as $row) {
$data['catid'] = $row['catid'];
$data['catname'] = $row['catname'];
$data['dispcatname'] = $space . $row['catname'];
$data['parentid'] = $row['parentid'];
$this->getcategorylist($row['catid'], $space);
}
}
return $data;
}
When I try to echo the array value,I'm only getting the last row.
-------
But when I tried to
Code:
echo $data['catname']
after the line
Code:
$data['parentid'] = $row['parentid'];
I was able to my list. But I dont want to echo the result from a model function. I would like to store the sorted hierarchy in an array and return it to the controller and then pass it to the view.
Please help!!!