如何根据PHP codeigniter中的管理员获取用户

时间:2015-09-04 08:48:19

标签: php mysql codeigniter

我有一个codeigniter应用程序,我有三个用户:管理员,经销商,用户。现在在管理仪表板上,我向用户和经销商展示。我想为管理员创建一个视图,他可以根据经销商查看用户。每个用户都通过一个名为key的字段映射到他的经销商,该字段是唯一的。因此,当我点击经销商时,我想只显示他的用户而不是数据库中的所有用户。

下面是我的get和get_by方法的基本模型:

public function array_from_post($fields){
    $data = array();
    foreach ($fields as $field) {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}


public function get($id = NULL, $single = FALSE){

    if ($id != NULL) {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->where($this->_primary_key, $id);
        $method = 'row';
    }
    elseif($single == TRUE) {
        $method = 'row';
    }
    else {
        $method = 'result';
    }


    return $this->db->get($this->_table_name)->$method();
}

public function get_by($where, $single = FALSE){
    $this->db->where($where);
    return $this->get(NULL, $single);
}

转销商表 Reseller Table

用户表 Users Table

转销商控制器:

<?php

类转销商扩展了Admin_Controller {

        public function __construct ()
        {
            parent::__construct();
        }

        public function index ()
        {
            $usertype=$this->session->userdata('usertype');
            if($usertype ==="admin")
                {
                    // Fetch all users
                    $this->data['users'] = $this->reseller_m->get();
                    // Load view
                    $this->data['subview'] = 'admin/reseller/index';
                    $this->load->view('admin/_layout_main', $this->data);
                }
            else
                {


                    $this->load->view('permission');
                }   

        }

REseller View

1 个答案:

答案 0 :(得分:1)

我从你的问题中了解到的是;您想要显示转售商及其用户的组合结果。为此,您必须在表之间使用JOIN。以下是Active Record Class链接,了解有关加入的信息。

或类似

SELECT * from reseller_table AS RT LEFT JOIN users_table as UT ON UT.sip_id=RT.sip_id WHERE conditions

或CI代码

$query="SELECT reseller.*,users.total_users FROM reseller LEFT JOIN (SELECT COUNT(id) as total_users,sip_id from users)AS UT on UT.sip_id =reseller.id"; $this->db->query($query);