Professional CodeIgniter, Thomas Myer

Chapter 5: Building a Shopping Cart
134
function getCategoriesNav(){
$data = array();
$this-
>
db-
>
select(`id,name,parentid');
$this-
>
db-
>
where(`status', `active');
$this-
>
db-
>
orderby(`parentid','asc');
$this-
>
db-
>
orderby(`name','asc');
$this-
>
db-
>
groupby(`parentid,id');
$Q = $this-
>
db-
>
get(`categories');
if ($Q-
>
num_rows()
>
0){
foreach ($Q-
>
result() as $row){
if ($row-
>
parentid
>
0){
$data[0][$row-
>
parentid][`children'][$row-
>
id] = $row-
>
name;
}else{
$data[0][$row-
>
id][`name'] = $row-
>
name;
}
}
}
$Q-
>
free_result();
return $data;
}

Running a
print_r()

on the data from this function reveals the structure of your new navigation array.
As you can see, everything is neatly packed away in three levels. First there is level 0, or the top of the
tree. Then come levels 7 and 8 (the categories " clothes " and " fun " ), each with their own children.
The children each have their own IDs and names stored properly.
Array
(
[0] =
>
Array
(
[7] =
>
Array
(
[name] =
>
clothes
[children] =
>
Array
(
[4] =
>
dresses
[3] =
>
pants
[2] =
>
shirts
[1] =
>
shoes
)
)
[8] =
>
Array
(
[name] =
>
fun
[children] =
>
Array
(
[6] =
>
games
[5] =
>
toys
)
)
)
)
c05.indd 134
c05.indd 134
6/10/08 5:35:25 PM
6/10/08 5:35:25 PM


Другие страницы

 
Cкачать книги бесплатно без регистрации в электронном виде (pdf, chm, txt).Вы можете читать книги онлайн на нашем сайте литературного портала книг.Большая подборка учебников, пособий, интересных книг.Электронные книги на английском языке скачать бесплатно без смс.