Search code examples
phpcodeignitersessionsession-variables

How to display value of my first name and last name into navigation bar


I am using code igniter and I wanted to display the full name of the users logged in my navigation.

I tried displaying the value of the username that logged in and also the password.

Then I have also tried adding array items such as first name and last name base on my database table users and it doesn't work.

Below is my login function code on Controller

        public function login(){
            $data['title'] = 'Sign In';

            $this->form_validation->set_rules('user_email','email','required|valid_email');
            $this->form_validation->set_rules('user_password','password','required');

if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');              
$this->load->view('login',$data);

} else {
    $email = $this->input->post('user_email');
$password = md5($this->input->post('user_password'));
//Login User
$user_id = $this->user_model->login($email,$password);
if($user_id){
//create sessions
$user_data = array(                     
                        'user_email' => $email,     
                        'user_name' => $user_id->user_name,
                        'user_roles' => $user_id->types,
                        'logged_in' => true
                    );
                    $this->session->set_userdata($user_data);

             $this->session->set_flashdata('user_loggedin','You are now Login');
             redirect(base_url() . 'site/dashboard');
                } else {
              //Set message
              $this->session->set_flashdata('failed_login','Login is Invalid');
              redirect(base_url() . 'site');
                }      
            }
        }

Below is my user_model login function

      public function login($email,$password)
      {
        $this->db->where('user_email',$email);
        $this->db->where('user_password',$password); 

        $result = $this->db->get('users');
        if($result->num_rows() === 1){
            return $result->row(0)->id;
        } else {
            return false;
        }
    }
 <li class="nav-item dropdown no-arrow">
    <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <span class="mr-2 d-none d-lg-inline text-gray-600 small">Welcome, <?php echo $this->session->userdata('user_name'); ?></span>
        <!-- <img class="img-profile rounded-circle" src="https://source.unsplash.com/QAB-WJcbgJk/60x60"> -->
    </a>
</li>

I have only succeeded in displaying the username and the password, not the name or last name tables that logged in


Solution

  • You didn't keep the data into session properly

    Just write this part of the code at your model function. I think this will solve your problem.

     //model function
            $this->load->model('user_model');
            $user_data = $this->user_model->login_success($username, $password);
            if ($user_data) {
                $session_data = array(
                    'username' => $username,
                    'password' => $password,
                    'first_name' => $user_data->first_name,  // This value from DB
                    'last_name' => $user_data->last_name    // This value from DB
             );
    
            $this->session->set_userdata($session_data);
    

    Write your model code like this:

      public function login($email,$password)
      {
        $this->db->where('user_email',$email);
        $this->db->where('user_password',$password); 
    
        $result = $this->db->get('users');
        if($result->num_rows() == 1){
            return $result->row() // it will fetch all the data from your db row
        } else {
            return false;
        }
    }
    

    you should change return $result->row(0)->id to return $result->row(). I hope now you will get the your desired output.