简体   繁体   English

在函数php中运行函数

[英]Run function inside function php

Happy new year!:) 新年快乐!:)

I'm fairly new to PHP and i'm trying to write a function to get the childs of the parent categories, and re use that function again to get the childs of childs categories. 我对PHP相当陌生,我正在尝试编写一个函数来获取父类别的子项,然后再次使用该函数来获取子类别的子项。

However, it is not working as I was hoping, and I can't really figure out why. 但是,它没有按我希望的那样工作,我无法弄清楚为什么。 I hope you have a minute to help me out here. 希望您有时间在这里帮助我。

Thank you! 谢谢! Roy 罗伊

$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
    )
)

The function 功能

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

The result 结果

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
                    )

            )

    )

)   

Result of print_r ($parent["children"]); 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
    )

)

So I thought: (but not working) 所以我想:(但不起作用)

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

Finally : 最后:

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

It's implemented in procedural style, but it works, just rewrite it as you need 它以程序样式实现,但可以正常工作,只需根据需要重写即可

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM