在函数php里面运行函数

时间:2015-01-01 23:45:41

标签: php mysql

新年快乐!:)

我对PHP很新,我试图编写一个函数来获取父类别的子节点,然后再次使用该函数来获取子类别的子节点。

然而,它并没有像我希望的那样工作,我无法弄清楚原因。我希望你有一分钟​​的时间来帮助我。

谢谢!罗伊

$Parents =

Array
(
[0] => Array
    (
        [category_id] => 3
        [parent_id] => 1
        [name] => PC Onderdelen
        [is_active] => 1
        [position] => 2
        [level] => 1
    )

[1] => Array
    (
        [category_id] => 11
        [parent_id] => 1
        [name] => test
        [is_active] => 1
        [position] => 3
        [level] => 1
    )

[2] => Array
    (
        [category_id] => 14
        [parent_id] => 1
        [name] => Rootcat3
        [is_active] => 1
        [position] => 4
        [level] => 1
    )
)

功能

public function getchilds($parents)
{
    $subcategories = array();

    foreach ($parents as $parent)
        {
            $parentid = $parent['category_id'];

            $sql = "SELECT * FROM categories WHERE parent_id = $parentid";

            $db = new DB ('novacpos');
            $result = $db->query($sql);

            while ($rows = mysqli_fetch_assoc($result))
                {
                    $subcategories[] = $rows;
                }
            $parent["children"] = $subcategories;
            unset ($subcategories);
            $parents1[] = $parent;
        }
    return $parents1;
}

结果

Array
(
[0] => Array
    (
        [category_id] => 3
        [parent_id] => 1
        [name] => PC Onderdelen
        [is_active] => 1
        [position] => 2
        [level] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [category_id] => 4
                        [parent_id] => 3
                        [name] => Moederborden
                        [is_active] => 1
                        [position] => 2
                        [level] => 2
                    )

                [1] => Array
                    (
                        [category_id] => 6
                        [parent_id] => 3
                        [name] => Behuizingen
                        [is_active] => 1
                        [position] => 1
                        [level] => 2
                    )

                [2] => Array
                    (
                        [category_id] => 8
                        [parent_id] => 3
                        [name] => Laptops
                        [is_active] => 1
                        [position] => 3
                        [level] => 2
                    )

                [3] => Array
                    (
                        [category_id] => 9
                        [parent_id] => 3
                        [name] => Muizen
                        [is_active] => 1
                        [position] => 4
                        [level] => 2
                    )

            )

    )

[1] => Array
    (
        [category_id] => 11
        [parent_id] => 1
        [name] => test
        [is_active] => 1
        [position] => 3
        [level] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [category_id] => 13
                        [parent_id] => 11
                        [name] => Test2
                        [is_active] => 1
                        [position] => 1
                        [level] => 2
                    )

            )

    )

[2] => Array
    (
        [category_id] => 14
        [parent_id] => 1
        [name] => Rootcat3
        [is_active] => 1
        [position] => 4
        [level] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [category_id] => 15
                        [parent_id] => 14
                        [name] => Extracat1
                        [is_active] => 1
                        [position] => 1
                        [level] => 2
                    )

            )

    )

)   

print_r ($parent["children"]);

的结果
Array
(
[0] => Array
    (
        [category_id] => 4
        [parent_id] => 3
        [name] => Moederborden
        [is_active] => 1
        [position] => 2
        [level] => 2
    )
[1] => Array
    (
        [category_id] => 6
        [parent_id] => 3
        [name] => Behuizingen
        [is_active] => 1
        [position] => 1
        [level] => 2
    )

[2] => Array
    (
        [category_id] => 8
        [parent_id] => 3
        [name] => Laptops
        [is_active] => 1
        [position] => 3
        [level] => 2
    )

[3] => Array
    (
        [category_id] => 9
        [parent_id] => 3
        [name] => Muizen
        [is_active] => 1
        [position] => 4
        [level] => 2
    )

)
Array
(
[0] => Array
    (
        [category_id] => 13
        [parent_id] => 11
        [name] => Test2
        [is_active] => 1
        [position] => 1
        [level] => 2
    )

)
Array
(
[0] => Array
    (
        [category_id] => 15
        [parent_id] => 14
        [name] => Extracat1
        [is_active] => 1
        [position] => 1
        [level] => 2
    )

)

所以我想:(但不工作)

public function getchilds($parents)
{
    $subcategories = array();

    foreach ($parents as $parent)
        {
            $parentid = $parent['category_id'];

            $sql = "SELECT * FROM categories WHERE parent_id = $parentid";

            $db = new DB ('novacpos');
            $result = $db->query($sql);

            while ($rows = mysqli_fetch_assoc($result))
                {
                    $subcategories[] = $rows;
                }
            $parent["children"] = $subcategories;
            unset ($subcategories);

            getchilds($parent["children"]); <-----------

            $parents1[] = $parent;
        }

    return $parents1;
}

2 个答案:

答案 0 :(得分:0)

class myModel{
public function get_categories(){

    $sql = "SELECT * FROM categories";

            $db = new DB ('novacpos'); // You should use a singleton pattern for this
            $result = $db->query($sql);
            $categories = array();

            while ($rows = mysqli_fetch_assoc($result))
                {
                    $categories[] = $rows;
                }

     return $categories;
}

public function organize_categories($categories) {
        $resultParents = array();
        $resultChildren = array();

        foreach ($categories as $row) {
            if(empty($row['parent']))
                $resultParents[] = $row;
            else
                $resultChildren[] = $row;
        }

        foreach ($resultParents as &$parent){
            $parent['children'] = $this->_organize_category($resultChildren, $parent['category_id']);
        }
        unset($parent);
        return $resultParents;
    }

    private function _organize_category($resultChildren, $parent) {
        $children = array();
        foreach ($resultChildren as $child) {
            if ($child['parent_id'] == $parent){
                $child['children'] = $this->_organize_category($resultChildren, $child['category_id']);
                $children[] = $child;
            }
        }

        return $children;
    }
}

最后:

$categories = $myModel->get_categories();
$organized_categories = $myModel->organize_categories($categories);
var_dump($organized_categories);

答案 1 :(得分:0)

它以程序风格实现,但它可以工作,只需根据需要重写它

$db = new mysqli('localhost', 'root', 'mysql', 'test');

$parents = [];
$result = $db->query('SELECT * FROM categories WHERE category_id IN (0,1)');

while ($row = $result->fetch_assoc())
{
    $parents[] = $row;
}


function getchilds($parents, $db)
{
    foreach ($parents as &$parent)
    {
        $result = $db->query("SELECT * FROM categories WHERE parent_id = {$parent['category_id']}");

        if ($result && $result->num_rows > 0)
        {
            while ($row = $result->fetch_assoc())
            {
                $parent['children'][] = $row;
            }

            $parent['children'] = getchilds($parent['children'], $db);
        }
    }

    return $parents;
}

echo '<pre>';
print_r(getchilds($parents, $db));