Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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 如何在codeigniter中获取父类别的所有子类别和孙子类别?_Php_Codeigniter_Parent Child_Categories - Fatal编程技术网

Php 如何在codeigniter中获取父类别的所有子类别和孙子类别?

Php 如何在codeigniter中获取父类别的所有子类别和孙子类别?,php,codeigniter,parent-child,categories,Php,Codeigniter,Parent Child,Categories,我想把父类别的所有子类别和孙子类别提升到任何级别。我的桌子结构是这样的。 有谁能建议我如何获取所有子类别(即手机类别的三星和S3)。假设这些子类别位于阵列中,则需要使用递归循环 // Information from database loaded into array $array = array( 0 => array( 'category_id' => 12, 'category_name' => '

我想把父类别的所有子类别和孙子类别提升到任何级别。我的桌子结构是这样的。


有谁能建议我如何获取所有子类别(即手机类别的三星和S3)。

假设这些子类别位于阵列中,则需要使用递归循环

// Information from database loaded into array
$array = array(
    0    => array(
        'category_id'        => 12,
        'category_name'      => 'Phone',
        'category_alias'     => 'phone',
        'parent_category'    => 0
    ),
    // ... other rows
); 
$children = array(); // Will contain all our children and grandchildren

function FindChildrenAndGrandchildren(array $array, $parentId, $level = 0) {
    $children = array();
    foreach($array as $category) {
        if($category['parent_category'] == $parentId) {
            if($level == 0) {
                $temp = FindChildrenAndGrandchildren($array, $category['category_id'], 1);
                $category['children'] = $temp;
            }
            $children[] = $category;
        }
    }
    return $children;
}

$children = FindChildrenAndGrandchildren($array, 12); // Start search

你需要递归性。创建如下函数(假设使用活动记录):

这样称呼它:

$all_categories = getCategoriesByParentId(0);
Array
(
    [0] => Array
        (
            [category_id] => 14
            [category_name] => s3
            [category_alias] => s3
            [parent_category] => 13
        )

    [1] => Array
        (
            [category_id] => 13
            [category_name] => Samsung
            [category_alias] => samsung
            [parent_category] => 12
        )

    [2] => Array
        (
            [category_id] => 12
            [category_name] => Phone
            [category_alias] => phone
            [parent_category] => 0
        )

)
输出如下:

$all_categories = getCategoriesByParentId(0);
Array
(
    [0] => Array
        (
            [category_id] => 14
            [category_name] => s3
            [category_alias] => s3
            [parent_category] => 13
        )

    [1] => Array
        (
            [category_id] => 13
            [category_name] => Samsung
            [category_alias] => samsung
            [parent_category] => 12
        )

    [2] => Array
        (
            [category_id] => 12
            [category_name] => Phone
            [category_alias] => phone
            [parent_category] => 0
        )

)

有关更多信息,请参见
OpenCart 1.5.4
,class
ModelCatalogCategory
,位于
catalog\model\catalog\category.php

下,但是sql查询在哪里?@machineaddict真的吗?您不能编写SQL查询来获取数据?请使用完整编码@machineaddict