找到顶级父级

时间:2012-02-17 16:59:22

标签: 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 );

        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 );
    }
}

我需要一个只返回顶级父级的函数。可能有几个子类别。

  • 类别ID 1
    • 类别ID 3
      • category id 4
      • category id 5
  • 类别ID 2
    • category id 6

在这个例子中,如果我在函数中输入3,4或5,则输出为1.如果我输入6,我将得到2.

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

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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

(1,0,URL1), (3,1,URL3), (4,3,URL4), (5,3,URL5), (2,0,URL2), (6,2,URL6)

然后你只需要走上清单

e.g。

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);
        }
    }

}

返工以返回URL(我没有验证此代码,但它应该有效):

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); 
        } 
    } 

} 

第一个函数返回URL,第二个函数应该返回一个数组数组...你将拥有标准索引,“category_id”和“url”作为嵌套/子数组...(如果有疑问,只需执行返回值的print_r以查看我的意思)

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