$sql = "SELECT cat_id, cat_parent_id, cat_name
FROM tbl_category
ORDER BY cat_id";
$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
$categories = array();
while($row = dbFetchArray($result))
{
list($id, $parentId, $name) = $row;
if ($parentId == 0) {
// we create a new array for each top level categories
$categories[$id] = array('name' => $name, 'children' => array());
//print_r($categories);
}
else
{
// the child categories are put int the parent category's array
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
//print_r($categories);
}
}
// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name = $value['name'];
$children = $value['children'];
$list .= "<optgroup label=\"$name\">";
foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";
if ($child['id'] == $catId)
{
$list.= " selected";
}
$list .= ">{$child['name']}</option>\r\n";
}
$list .= "</optgroup>";
}
return $list;
The database like this
cat_id cat_parent_id cat_name cat_description cat_image 17 13 Hunter X Hunter Story about hunter and combat 12 0 Cars Expensive and luxurious cars dce08605333d805106217aaab7f93b95.jpg 13 0 Manga It's all about manga, yay.... 2a5d7eb60c1625144b3bd785bf70342c.jpg 14 12 Volvo Swedish luxury car 15 12 Mercedes-Benz Expensive but real good 16 13 Naruto This is the story of Naruto and all his gang 18 0 testing 123 tes
it gives me
Array ( [12] => Array ( [name] => Cars [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) ) [18] => Array ( [name] => testing 123 [children] => Array ( ) ) )
and
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
print_r($categories);
it gives me
Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) ) ) ) Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) ) )
1.How should i format the output so i can learn the results that was returned?
2.What does
$categories[$id] = array('name' => $name, 'children' => array()); and$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); means?3.What is the uses of .= ?
Thanks a lot...


Sign In
Create Account


Back to top









