Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在多级菜单中查找节的绝对父级_Php_Mysql_Recursion_Menu_Multi Level - Fatal编程技术网

Php 在多级菜单中查找节的绝对父级

Php 在多级菜单中查找节的绝对父级,php,mysql,recursion,menu,multi-level,Php,Mysql,Recursion,Menu,Multi Level,我的网站中有一个多级菜单和以下数据库表结构: 截面图 家长id 章节名称 第四节永久性 我知道这不是最好的桌子设计:| 我要做的是获取此菜单中某个节的绝对父级(父级为0的节) 要检索整个树,我使用此函数: function generate_sections_select($parent, $level, $selected) { global $db; $stmt = $db->prepare("SELECT * FROM sections WHERE parent

我的网站中有一个多级菜单和以下数据库表结构:

  • 截面图
  • 家长id
  • 章节名称
  • 第四节永久性
我知道这不是最好的桌子设计:|

我要做的是获取此菜单中某个节的绝对父级(父级为0的节)

要检索整个树,我使用此函数:

function generate_sections_select($parent, $level, $selected) {
    global $db;

    $stmt = $db->prepare("SELECT * FROM sections WHERE parent_id = :parent_id AND deleted = '0'");
    $stmt->bindParam(':parent_id', $parent, PDO::PARAM_INT);
    $stmt->execute();

    while($row = $stmt->fetch()) {
        if ($selected == $row['section_id']) {
            echo "<option value='".$row['section_id']."' selected>".str_repeat('-', $level).' '.$row['name_ro']."</option>\n";
        } else {
            echo "<option value='".$row['section_id']."'>".str_repeat('-', $level).' '.$row['name_ro']."</option>\n";
        }

        generate_sections_select($row['section_id'], $level+1, $selected);
    }
}
函数生成\u节\u选择($parent、$level、$selected){
全球$db;
$stmt=$db->prepare(“从parent_id=:parent_id AND deleted='0'的部分中选择*);
$stmt->bindParam(':parent_id',$parent,PDO::PARAM_INT);
$stmt->execute();
而($row=$stmt->fetch()){
如果($selected==$row['section\u id']){
回显“.str_repeat('-',$level)。'.$row['name_ro'.]。“\n”;
}否则{
回显“.str_repeat('-',$level)。'.$row['name_ro'.]。“\n”;
}
生成分区选择($row['section\u id',$level+1,$selected);
}
}
例如:

  • 级别1:主页
  • 第二级:关于我们
  • 第三级:我们的团队

“我们的团队”或“关于我们”的绝对父项是“主页”。

绝对父项将是没有父项的记录。是的,正如我所说,父项id=0,但我如何获得它?:)看一看。它似乎与你所问的非常相似。Tomas,它不是相同的表布局,我无法更改它。Thomas的链接具有id和父节点id,因此逻辑完全相同。为了满足你的需要而改变它应该是微不足道的。