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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘