gpt4 book ai didi

php - 如何通过反复查询数据库在PHP中创建函数

转载 作者:可可西里 更新时间:2023-11-01 08:10:01 25 4
gpt4 key购买 nike

我的代码可以正常工作,但我对此并不满意,它看起来很脏。我的问题是,如何为我的代码创建一个不重复的函数。这是我的代码,

    <?php
// PARENT MENU
$q2 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = (SELECT MIN(parent) from userpage) ORDER BY t.sort ASC";
$r2 = $db->query($q2);
$a2 = $db->fetch_all_array($q2);
foreach($a2 as $k => $v)
{
$indent = "&#10149;";
$tab = "&emsp;";
$menu_id = $v['m_id'];
echo $v['pagename'] . "(" . $v['page'] . ")";
// 1ST SUB MENU
if ($menu_id != '')
{
$q3 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id . " ORDER BY t.sort ASC";
$r3 = $db->query($q3);
$a3 = $db->fetch_all_array($q3);
foreach($a3 as $k3 => $v3)
{
$menu_id2 = $v3['m_id'];
echo $v3['pagename'] . "(" . $v3['page'] . ")";
// 2ND SUB MENU
if ($menu_id2 != '')
{
$q4 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id2 . " ORDER BY t.sort ASC";
$r4 = $db->query($q4);
$a4 = $db->fetch_all_array($q4);
foreach($a4 as $k4 => $v4)
{
$menu_id3 = $v4['m_id'];
echo $v4['pagename'] . "(" . $v4['page'] . ")";
// 3RD SUB MENU
if ($menu_id3 != '')
{
$q5 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id3 . " ORDER BY t.sort ASC";
$r5 = $db->query($q5);
$a5 = $db->fetch_all_array($q5);
foreach($a5 as $k5 => $v5)
{
$menu_id4 = $v5['m_id'];
echo $v5['pagename'] . "(" . $v5['page'] . ")";
// 4TH SUB MENU
if ($menu_id4 != '')
{
$q6 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id4 . " ORDER BY t.sort ASC";
$r6 = $db->query($q6);
$a6 = $db->fetch_all_array($q6);
foreach($a6 as $k6 => $v6)
{
$menu_id5 = $v6['m_id'];
echo $v6['pagename'] . "(" . $v6['page'] . ")";
// 5TH SUB MENU
if ($menu_id5 != '')
{
$q7 = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id5 . " ORDER BY t.sort ASC";
$r7 = $db->query($q7);
$a7 = $db->fetch_all_array($q7);
foreach($a7 as $k7 => $v7)
{
$menu_id6 = $v7['m_id'];
echo $v['pagename'] . "(" . $v['page'] . ")";
} //5th submenu
} //closing if for 5th submenu
} //4th submenu
} //closing if for 4th submenu
} //3rd submenu
} //closing if for 3rd submenu
} //2nd submenu
} //closing if for 2nd submenu
} //1st submenu
} //closing if for 1st submenu
} //parent menu
?>

谢谢。非常感谢任何答案。

最佳答案

您可以更改代码以使用递归。执行此操作的最简单方法仍将多次调用数据库,但您不需要在代码中全部调用它们。

调用下面的函数将写出子菜单及其所有子级/孙级等...

它使用 recursion 工作在这种情况下,这意味着该函数会调用自身来完成其任务。

function subMenu($menu_id) {
$q = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id . " ORDER BY t.sort ASC";
$r = $db->query($q);
$a = $db->fetch_all_array($q);
foreach($a2 as $k => $v)
{
$menu_id2 = $v['m_id'];
echo $v['pagename'] . "(" . $v['page'] . ")";
if ($menu_id2 != '') {
// write out the submenu of this submenu
subMenu($menu_id2);
}
}
}

带有缩进计数器。将 0 传递给 indent_count

function subMenu($menu_id, $indent_count) {
$q = "SELECT t.*, t2.id as m_id FROM userpage t left join menu t2 on (t2.parent=t.parent and t2.sort=t.sort) WHERE t.parent = " . $menu_id . " ORDER BY t.sort ASC";
$r = $db->query($q);
$a = $db->fetch_all_array($q);
foreach($a2 as $k => $v)
{
$menu_id2 = $v['m_id'];
// You need to add in an indent based on $indent_count
echo $v['pagename'] . "(" . $v['page'] . ")";
if ($menu_id2 != '') {
// write out the submenu of this submenu
subMenu($menu_id2, $indent_count + 1);
}
}
}

关于php - 如何通过反复查询数据库在PHP中创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39247363/

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