Rajan
Rajan

Reputation: 2425

How do i Fetch Users as per their admin in PHP codeigniter

I have a codeigniter application where i have three users : Admin,Reseller, Users. Now on admin dashboard i show users and reseller both. I want to create a view for admin, where he can see users as per the Reseller. Each user is mapped to his reseller by a field called key which is unique. So when i click on reseller i want to show only his users and not all the users in database.

below is my base model for get and get_by method:

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 Reseller Table

Users Table Users Table

The Reseller Controller :

<?php

class Reseller extends 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

Upvotes: 0

Views: 1090

Answers (1)

Chintan7027
Chintan7027

Reputation: 7605

What i have understand from your problem is; you want to display combine result of Re-sellers and its users. To do so you have to use JOIN between your tables. Here is Active Record Class link to see about join.

OR something like

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

Or CI Code

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

Upvotes: 1

Related Questions