donglinxi1467
donglinxi1467
2015-08-11 13:48
浏览 22
已采纳

如何在codeigniter中分别获取每个管理员创建的用户?

i have a Codeigniter system where Admin creates Reseller. So in reseller table values like name,country,phone,email,key,user_number, are stored. Whatever value is in user_number that much blank rows are created in users table. So reseller key is mapped to that user rows. if reseller asks for 10 user than 10 blank rows are created in user table mapped to his key.

Now once a reseller is created he can login and can edit his users i.e(he can fill in the user details)

Now my issues is that i want to fetch only the users mapped to currently loggedin reseller and not all the users in table. I dont know how to do this please help:

below is my Code where i fetch all the users for a reseller :

Controller:

public function index ()
{
    // Fetch all users


    $session_data = $this->session->userdata('logged_in');
    $id = $session_data['id'];

    $data['users'] = $this->user_m->get_users($id);


    // Load view
    $this->data['subview'] = 'reseller/user/index';
    $this->load->view('reseller/_layout_main', $this->data);


}

The Model function:

public function get_users(){

    $query = $this->db->get_where('users', array('key' => $id));   

    if($query -> num_rows() > 1)
    {
    return $query->result();
    }
    else
    {
   return false;
    }


   }

I have tried to fetch the users but got stucked with errors! please help me with code. i just need to show user mapped by key of reseller. So reseller should only see his own users and not others!

图片转代码服务由CSDN问答提供 功能建议

我有一个Codeigniter系统,其中Admin创建了经销商。 因此,存储在名称,国家/地区,电话,电子邮件,密钥, user_number 等转销商表格值中。 无论 user_number 中的值是什么,在users表中创建了很多空白行。 因此,经销商密钥将映射到该用户行。 如果经销商要求10个用户,则在映射到其密钥的用户表中创建10个空白行。

现在一旦创建了经销商,他就可以登录并可以编辑他的用户即(他可以填写) 在用户详细信息中)

现在我的问题是我只想获取映射到当前登录的经销商的用户而不是表中的所有用户。 我不知道如何做到这一点,请帮助:

下面是我的代码,我获取经销商的所有用户:

控制器:

 公共函数索引()
 {
 //获取所有用户
 
 
 $ session_data = $ this-> session-> userdata('logged_in'  ); 
 $ id = $ session_data ['id']; 
 
 $ data ['users'] = $ this-> user_m-> get_users($ id); 
 
 
 // 加载视图
 $ this-> data ['subview'] ='reseller / user / index'; 
 $ this-> load-> view('reseller / _layout_main',$ this-> data)  ; 
 
 
} 
   
 
 

模型函数:

  public function get_users(){\  n 
 $ query = $ this-> db-> get_where('users',array('key'=> $ id));  
 
 if($ query  - > num_rows()> 1)
 {
返回$ query-> result(); 
} 
 else 
 {
返回false; 
}  
 
 
} 
   
 
 

我试图获取用户但却遇到了错误! 请帮我代码。 我只需要通过经销商的密钥显示用户映射。 因此,经销商应该只看到自己的用户而不是其他用户!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxia026531
    dongxia026531 2015-08-11 13:57
    已采纳

    You can set the a flag in db to know the reseller is logged in for example logged_in 1 and when the user will logout just make the logged_in to 0.

    that way when you fetching the reseller just use the condition array('key' => $id, 'logged_in' => 1).

    Hope that will help.

    点赞 评论
  • dua27031
    dua27031 2015-08-11 14:00

    You need to pass $id argument in your model function

    public function get_users($id){
    
    点赞 评论
  • douchuang8359
    douchuang8359 2015-08-11 17:16

    Finally i got the answer using sessions :)

        $id = $this->session->userdata('id');
    
        $this->db->where("key",$this->session->userdata('id'));     
        $this->data['users'] = $this->user_m->get();
    
    点赞 评论

相关推荐