Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays - Fatal编程技术网

Php 查找顶级父级

Php 查找顶级父级,php,arrays,Php,Arrays,我有一个带有id、category_id(父id)和url的MySQL表 我有一门课看起来像这样。我已经删除了所有不必要的功能 class categoriesBuilder { var $items = array(); var $html = array(); function fetch_assoc_all( $sql ) { $result = mysql_query( $sql, $this->conn );

我有一个带有id、category_id(父id)和url的MySQL表

我有一门课看起来像这样。我已经删除了所有不必要的功能

class categoriesBuilder
{
    var $items = array();
    var $html  = array();

    function fetch_assoc_all( $sql )
    {
        $result = mysql_query( $sql, $this->conn );

        if ( !$result ){
            return false;
        }

         $assoc_all = array();

         while( $fetch = mysql_fetch_assoc( $result ) ){
                $assoc_all[] = $fetch;
        }

        mysql_free_result( $result );

        return $assoc_all;
    }

    function get_categories()
    {
        $sql = 'SELECT id, category_id, url FROM categories ORDER BY category_id, id;';

        return $this->fetch_assoc_all( $sql );
    }

    function get_category_string($root_id=0)
    {
        $this->html  = array();
        $this->items = $this->get_categories();

        foreach ( $this->items as $item )
            $children[$item['category_id']][] = $item;

        // loop will be false if the root has no children
        $loop = !empty( $children[$root_id] );

        // initializing $parent as the root
        $parent = $root_id;
        $parent_stack = array();

        while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
        {
            if ( $option === false )
            {
                $parent = array_pop( $parent_stack );
            }
            elseif ( !empty( $children[$option['value']['id']] ) )
            {
                array_push( $parent_stack, $option['value']['category_id'] );

                $parent = $option['value']['id'];

                // HTML for menu item containing childrens (open)
                $this->html[] = $option['value']['url'] . "/";
            }
            else
            {
                $this->html[] = $option['value']['url'] . "/";
            }
        }

        return implode($this->html );
    }
}
我需要一个只返回顶级父级的函数。可能有几个子类别

i、 e

  • 类别id 1
    • 类别id 3
      • 类别id 4
      • 类别id 5
  • 类别id 2
    • 类别id 6
在本例中,如果在函数中输入3、4或5,则输出为1。如果我输入6,我将得到2

我还需要一个类似的功能,显示所有父文件夹

例如,如果我输入3,我将得到1,但如果我输入4或5,我将得到1-3


谢谢您的帮助。

假设您的数据库是这样的:

(1,0,1), (3,1,3), (4,3,4), (5,3,5), (2,0,2), (6、2、6)

然后你只需要在名单上走一步

e、 g

已修改以返回URL(我没有验证此代码,但它应该可以工作):

第一个函数返回URL,第二个函数应该返回数组数组。。。您将拥有标准索引,“category_id”和“url”作为嵌套/子数组。。。(如果有疑问,只需打印返回值即可了解我的意思)


我再次检查了原始代码,但没有检查更新…

我们不会为您编写此文件。您尝试了什么?我已经包含了一个我创建的函数,用于显示从ID开始的所有类别。我只是想不出如何扭转这种局面。很遗憾你没有使用PostgreSQL。在pg中,使用公共表表达式跟踪层次结构是一项简单的任务。真是太遗憾了!不幸的是,我被迫进入这个设置。有人在PHP中有任何指针吗?欢迎。。。只要一句警告的话,当心(真实的)陈述。。。它们可能会导致无休止的循环,使脚本超时(如果db继承人权限的结构不好或缺少父类别)。。。您可能需要设置某种计数器或计时器来终止循环(例如“RETURN FALSE”),谢谢您的建议。您知道如何返回url而不是id吗?我需要while循环中的each循环吗?对于get_top_parent,可以使用do{..}while($item_list[$current_category]['category_id']>$root_id)而不是while(true){..}
function get_top_parent($category_id, $root_id=0)
{
    // Grab the id's and category's
    $item_list = array();
    foreach($this->items as $item) {
        $item_list[$item['id']] = $item['category_id'];
    }

    $current_category = $category_id;

    while(TRUE) {
        if ($item_list[$current_category] == $root_id) {
            // Check to see if we have found the parent category.
            return $current_category;
        } else {
            // update our current category
            $current_category = $item_list[$current_category];
        }
    }

}

function get_parents($category_id, $root_id=0) 
{
    $parents = array();
    // Grab the id's and category's
    $item_list = array();
    foreach($this->items as $item) {
        $item_list[$item['id']] = $item['category_id'];
    }

    $current_category = $category_id;
    while(TRUE) {
        // Check to see if we have found the root category.
        if ($item_list[$current_category] == $root_id) {
            return $parents;
        } else {
            // update our current category and parents
            $current_category = $item_list[$current_category];
            array_unshift($parents, $current_category);
        }
    }

}
function get_top_parent($category_id, $root_id=0) 
{ 
    // Grab the id's and category's 
    $item_list = array(); 
    foreach($this->items as $item) { 
        $item_list[$item['id']] = array(
            'category_id'   => $item['category_id'],
            'url'           => $item['url']
        );
    } 

    $current_category = $category_id; 

    while(TRUE) { 
        if ($item_list[$current_category]['category_id'] == $root_id) { 
            // Check to see if we have found the parent category. 
            return $item_list[$current_category]['url']; 
        } else { 
            // update our current category 
            $current_category = $item_list[$current_category]['category_id']; 
        } 
    } 

} 

function get_parents($category_id, $root_id=0)  
{ 
    $parents = array(); 
    // Grab the id's and category's 
    $item_list = array(); 
    foreach($this->items as $item) { 
        $item_list[$item['id']] = array(
            'category_id' => $item['category_id'],
            'url' => $item['url']
        );
    } 

    $current_category = $category_id; 
    while(TRUE) { 
        // Check to see if we have found the root category. 
        if ($item_list[$current_category]['category_id'] == $root_id) { 
            return $parents; 
        } else { 
            $temp_array = array(
                'category_id'   => $current_category
                'url'           => $item_list[$current_category]['url']
            );
            // update our current category and parents 
            $current_category = $item_list[$current_category]['category_id']; 
            array_unshift($parents, $temp_array); 
        } 
    } 

}