1

How can I show customer's or user's information such as Name and Surname on the header?

Firstly, I have a login page. Customer and User can login to the dashboard on the same login page. While login process, it uses 2 database tables such as User and Customer. Login process system takes data from 2 database tables when customer and user login to dashboard.

Model of LOGIN:

private $table = "user, customer";
    private $_data = array();

    public function validate()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        // Username or Email Sending Settings
        $this->db->where("(user.userEmail = '$username' OR user.userUsername = '$username') OR (customer.cosEmail = '$username' OR customer.cosUserName = '$username')"); //
        $query = $this->db->get($this->table);

        if ($query->num_rows())
        {
            // found row by username    
            $row = $query->row_array();

            // now check for the password
            if ($row['userPass'] == $password OR $row['cosPassword'] == $password)
            {
                // we not need password to store in session
                unset($row['userPass' OR 'cosPassword']);
                $this->_data = $row;
                return ERR_NONE;
            }

            // password not match
            return ERR_INVALID_PASSWORD;
        }
        else {
            // not found
            return ERR_INVALID_USERNAME;
        }
    }

    public function get_data()
    {
        return $this->_data;
    }

And I want to show information of customer or user on the header. When customer login, the customer can see own information or when user login, the user can see own information in the same dashboard.

For this, I use SessionData of Codeigniter. View Header:

<li class="dropdown">
                <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                    <img alt="" src="<?php echo base_url().'upload/user/'.$this->session->userdata('userImg'); ?>">
                    <span class="username"><?php echo $this->session->userdata('userName'); ?>  <?php echo $this->session->userdata('userSurname'); ?></span>
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu extended logout">
                    <div class="log-arrow-up"></div>
                    <li><a href="<?php echo base_url(); ?>profile"><i class=" fa fa-suitcase"></i>Profile</a></li>
                    <li hidden><a href="#"><i class="fa fa-cog"></i> Settings</a></li>
                    <li hidden><a href="<?php echo base_url(); ?>notifications"><i class="fa fa-bell-o"></i> Notification</a></li>
                    <li><a href="<?php echo base_url(); ?>logout"><i class="fa fa-key"></i> Log Out</a></li>
                </ul>
            </li>

But I can't show anything when customer login. Because I don't know how can I show customer information when customer login. When customer login dashboard, I see user information again. How can I set this what I want? Help me, It is so important.

I tried it:

$this->session->userdata('userName') || $this->session->userdata('cosName')

But system shows me 1 on username. It doesn't work.

What can I show information according to customer or user with userdata?

My Login Controller:

public function index()
    {   
        $this->logged_in_check();

        $this->load->library('form_validation');
        $this->form_validation->set_rules("username", "Username", "trim|required");
        $this->form_validation->set_rules("password", "Password", "trim|required");
        if ($this->form_validation->run() == true) 
        {
            $this->load->model('login_model', 'login'); 
            // check the username & password of user
            $status = $this->login->validate();
            if ($status == ERR_INVALID_USERNAME) {
                $this->session->set_flashdata("error", "Username or Email is invalid");
            }
            elseif ($status == ERR_INVALID_PASSWORD) {
                $this->session->set_flashdata("error", "Password is invalid");
            }
            else
            {
                // success
                // store the user data to session
                $this->session->set_userdata($this->login->get_data());
                $this->session->set_userdata("logged_in", true);
                // redirect to dashboard
                redirect("dashboard");
            }
        }   
        $this->load->view("login");
    }
7
  • What you're writing is an if statement; if you want to display something use an echo ... e.g: echo $this->session->userdata('userName') Commented Jun 23, 2018 at 13:59
  • where r u setting your session data , show your code for that also Commented Jun 23, 2018 at 14:02
  • I did not set my session data. How can I set it? Commented Jun 23, 2018 at 14:03
  • I added my login controller. And I already write // echo $this->session->userdata('userName'); @ahmad I explain: When customer login, customer can see own information or when user login, user can see own information on header. What can I do like that ? Commented Jun 23, 2018 at 14:06
  • and then you can get user or customer data from db and then set his session data. Commented Jun 23, 2018 at 17:59

1 Answer 1

1

You can use just this function and remove other function.

 public function index(){   
    $this->load->library('form_validation');
    $this->form_validation->set_rules("username", "Username", "trim|required");
    $this->form_validation->set_rules("password", "Password", "trim|required");
    if ($this->form_validation->run() == FALSE) 
    {
        $this->session->set_flashdata("error", validation_errors());
        $this->load->view("login");
    }
    else{
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user_data = $this->db->query("(SELECT * FROM user WHERE  userUsername = '$username' OR userEmail = '$username' AND userPass = '$password')");
        $cus_data = $this->db->query("(SELECT * FROM customer WHERE  cosUserName = '$username' OR cosEmail = '$username' AND cosPassword = '$password')");
        if ($user_data->num_rows() > 0) {
            foreach ($user_data->result_array() as $row) {
                $this->session->set_userdata('people_name',$row['userUsername']);
                $this->session->set_userdata("logged_in", true);
            }
            // redirect to dashboard
            redirect("dashboard");
        }
        elseif ( $cus_data->num_rows() > 0) {
            foreach ($cus_data->result_array() as $row) {
                $this->session->set_userdata('people_name',$row['cosUserName']);
                $this->session->set_userdata("logged_in", true);
            }
            // redirect to dashboard
            redirect("dashboard");
        }
        else{
            $this->session->set_flashdata("error", "Username or Email is invalid");
            $this->load->view("login");
        }
    }  
}

also in view use this code echo $this->session->userdata('people_name') to show user or customer name.

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.