King
King

Reputation: 2025

passing user information to view in codeigniter

I am trying to get users details from the database and put it in session so that it can be used in the view. I have tried all I can but I keep getting error. Undefined Variable Email.

MODEL:

function login($username, $password)
    {
        $this->db->select('authTbl.id, authTbl.password, authTbl.username, authTbl.email, authTbl.mobile');
        $this->db->from('users as authTbl');
        $this->db->where('authTbl.username', $username);
        $this->db->where('authTbl.isDeleted', 0);
        $query = $this->db->get();

        $user = $query->result();

        if(!empty($user)){
            if(verifyHashedPassword($password, $user[0]->password)){
                return $user;
            } else {
                return array();
            }
        } else {
            return array();
        }
    }

CONTROLLER:

function isLoggedIn()
    {
        $isLoggedIn = $this->session->userdata('isLoggedIn');
        $data['title'] = 'Login';
        if(!isset($isLoggedIn) || $isLoggedIn != TRUE)
        {
            $this->load->view('templates/header');
            $this->load->view('users/login', $data);
            $this->load->view('templates/footer');
        }
        else
        {
            redirect('posts');
        }
    }

    /**
     * This function used to logged in user
     */
    public function login()
    {
        $this->load->library('form_validation');
        $data['title'] = 'Login';
        $this->form_validation->set_rules('username', 'Username', 'required|max_length[128]|trim');
        //$this->form_validation->set_rules('password', 'Password', 'required|max_length[32]|');

        if($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('users/login', $data);
            $this->load->view('templates/footer');
        }
        else
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $result = $this->user_model->login($username, $password);

            if(count($result) > 0)
            {
                foreach ($result as $res)
                {
                    $sessionArray = array('id'=>$res->id,                    
                                            'username'=>$res->username,
                                            'email'=>$res->email,
                                            'mobile'=>$res->mobile,
                                            'isLoggedIn' => TRUE
                                    );

                    $this->session->set_userdata($sessionArray);
                    $this->session->set_flashdata('user_login', 'Welcome');
                    redirect('posts');
                }
            }
            else
            {
                $this->session->set_flashdata('login_fail', 'Email or password mismatch');

                redirect('users/login');
            }
        }
    }

VIEW:

<?php echo $email; ?>

Upvotes: 0

Views: 220

Answers (3)

Akshit Ahuja
Akshit Ahuja

Reputation: 337

$email will not work because writing $email means ordinary variable but in your case you have to write like :

<?php echo $this->session->userdata('email'); ?>

Upvotes: 2

D D Parmar
D D Parmar

Reputation: 137

If You are using flashdata, then You can get it using below code:

Controller

if (empty($this->session->userdata('UserID'))) {
            $this->session->set_flashdata('flash_data', 'You don\'t have access!');
            $this->load->view("initialHeader");
            $this->load->view("login");
            redirect('Login');
        }

View

<?php if(!empty($this->session->flashdata('flash_data'))) {
                                ?>
                                <div class="alert alert-danger">
                                    <?php echo $this->session->flashdata('flash_data'); ?>
                                </div>

                            <?php } ?>

If You are using tmp data as a session, please refer below code:

Controller

<?php 
if (!empty($data)) {
            $this->session->set_userdata('permission_error_msg', $data);
            $this->session->mark_as_temp('permission_error_msg', 10); // For 10 Seconds
            $this->load->view('dashboard', array($title, $data));
        } ?>

View

<?php if ($this->session->tempdata('permission_error_msg')) { ?>
    <b class="login_error_msg"><?php
        if (!empty($this->session->tempdata('permission_error_msg'))) {
            echo $this->session->tempdata('permission_error_msg');
        }
        ?></b>
<?php } ?>

Thank You.

Upvotes: 0

Madhur
Madhur

Reputation: 1772

You are creating the session for user information

Try to fetch the session data :

echo $this->session->userdata('email');

or trying to pass the data in views $data

Upvotes: 2

Related Questions