簡體   English   中英

使用 Codeigniter 在登錄用戶的個人資料頁面上顯示信息?

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

請這是我第一次使用 CodeIgniter,我在顯示登錄用戶的個人資料時遇到問題(現在只顯示電子郵件地址,不顯示姓名、姓氏和電話號碼,這是其余的詳細信息從數據庫中我想顯示在個人資料頁面上)請我很想知道我錯在哪里。 到目前為止,這是我的代碼: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');
      }
    }

這是僅在控制器文件夾中登錄的授權碼

控制器——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');
       }

這是模型文件夾中的 UserModel.php 代碼

    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();
           }
       }
    }

首先,您必須熟悉模型-視圖-控制器方法。

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);
}

在您的查看頁面上,您使用變量 $profile ($data['profile']) 回顯您的用戶信息,如下所示

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

文檔是您的朋友。 它的全部在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM