doumei1908 2019-08-09 16:37
浏览 157
已采纳

登录后如何显示管理员全名的名称 - Codeigniter

I am having a trouble on displaying my admin's full name, like for example admin full name is John Doe, it is not displaying. I am still learning codeigniter please give me advice thank you!

here is my controller

            //Get username
        $username = $this->input->post('username');

        //Get and encrypt the password
        $password = $this->input->post('password');

        // Login user
        $user_id = $this->role_model->login($username, $password);


        if ($user_id) {
        // Create session
        $user_data = array(
            'user_id' => $user_id,
            'name' => $user_id->name,
            'username' => $username,
            'logged_in' => true
        );

        $this->session->set_userdata($user_data);

        //Set message
        $this->session->set_flashdata('user_loggedin','You are now logged in');

        redirect('pages/index');

here is my View - where I would like to display my full name, as you can see 'name' is the data field I have to display but it is does not show anything, it gives an error that says name is not defined.

  <li><a> Welcome, <?php echo $this->session->name ?> </a></li>

Model

        public function login($username, $password){
        //Validate
        $this->db->where('username',$username);
        $this->db->where('password',$password);

        $result = $this->db->get('users');

        if ($result->num_rows() == 1) {
            return $result->row(0)->id;
        }else{
            return false;
        }
    }
  • 写回答

3条回答 默认 最新

  • douaoren4402 2019-08-09 17:26
    关注

    Your method login() returns only id = digit (return $result->row(0)->id;), not object (in controller your get $user_id->name).

    Do this, in the model:

    if ($result->num_rows() == 1) {
        return $result->row(0); // fix
    }else{
        return false;
    }
    

    In the controller:

    $user = $this->role_model->login($username, $password);
    
    $user_data = array(
        'user_id' => $user->id, // fix
        'name' => $user->name, // fix
        'username' => $username,
        'logged_in' => true
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号