确定什么是chlid php [英] Determine what is the level of chlid php

查看:50
本文介绍了确定什么是chlid php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立亲子关系并成功地获得这种关系,但是我被困在如何确定孩子的水平上。

I am trying to get parent child relation and successfully get that But I am stuck how to identify what is the level of the child.

这是我的代码

   public function getDownline($userid = null) {
        $category_data = array();

        $where = array('parent_id' => $userid);
        $this->db->where($where);
        $category_query = $this->db->get('users')->result();

        $category_data = array();
        foreach ($category_query as $category) {
            $category_data[$category->id] = array($category);
            $children = $this->getDownline($category->id);
            if ($children) {
                $category_data[$category->id]['children'] = $children;
            }
        }

        return $category_data;
    }

下面是我的表结构

我正在以这种格式输出

 [1136] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1136
                    [gid] => 4
                    [parent_id] => 1112
                    [username] => test
                    [email] => abc@abc.com
                    [name] => test
                    [status] => 1
                    [registerd] => 2017-04-20 08:49:25
                    [last_login] => 2017-04-21 10:42:25
                    [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                    [tranjection_password] => 
                )

            [children] => Array
                (
                    [1148] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 1148
                                    [gid] => 4
                                    [parent_id] => 1136
                                    [username] => test_downline
                                    [email] => kapil@abc.in
                                    [name] => test_downline
                                    [status] => 1
                                    [registerd] => 2017-04-21 10:42:56
                                    [last_login] => 2017-04-21 11:08:00
                                    [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                                    [tranjection_password] => 
                                )

                            [children] => Array
                                (
                                    [1150] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [id] => 1150
                                                    [gid] => 4
                                                    [parent_id] => 1148
                                                    [username] => test1_downline1
                                                    [email] => kapil25@abc.in
                                                    [name] => test1_downline1
                                                    [status] => 1
                                                    [registerd] => 2017-04-21 11:08:27
                                                    [last_login] => 0000-00-00 00:00:00
                                                    [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                                                    [tranjection_password] => 
                                                )

                                        )

                                )

                        )

                    [1149] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 1149
                                    [gid] => 4
                                    [parent_id] => 1136
                                    [username] => test_downline2
                                    [email] => kapil1@abc.in
                                    [name] => test_downline2
                                    [status] => 1
                                    [registerd] => 2017-04-21 11:06:35
                                    [last_login] => 0000-00-00 00:00:00
                                    [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                                    [tranjection_password] => 
                                )

                        )

                )

        )


推荐答案

i认为您应该在对象上下文中执行此操作
类似的事情应该起作用

i think you should do this in an object context here something like that should work

class Users_Model extends CI_Model
{

    public function getDownline(User_Object $obj, $level = 0) 
    {
        $obj->level = $level;

        $where = array('parent_id' => $obj->id);
        $this->db->where($where);
        $query = $this->db->get('users')->result("User_Object");

        foreach ($query as $objUser) 
        {
            $obj->add($objUser);
            $this->getDownline($objUser, ($level+1));
        }
        return $obj;
    }

    public function downline_income($userId = null, $offset = 0) 
    { 
        $userId = user::id(); 
        $objUser = new User_Object;
        $objUser->id = $userId;
        $downline = $this->user->getDownline($objUser);
    }
}


class User_Object
{
    private $children = [];
    public $level = 0;

    public function add(User_Object $obj)
    {
        $this->children[] = $obj;
    }

}

这篇关于确定什么是chlid php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆