karthikanov24 Posted August 25, 2009 Share Posted August 25, 2009 Hi, In the following codes,i could not understand these lines.. $categories[$id] = array('name' => $name, 'children' => array()); $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); $name = $value['name']; $children = $value['children']; Could u explain me clearly with an example.....? function buildCategoryOptions($catId = 0) { $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()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<option value=\"$key\""; if ($key == $catId) { $list.= " selected"; } Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/ Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } This code creates an array for every top-level category (every record in your database that a 0 as a parent_id). Which will give you something like: Cat 1 Cat 2 Cat 3 else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } This code retrieves the earlier set $id (== $parentId) and fills the 'children' => array() part with the $id and name of the child categories. Which will give you something like: Cat 1 Cat 1.1 Cat 1.2 Cat 1.3 Cat 2 .. But it is also possible that a child becomes a parent like: Cat 1 (parent of 1.1 and ancestor of 1.1.1) Cat 1.1 (child of 1) Cat 1.1.1 (child of 1.1 and descendant of 1) Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-905655 Share on other sites More sharing options...
karthikanov24 Posted August 25, 2009 Author Share Posted August 25, 2009 hi What are the keys and values of $categories and what is stored in $name variable and $children variable ....in the following lines of the above posted codes... foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-905683 Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $name is the name of the category and $children are the children of the parent category presumably they afterwards create another foreach loop going over the children categories. Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-905703 Share on other sites More sharing options...
watsmyname Posted August 25, 2009 Share Posted August 25, 2009 foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $name is the name of the category and $children are the children of the parent category presumably they afterwards create another foreach loop going over the children categories. - write a query to fetch all categories and their id - in html use <optgroup>code to display categories here</optgroup>...after you display categories, inside optgroup write a query to fetch products related to that category..and use <option>//code to display products</option> Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-905706 Share on other sites More sharing options...
karthikanov24 Posted August 29, 2009 Author Share Posted August 29, 2009 hi if i am typing the following codes to see the array output.. print_r $categories; print_r $categories[$id]; at the above posted codings as like this...i am not getting the out put..so how can i see those array output..? if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } print_r $categories; print_r $categories[$id]; Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-908904 Share on other sites More sharing options...
ignace Posted August 29, 2009 Share Posted August 29, 2009 print_r($categories); print_r($categories[$id]); Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-908910 Share on other sites More sharing options...
karthikanov24 Posted August 29, 2009 Author Share Posted August 29, 2009 hi if i am trying to print this print_r ($categories[$parentId]['children']); in the above posted code..i get the output as: Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) Here 'Naruto' and 'Hunter x Hunter' are the two sub categories(children) of the 'Manga' category But I have other 2 sub categories(children) 'volvo','mercedes benz' for cars(first level category) which is not displayed here..when running the file. can u say me the reason and how to display it.... Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-908918 Share on other sites More sharing options...
ignace Posted August 29, 2009 Share Posted August 29, 2009 can u say me the reason and how to display it.... Because you prolly did the print_r() outside of the loop whereby you ask for the children of the last assigned $parentId Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-908944 Share on other sites More sharing options...
karthikanov24 Posted September 4, 2009 Author Share Posted September 4, 2009 hi In the following code, $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); at the above posting.. what is this [] and what is stored in it..? Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-912480 Share on other sites More sharing options...
ignace Posted September 5, 2009 Share Posted September 5, 2009 It contains 'children' if you would print $categories you would get: Array ( $parentId => Array ( 'children' => Array ( 0 => Array ( 'id' => $id, 'name' => $name), 1 => Array ( 'id' => $id, 'name' => $name), .. ) ) ) I used $parentId, $id and $name these would translate to what they hold at that time ofcourse. Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-913106 Share on other sites More sharing options...
karthikanov24 Posted September 8, 2009 Author Share Posted September 8, 2009 hi In the following codes, 1.I tried to echo $children;below the following line of code $children=$value['children']; echo $children; it gives output as"array array".whats the reason? How to see the output of $children ? if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<option value=\"$key\""; if ($key == $catId) { $list.= " selected"; } $list .= ">$name</option>\r\n"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= "> {$child['name']}</option>\r\n"; } } return $list; } 2. <optgroup>and </optgroup> displays -cat1 and cat 2 How the children categories cat.1 ,cat1.2,cat .1,cat2.2) in <option></option> is matched to categories cat 1 and cat2 in the foreach loop.? Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-914511 Share on other sites More sharing options...
ignace Posted September 8, 2009 Share Posted September 8, 2009 print_r($value['children']); Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-914560 Share on other sites More sharing options...
karthikanov24 Posted September 8, 2009 Author Share Posted September 8, 2009 hi in the above posting,when i printed $categories using, print_r ($categories); I get output as, 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 ) ) ) ) But when i printed the key of [name] which is 12 using print_r ($key[name]); and value of 12 which is [name],[children], etc..using print_r ($value[12]); i am not getting the output.. So how can i get above said outputs..what is the codings..? Thanks in advance.. Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-914675 Share on other sites More sharing options...
karthikanov24 Posted September 8, 2009 Author Share Posted September 8, 2009 hi could u answer for my above question... Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-914689 Share on other sites More sharing options...
ignace Posted September 8, 2009 Share Posted September 8, 2009 print_r($value['children'][12]); Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-914811 Share on other sites More sharing options...
karthikanov24 Posted September 9, 2009 Author Share Posted September 9, 2009 hi please reply for my above question...?? Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-915586 Share on other sites More sharing options...
ignace Posted September 9, 2009 Share Posted September 9, 2009 print_r($value['children'][12]); Have you tried this? Link to comment https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/#findComment-915614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.