duanlan8763 2015-09-25 08:37
浏览 59
已采纳

在Code Igniter中的set_userdata函数中添加数据库中的所有数据

I want to add the data of the user from the database in the set_userdata() function in Code Igniter and my code is not working. When i display the userdata() all of the fields from the database didnt displayed. Can you check whats wrong? NEWBIE IN CODE IGNITER HERE :(

 if($this->form_validation->run()){
            $this->load->model('model_users');
            $userinfo=$this->model_users->get_user_info();
            $data = array(
                'empID' => $userinfo->empID,
                'email' => $userinfo->email,
                'emp_name' => $userinfo->emp_name,
                'address' => $userinfo->address,
                'position' => $userinfo->position,
                'usertype' => $userinfo->usertype,
                'is_logged_in' => 1
            );
            $this->session->set_userdata($data);
            redirect('main/home');
        } else{
            $this->login();
        }

and here is the code from the model

 public function can_log_in(){
    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', md5($this->input->post('password')));

    $query = $this->db->get('employee');
    if($query->num_rows() == 1){
        return true;
    } else {
        return false;
    }
}

public function get_user_info(){
     $query = $this->db->get_where('employee',array('email'=> $this->input->post('email'),'password'=> md5($this->input->post('password'))));
       foreach ($query->result() as $userinfo)
            {
               echo $userinfo->empID;
               echo $userinfo->email;
               echo $userinfo->emp_name;
               echo $userinfo->address;
               echo $userinfo->position;
               echo $userinfo->usertype;
            }
        return $userinfo;
}
  • 写回答

2条回答 默认 最新

  • dongxue163536 2015-09-25 08:47
    关注

    You need to pass $query->result from your model if yu get any rows from your query. afterforech loop variable $userinfo is empty so its return nothig

    Model

    function get_user_info(){
         $query = $this->db->get_where('employee',array('email'=> $this->input->post('email'),'password'=> md5($this->input->post('password'))));
         $num = $query->num_rows();
         if($num>0){
           return $query->result();
         }else{
              return FALSE;
         }
    

    Here you use foreach loop to extract your value

    Controller

    $user=$this->model_users->get_user_info();
    if(!empty($user)){
          foreach($user as $userinfo){
                $data = array(
                    'empID' => $userinfo->empID,
                    'email' => $userinfo->email,
                    'emp_name' => $userinfo->emp_name,
                    'address' => $userinfo->address,
                    'position' => $userinfo->position,
                    'usertype' => $userinfo->usertype,
                    'is_logged_in' => 1
                );
    }
        $this->session->set_userdata($data);
    }
    

    Updated

    MODEL

    function get_user_info() {
        $this->db->select('*');
        $this->db->from('employee');
        $this->db->where('email',$this->input->post('email'));
        $this->db->where('password',md5($this->input->post('password')));
        $query=$this->db->get();
         $num = $query->num_rows();
         if($num>0){
           return $query->row();
         }else{
              return FALSE;
         }
    }
    

    Controller

    $user=$this->model_users->get_user_info();
        if(!empty($user)){
                    $data = array(
                        'empID' => $userinfo->empID,
                        'email' => $userinfo->email,
                        'emp_name' => $userinfo->emp_name,
                        'address' => $userinfo->address,
                        'position' => $userinfo->position,
                        'usertype' => $userinfo->usertype,
                        'is_logged_in' => 1
                    );
    
            $this->session->set_userdata($data);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?