gpt4 book ai didi

php - MySQL 的 Json 菜单结构

转载 作者:行者123 更新时间:2023-11-29 08:43:26 25 4
gpt4 key购买 nike

我一直在纠结这个问题,看起来很简单。我正在尝试使用 PHP/MYSQL 生成菜单/子菜单 json 输出:这些是表格:

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

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

“menuHome”将具有“标题名称”,例如“关于我们”,在“菜单”上由“parentmenu”加入“id”,这将具有多个条目,例如“历史”、“所有者”、“新闻”。

我想要实现的结构是:

`
{
"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"
}
]
}
]
}
}


`

我已经尝试过,while,for,for every 在很多配置中,但我无法让它构建正确的数组结构来编码为 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"
}
]
}

问题是我无法事先获得“对” - “title”=“关于我们”...这很简单,但我对这个问题已经脑死亡了。

我可以在 json_encode 之前添加它:

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

但是如何让“key”“titleName”或仅“title”显示在“关于我们”之前...

最佳答案

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

INSERT INTO menu
(id, parentmenu, name)
VALUES
(1,1,"history"),
(2,1,"owners"),
(3,1,"news"),
(4,2,"address"),
(5,2,"map"),
(6,3,"jeans"),
(7,3,"tables"),
(8,3,"shoes");


INSERT INTO menuHome
(id,titleName)
VALUES
(1,"About Us"),
(2,"Contact Us"),
(3,"Products");

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

SELECT pm.id, cm.id, pm.titleName, cm.name FROM menu as cm
LEFT JOIN (menuHome as pm)
ON (pm.id = cm.parentmenu);


  • 第三,让我们开始编码


出于我们的目的,我们想要这样的东西 -

$arys = ["menu"=>["sections" => [
["title" => "About Us", "items" => [
["name"=>"History","id"=>1],
["name"=>"Owners","id"=>2],
["name"=>"News","id"=>3]
]],
["title" => "Contact Us", "items" => [
["name"=>"Address","id"=>4],
["name"=>"Map","id"=>5]
]],
["title" => "Products", "items" => [
["name"=>"Jeans","id"=>6],
["name"=>"Tables","id"=>7],
["name"=>"Shoes","id"=>8]
]]
]
]
];

让我们来构建它

$querys = "Select pm.id AS pmd , cm.id AS cmd, pm.titleName AS pmt, cm.name AS cmt from menu as cm left join (menuHome as pm) on (pm.id = cm.parentmenu) Order By 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);

您将获得所需的输出

关于php - MySQL 的 Json 菜单结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com