如何在CodeIgniter中检索使用会话登录的用户所拥有的单个用户数据



我是CodeIgniter的新手。我的代码的问题是,当用户登录到他/她的帐户时,他无法查看他/她的详细信息,而是查看我提取该帐户的表中第一行的详细信息。我尝试了所有帐户,但第一行仍然会显示。

我的目标是,当用户可以登录时,他将被重定向到他的帐户并提供详细信息。

控制器

public function __construct()
{
    parent::__construct();
    $this->load->model('My_profile_model');
    if(!$this->session->userdata('logged_in'))
    {
        // Set error
        $this->session->set_flashdata('need_login', 'Sorry, you need to be logged in to view that area');
        redirect('home/index');
    }
}
public function show($teachers_id)
{
    $user_id = $this->session->userdata('user_id');
    $data['my_data'] = $this->My_profile_model->get_my_profile($teachers_id);
    $data['title'] ='My Profile';
    $data['nav_content'] = 'layouts/teacher_nav';
    $data['main_content'] = 'teachers/my_profile';
    $this->load->view('layouts/main', $data);
}

型号

<?php
class My_profile_model extends CI_Model{
public function get_my_profile($teachers_id)
{
    $query = $this->db->get('teachers');
    $this->db->where('teachers_id',$teachers_id);
    return $query->row();
    }
}

查看

<h2 class="featurette-heading"><?php echo $my_data->first_name; ?>&nbsp;<?php echo $my_data->last_name; ?> <BR /><span class="text-muted"><?php echo $my_data->rank_id; ?></span></h2>
<hr>
<h4><small>Gender:</small> <?php echo $my_data->gender; ?></h4>
<h4><small>Status:</small> <?php echo $my_data->status; ?></h4>
<h4><small>Address:</small> <?php echo $my_data->address_id; ?></h4>
<h4><small>Field of Specialization:</small> <?php echo $my_data->fos_id; ?></h4>
<h4><small>School Graduated:</small> <?php echo $my_data->school_graduated; ?></h4>
<h4><small>Eligibility:</small> <?php echo $my_data->eligibilities; ?></h4>
<h3>Educational Qualification</h3>
<h4><small>Bachelors Degree:</small> <?php echo $my_data->bachelors_degree; ?></h4>
<h4><small>Masteral (Unit):</small> <?php echo $my_data->m_unit; ?></h4>
<h4><small>Years of Teaching Experience:</small> <?php echo $my_data->years_of_teaching; ?></h4>

在这里,我还将包括我的登录控制器,因为我不确定这个控制器是否返回会话。

用户控制器

public function login()
{
    $this->form_validation->set_rules('username','username','trim|required|min_length[4]|xss_clean');
    $this->form_validation->set_rules('password','password','trim|required|min_length[4]|max_length[50]|xss_clean');
    if($this->form_validation->run() == FALSE)
    {
        redirect('home');
    }
    else
    {
        //Get from post
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $level = $this->input->post('level');
        //Get user id from model
        $user_id = $this->User_model->login_user($username, $password, 1, $level);
        //Validate user
        if($user_id <> 0)
        {
            //Create array of user data
            $user_data = array(
                                'user_id'   => $user_id,
                                'username'  => $username,
                                'level' => $level,
                                'logged_in' => TRUE
                            );
            //Set session userdata
            $this->session->set_userdata($user_data);
            if ($level === 'Administrator') {
                $this->session->set_flashdata('login_success', 'You are now logged in as an administrator');
                redirect('admin/profiles');
            }
            if ($level === 'Teacher') {
                $this->session->set_flashdata('login_success', 'You are now logged in as a teacher');
                redirect('teacher/my_profile');
            }
        }
        else
        {
            //Set error
            $this->session->set_flashdata('login_failed', 'The login info that you entred is invalid. Please try again.');
            redirect('home/index');
        }
    }
}

以及用户控制器的模型

public function login_user($username, $password, $status, $level)
{
    //Validate
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where('username', $username);
    $this->db->where('password', $password);
    $this->db->where('status', $status);
    $this->db->where('level', $level);
    $result = $this->db->get();
    if ($result->num_rows() == 1){
        return $result->row(0)->teachers_id;
    }
}

正如我所说的,我是新手。任何帮助都将不胜感激。提前感谢:)

哦,现在得到答案了。。

我只是更改了我的型号的查询

public function get_my_profile($teachers_id = NULL)
{
    $user_id = $this->session->userdata('user_id');
    $this->db->select('*');
    $this->db->from('teachers');
    $this->db->join('users','users.teachers_id = teachers.teachers_id');
    $this->db->where('users.teachers_id',$user_id);
    $query = $this->db->get();
    return $query->row();
}

相关内容

最新更新