【问题标题】:PHP Menu - How To Recursively Delete Parent and ChildPHP 菜单 - 如何递归删除父子节点
【发布时间】:2010-12-15 22:51:22
【问题描述】:

我正在开发 PHP /MySQL 中的数据驱动菜单系统。我不知道如何删除菜单项而不让其中一些成为孤立项。

所有顶级菜单项都有一个零 (0) 父 ID 值,表明它们是顶级的。我的 gridview 显示所有菜单、顶级和子菜单项,并允许多选删除。

问题是,如果在gridview中选择的要删除的项目之一是顶级菜单项,那么它下面的所有子菜单都会变成孤立的。

我需要实现的一般逻辑是什么?

【问题讨论】:

    标签: php menu data-driven


    【解决方案1】:

    删除某些项目时只需删除子项目。如果您只有 2 级深度,这应该不是太大的问题。如果您可以有 X 个级别,那么您将不得不为您删除的每个元素递归地删除每个子元素。

    【讨论】:

    • 我的菜单允许无限级别。所以可能有多个父菜单项有子项。
    • 您能否提供一个简化的 x 级别示例?
    【解决方案2】:

    下面的类将与你可以创建的尽可能多的孩子一起工作(无限)......所以考虑到你的 mysql 表被结构化为(id,parent,name),你所需要的只是一个从当前获取所有项目的函数级别,遍历每个项目并再次递归调用该函数以获取当前循环ID的子项目,每次将在数组中找到的ID保留以稍后删除,下面是我使用类完成它的完整代码,但它也可以使用全局数组和函数来完成。

    //full class below comprising of 2 methods(functions)  
    class menu_manager{
    
        //this is the function called with id, to initiate the recursive function below
        private function remove_menu_item($id){
            $ids_to_delete ="";
            //Zero global arrays for more than one call for this function get child menus id first in a an array
            $this->child_items_ids = array(0 => $id);
            $this->incrementby_one=0;
            //call recursive function with $id provided
            $this->get_array_of_child_ids($id); 
            //then createw ids for mysql IN Statment, foreach will create - 1,10,25,65,32,45,
            foreach($this->child_items_ids as $k=>$v ) $ids_to_delete.=$v.",";
            //Then we wrap it in around "(" and ")" and remove last coma to provide - (1,10,25,65,32,45)
            $ids_to_delete="(".substr($ids_to_delete, 0, -1).")";
            //then we Delete all id in one query only
            $remove = $this->db->query("DELETE FROM menu WHERE id IN $ids_to_delete ");
            if(!$remove) return false;
            else return true;
        } 
    
        /*this is the function that will be called as many times as a child is found,
    this function is called inside of itself in the query loop*/ 
    
        private function get_array_of_child_ids($id){
            $query = $this->db->query("SELECT id,label,parent FROM menu WHERE parent='".$id."' ");
            if($query){
                if($query->num_rows > 0) { // if found any items
                    while($list = $query->fetch_assoc()){ // we loop through each item
                        //increments array index by 1
                        $this->incrementby_one += 1;
                        //place current id in the array
                        $this->child_items_ids[$this->incrementby_one] = intval($list["id"]);
                        //and we call this function again for the current id
                        $this->get_array_of_child_ids($list["id"]);
                    } // while closing
                } // second if closing
            }  //first if closing  
        }   // recursive function closing
    }   // class closing
    
    //to call the class you need:
    $delete_items = new menu_manager;
    $delete_items->remove_menu_item($id); //$id is the id for the item to be removed
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2017-05-04
      • 1970-01-01
      • 2021-05-10
      相关资源
      最近更新 更多