如何在codeigniter中获取映射到特定零售商的用户?

时间:2015-08-06 11:46:30

标签: php mysql codeigniter

我有一个门户,其中管理员创建转销商。每个经销商都可以拥有自己的用户。因此,在用户表中,它们使用转销商进行映射。所以现在我想只显示那些映射到他的 ID 的用户。那么如何只显示那些映射到他的帐户的用户。我的保存功能提取所有用户,但我想获取该经销商的用户,该用户已登录而不是获取所有记录。
以下是我的代码:

控制器:

public function index ()
{
    // Fetch all users
    $this->data['users'] = $this->reseller_m->get();

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

型号:

public function login ()
{
    $user = $this->get_by(array(
        'email' => $this->input->post('email'),
        'password' => $this->hash($this->input->post('password')),

    ), TRUE);

    if (count($user)) {
        // Log in user
        $data = array(
            'name' => $user->name,
            'email' => $user->email,


            'id' => $user->id,

            'loggedin' => TRUE,
        );
        $this->session->set_userdata($data);
    }
}

public function logout ()
{
    $this->session->sess_destroy();
    $this->session->unset_userdata('logged_in','id','name','email');

}

public function loggedin ()
{
    return (bool) $this->session->userdata('loggedin');
}

存在保存功能的基本模型。

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

public function save($data, $id = NULL){




    // Set timestamps
    if ($this->_timestamps == TRUE) {
        $now = date('Y-m-d H:i:s');
        $id || $data['created'] = $now;
        $data['modified'] = $now;
    }

    // Insert
    if ($id === NULL) {
        !isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
        $this->db->set($data);
        $this->db->insert($this->_table_name);
        $id = $this->db->insert_id();
    }
    // Update
    else {
        $filter = $this->_primary_filter;
        $id = $filter($id);
        $this->db->set($data);
        $this->db->where($this->_primary_key, $id);
        $this->db->update($this->_table_name);
    }

    return $id;
}

查看档案:

<?php if(count($users)): foreach($users as $user): ?>   
    <tr class="info">
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->sip_username); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->sip_password); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->key); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->name); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->status); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->email); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->phone); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->balance); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->created); ?></td>
    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->modified); ?></td> 
    <td><?php echo btn_edit('admin/user/edit/' . $user->id); ?></td>
    <td><?php echo btn_delete('admin/user/delete/' . $user->id); ?></td>
    </tr>
<?php endforeach; ?>
<?php else: ?>
        <tr>
            <td colspan="3">We could not find any users.</td>
        </tr>
<?php endif; ?> 

1 个答案:

答案 0 :(得分:1)

首先,当用户login.check usertype并在session中包含usertype。 或

)

您的模型string country = "COTE D’IVOIRE" bool isValid = Regex.IsMatch(country.Trim(), @"^[a-zA-Z\-'()/.,\s]+$"); 代码

public function index ()
{
    // Fetch all users
    $session_data = $this->session->userdata('logged_in');
    $id = $session_data['id'];

    $data['users'] = $this->reseller_m->get($id)

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

在您的视图文件中

reseller_m.php