MySQL的Json菜单结构

时间:2012-10-31 10:48:11

标签: php mysql json

我一直四处寻找这个问题,看起来很简单。我正在尝试使用PHP / MYSQL生成菜单/子菜单json:这些是表:

   CREATE TABLE `menuHome` 
  `id`,
  `titleName`

   CREATE TABLE `menu` 
  `id`,
  `parentmenu`,
  `name`

'menuHome'会有'titleName',例如'关于我们',''菜单'上的'id'加上'parentmenu',这将有多个条目,如'history','Owners','News'。

我想要实现的结构是:

`
 {
  "menu" : {
    "sections" : [
      {
        "title" : "About Us",
        "items" : [
          {
            "name" : "History",
            "id" : "0909"
          },
          {
            "name" : "Owners",
            "id" : "0910"
          },
          {
            "name" : "News",
            "id" : "0916"
          }
        ]
      },
      {
        "title" : "Contact Us",
        "items" : [
          {
            "name" : "Address",
            "id" : "0949"
          },
          {
            "name" : "Map",
            "id" : "0978"
          }
        ]
      },
      {
        "title" : "Products",
        "items" : [
          {
            "name" : "Jeans",
            "id" : "1010"
          },
          {
            "name" : "Tables",
            "id" : "1088"
          },
          {
            "name" : "Shoes",
            "id" : "2424"
          }
        ]
      }
    ]
  }
}


`

我已经尝试了,但是,对于每个在很多配置中但我无法使它构建正确的数组结构以编码为json。我现在处于代码失明阶段,所以任何帮助都会受到极大的欢迎

这是我的最后一次尝试:

     $sql_query  = 'SELECT menuHome.titleName, menuHome.id FROM menuHome';
        $result = $mysqli->query($sql_query);
        $menu = array();
        while ($row = $result->fetch_assoc()) {

        $menuid = $row["id"];

        $sql_query2  = 'SELECT menu.name, menu.id FROM menu WHERE menu.parentmenu = "' .  $menuid . '"';

        $result2 = $mysqli->query($sql_query2);

        while ($row2 = $result2->fetch_assoc()) {

         $menu[$row["titleName"]][] = $row2;


         }
        }

return json_encode($menu); 

这是上面带来的结果:

{
  "About Us" : [
    {
      "name" : "History",
      "id" : "1"
    },
    {
      "name" : "Owners",
      "id" : "2"
    },
    {
      "name" : "News",
      "id" : "3"
    }
  ],
  "Contact Us" : [
    {
      "name" : "Address",
      "id" : "4"
    },
    {
      "name" : "Map",
      "id" : "5"
    }
  ],
  "Products" : [
    {
      "name" : "Jeans",
      "id" : "6"
    },
    {
      "name" : "Tables",
      "id" : "7"
    },
    {
      "name" : "Shoes",
      "id" : "8"
    }
  ]
}

问题是我不能事先得到'对' - “标题”=“关于我们”......这很简单,但我在这个问题上已经死了。

我可以在json_encode之前添加:

  $menuoutput = array("menu" => array("sections" => array($menu)));

但是如何在“关于我们”之前显示“关键”'titleName'或仅显示'标题'......

1 个答案:

答案 0 :(得分:2)

  • 首先我假设你的数据是这样的 -
  

INSERT INTO menu
  (idparentmenuname
  价值观   (1,1,“历史”),
  (2,1,“所有者”),
  (3,1,“新闻”),
  (4,2,“地址”),
  (5,2,“地图”),
  (6,3,“牛仔裤”),
  (7,3,“表格”),
  (8,3, “鞋子”);


  

INSERT INTO menuHome
  (idtitleName
  价值观   (1,“关于我们”),
  (2,“联系我们”),
  (3, “产品”);

  • 其次,我建议您将查询更改为 -
  

以pm为单位选择pm.id,cm.id,pm.titleName,cm.name FROM菜单   LEFT JOIN(menuHome as pm)
  ON(pm.id = cm.parentmenu);


  • 第三,让我们来编码


为了我们的目的,我们想要这样的东西 -

  

$ arys = [“menu”=> [“sections”=> [
  [“title”=> “关于我们”,“项目”=> [
                                                                          [ “名称”=> “中史”, “ID”=→1],
                                                                          [ “名称”=> “中拥有者”, “ID”=→2],
                                                                          [ “名称”=> “中新闻”, “ID”=→3]
  ]],
  [“title”=> “联系我们”,“项目”=> [
  [ “名称”=> “中地址”, “ID”= 1→4],
  [ “名称”=> “中图”, “ID”=大于5]
  ]],
  [“title”=> “产品”,“项目”=> [
  [ “名称”=> “中牛仔裤”, “ID”=→6],
  [ “名称”=> “中的表”, “ID”=大于7],
  [ “名称”=> “中鞋”, “ID”=> 8]
  ]]
  ]
  ]
  ];

所以让我们建立起来

  

$ querys =“选择pm.id AS pmd,cm.id AS cmd,pm.titleName AS pmt,cm.name AS cmt from菜单,cm cm left join(menuHome as pm)on(pm.id = cm。 parentmenu)按pmd命令,cmd“;
  $ result = mysql_query($ querys);
  $ jsary = [“menu”=> [“sections”=> []]];
  $ lastPid = 0;
  $ currentPid = 0;
  $ title =“”;
  $ ifff = 0;
  $ elss = 0;
  while($ row = mysql_fetch_array($ result))
  {
  $ currentPid = $ row ['pmd'];
  $ title = $ row ['pmt'];

    $cmd = $row['cmd'];
    $cmt = $row['cmt'];

    if($lastPid != $currentPid)
    {
            $insAry = [];
            $insAry = ["title"=> $title, "items" => [["name" => $cmt, "id" => $cmd]]];
            array_push($jsary["menu"]["sections"], $insAry);
            $lastPid = $currentPid;
            $ifff = $ifff + 1;
            $currentPid = 0;
    }
    else
    {
            $ind = 0;
            if($ifff > 0)
            {
                    $ind = $ifff-1;
            }
            $insAry = [];
            $insAry = ["name" => $cmt, "id" => $cmd];
            array_push($jsary["menu"]["sections"][$ind]["items"], $insAry);
    }}

现在只需调用json_encode()

  

json_encode($ jsary);

您将获得所需的输出