PHP动态菜单

时间:2013-06-29 11:16:25

标签: php mysql dynamic menu

我正在尝试学习动态菜单的工作原理。我找到了一个教程,我可以根据下面的数组构建一个动态菜单。

$menu_items = array(
  (object) array('id'=>'1', 'name'=>'Home', 'parent_menu_id'=>'0'),
  (object) array('id'=>'2', 'name'=>'Products', 'parent_menu_id'=>'0'),
  (object) array('id'=>'3', 'name'=>'About', 'parent_menu_id'=>'0'),
  (object) array('id'=>'4', 'name'=>'Contact', 'parent_menu_id'=>'0'),
  (object) array('id'=>'5', 'name'=>'Apparel', 'parent_menu_id'=>'2'),
  (object) array('id'=>'6', 'name'=>'Shirts', 'parent_menu_id'=>'2'),
  (object) array('id'=>'7', 'name'=>'Pants', 'parent_menu_id'=>'2'),
  (object) array('id'=>'8', 'name'=>'Hats', 'parent_menu_id'=>'5'),
  (object) array('id'=>'9', 'name'=>'Gloves', 'parent_menu_id'=>'5'),
  (object) array('id'=>'10', 'name'=>'Ballcaps', 'parent_menu_id'=>'8'),
  (object) array('id'=>'11', 'name'=>'Beanies', 'parent_menu_id'=>'8'),
  (object) array('id'=>'12', 'name'=>'Wool', 'parent_menu_id'=>'11'),
  (object) array('id'=>'13', 'name'=>'Polyester', 'parent_menu_id'=>'11'),
  (object) array('id'=>'14', 'name'=>'Jimsider.com', 'parent_menu_id'=>'4'),
 );

现在我的问题。我希望能够从MySQL填充这个数组。我试图通过fetchall实现这一点,这确实使我成为一个数组,但是数组与上面的PHP数组有太大不同。 (使用var_dump检查)

我创建了一个包含以下列的MySQL表:id, name, parent_menu_id。我按上面插入了信息。

我如何在与上面的PHP数组相同的数组类型中获取我的数据库信息?

数组从PHP看起来的方式:

array(14) { [0]=> object(stdClass)#15 (3) { ["id"]=> string(1) "1" ["name"]=> string(4) "Home" ["parent_menu_id"]=> string(1) "0" } [1]=> object(stdClass)#14 (3) { ["id"]=> string(1) "2" ["name"]=> string(8) "Products" ["parent_menu_id"]=> string(1) "0" } [2]=> object(stdClass)#13 (3) { ["id"]=> string(1) "3" ["name"]=> string(5) "About" ["parent_menu_id"]=> string(1) "0" } [3]=> object(stdClass)#12 (3) { ["id"]=> string(1) "4" ["name"]=> string(7) "Contact" ["parent_menu_id"]=> string(1) "0" } [4]=> object(stdClass)#11 (3) { ["id"]=> string(1) "5" ["name"]=> string(7) "Apparel" ["parent_menu_id"]=> string(1) "2" } [5]=> object(stdClass)#10 (3) { ["id"]=> string(1) "6" ["name"]=> string(6) "Shirts" ["parent_menu_id"]=> string(1) "2" } [6]=> object(stdClass)#9 (3) { ["id"]=> string(1) "7" ["name"]=> string(5) "Pants" ["parent_menu_id"]=> string(1) "2" } [7]=> object(stdClass)#8 (3) { ["id"]=> string(1) "8" ["name"]=> string(4) "Hats" ["parent_menu_id"]=> string(1) "5" } [8]=> object(stdClass)#7 (3) { ["id"]=> string(1) "9" ["name"]=> string(6) "Gloves" ["parent_menu_id"]=> string(1) "5" } [9]=> object(stdClass)#6 (3) { ["id"]=> string(2) "10" ["name"]=> string(8) "Ballcaps" ["parent_menu_id"]=> string(1) "8" } [10]=> object(stdClass)#5 (3) { ["id"]=> string(2) "11" ["name"]=> string(7) "Beanies" ["parent_menu_id"]=> string(1) "8" } [11]=> object(stdClass)#4 (3) { ["id"]=> string(2) "12" ["name"]=> string(4) "Wool" ["parent_menu_id"]=> string(2) "11" } [12]=> object(stdClass)#3 (3) { ["id"]=> string(2) "13" ["name"]=> string(9) "Polyester" ["parent_menu_id"]=> string(2) "11" } [13]=> object(stdClass)#2 (3) { ["id"]=> string(2) "14" ["name"]=> string(12) "Jimsider.com" 

我尝试做一个foreach并填充它。但是然后只有我的db中的最后一个条目在数组中。

try {
$conn = new PDO($dblink, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $data = $conn->query('SELECT * FROM menu
');

   foreach($data as $row) { 

          $menu_items = array(
            (object) array('id'=>''.sprintf($row[id]).'', 'name'=>''.sprintf($row[name]).'', 'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''),

}
} catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
}                        

数组在foreach尝试中的显示方式:

array(1) { [0]=> object(stdClass)#5 (3) { ["id"]=> string(1) "3" ["name"]=> string(8) "Over ons" ["parent_menu_id"]=> string(1) "0" } }

编辑:当使用括号填充数组时:foreach似乎陷入了循环?

try {
$conn = new PDO($dblink, $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $data = $conn->query('SELECT id,name,parent_menu_id FROM menu
');

   foreach($data as $row) { 

      $menu_items[] = array(
        (object) array('id'=>''.$row[id].'', 'name'=>''.$row[name].'',       'parent_menu_id'=>''.$row[parent_menu_id].''),



 );
      }
 } catch(PDOException $e) {
 echo 'ERROR: ' . $e->getMessage();
 }        

1 个答案:

答案 0 :(得分:0)

好的,这里的每一行都分配给$menu_items

foreach($data as $row) { 
      $menu_items = array(
        (object) array('id'=>''.sprintf($row[id]).'', 'name'=>''.sprintf($row[name]).'', 'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''),

}

它应该添加到$menu_items

foreach($data as $row) { 
     $menu_items[] = (object) array(
         'id'=>''.sprintf($row[id]).'', 
         'name'=>''.sprintf($row[name]).'',
         'parent_menu_id'=>''.sprintf($row[parent_menu_id]).''
     );
}