handoyo Posted September 30, 2009 Share Posted September 30, 2009 Hi all,i found out a tutorial about ecommerce on phpwebcommerce.com..There are a some codes like this $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 When i try to print_r like this $categories[$id] = array('name' => $name, 'children' => array()); print_r($categories); 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...God bless you all.. Link to comment https://forums.phpfreaks.com/topic/176038-need-help-abut-understanding-array/ Share on other sites More sharing options...
marvelade Posted September 30, 2009 Share Posted September 30, 2009 1) For a more readable output do this instead: echo '<pre>' . print_r($result,true) . '</pre>'; 2) When you've completed a tutorial on how associative arrays work you will understand. there's tons of explanations on the internet far better than waht I can explain here 3) .= means you glue a string to another variable $a = "Hello"; $a.= " Moon"; echo $a; // this will output "Hello Moon". actually this: $a .= " Moon"; does the same as $a = $a . " Moon"; but it's a little shorter to write. greetz, Marv Link to comment https://forums.phpfreaks.com/topic/176038-need-help-abut-understanding-array/#findComment-927601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.