简体   繁体   中英

Displaying the information on the profile page of a logged in user using Codeigniter?

please this is my first time using CodeIgniter and I'm having issue with displaying the profile of a logged in user(right now only the email address is displaying, the name, surname and phone number aren't displaying, which are the remaining details from the database I want to display on the profile page) please I would love to know where I'm wrong. this is my code so far: Controller-- User.php

    class user extends CI_Controller
    {
        private $userid;

        public function __construct()
         {
    
           parent::__construct();
           if (!isset($_SESSION['user_logged'])){
              $this->session->set_flashdata("error", "please login to view this page");
              redirect("auth/login");
            }
           $this->userid = $this->session->userdata("id");
        }
    public function profile($userid)
       {
          $this->db->select('*');
          $this->db->from('users');
          $this->db->where('id', $userid);
          $query = $this->db->get();
          if ($this->db->affected_rows()) {
             return $query->row();
           }
    
       $this->load->view('profile');
      }
    }

this is the Auth code for only login in the controller folder

Controller-- Auth.php

    class Auth extends CI_Controller
      {
          public function login()
            {
                 $this->form_validation->set_rules('password', 'password','required|min_length[5]');

          if ($this->form_validation->run() == TRUE) {
            $email = $_POST['email'];
            $password =md5($_POST['password']);

            //check user in database
            $this->db->select('*');
            $this->db->from('users');
            $this->db->where(array('email' => $email, 'password' => $password));
            $query = $this->db->get();

            $user = $query->row();

             //if user exist
            if ($user->email) {
                $this->session->set_flashdata("success", "logged in");

             //session variables
               $_SESSION['user_logged'] = TRUE;
               $_SESSION['email'] = $user->email;

            //redirect to profile page
               redirect("user/profile", "refresh");

              }
              else{
                 $this->session->set_flashdata("error", "wrong details");
                redirect("auth/login", "refresh");
              }


           }
          $this->load->view('login');
       }

this is the UserModel.php code in the model folder

    class UserModel extends ci_Model{
        public function user_logged($userid){
          $this->db->select('*');
          $this->db->from('users');
          $this->db->where("id",$userid);

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

         if($result->num_rows()>0)
           {
             return $result ->rows();
           }
       }
    }

First you have to be familiar with Model-View-Controller approach.

public function profile($userid)
{
    # Load your Model
  $this->load->model("UserModel");

  # Get Your user profile from model, save it to array
  $data['profile'] = $this->UserModel->user_logged($userid);

  # Load your view, together with your array that contain the user info
  $this->load->view('login', $data);
}

On your View Page, you echo your user info using the variable $profile ($data['profile']), like so

<h1><?php echo $profile->name ?></h1>

Documentation is your friend. Its all there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM