php codeigniter中的用户个人资料

问题描述

我正在尝试从“ profile_details”表中获取我的用户信息,并显示在profile.PHP视图中。 这是我的Profile.PHP控制器,目前仅显示视图

<?PHP
class Profile extends CI_Controller
{
 function __construct()
 {
     parent::__construct();  

 }

function index()
{

    $this->load->view('admin/profile/index');
}
 
}

这是profile_model.PHP

<?PHP
class Profile_model extends CI_Model
{
//table name: profile_details

public function __construct()
{
    parent::__construct();
}


function myProfileDetails($username)
{

$query=$this->db->query('SELECT * FROM profile_details WHERE username = $username');
//$this->db->select('*');
//$query= $this->db->get('customer');
return $query->result();

}

}    

这是我的视图(配置文件)index.PHP

 <div class="tab-content profile-tab" id="myTabContent">
                    <div class="tab-pane fade show active" id="About" role="tabpanel" aria-labelledby="home-tab">
                        <div class="row">
                          
                           <div class="form-group col-md-6">
                              <label>First Name</label>
                              <span class="form-control"></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Last Name</label>
                              <span class="form-control">3</span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Email Id</label>
                              <span class="form-control">f</span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Phone</label>
                              <span class="form-control">f</span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Sub-Company Name</label>
                              <span class="form-control">g</span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Role</label>
                              <span class="form-control">t</span>
                            </div>
                    </div>
                        
                </div>

下面是profile_details表

enter image description here

请您帮我看看如何显示信息。

解决方法

一个问题:您要获取所有用户,还是仅获取您显示的身份验证用户?好吧,让我解释一下如何创建myProfile(身份验证用户)。

假设您尚未插入会话用户数据。

您的模型应该是这样的:

<?php
class Profile_model extends CI_Model
{

public function __construct()
{
    parent::__construct();
    $this->load->database();
}


function myProfileDetails($username)
{

$query=$this->db->query("SELECT first_name,last_name,email,mobile_no,role,company_name,username FROM profile_details WHERE username = '$username'");
return $query->row();

}

}  

创建模型后(获取必要的列以进行查看),应在控制器内调用模型:

<?php
class Profile extends CI_Controller
{
 function __construct()
 {
     parent::__construct();  
     $this->load->model('Profile_model');

 }

function index()
{

// this is your session:::
    $user_id = 48 // this is your id,roxana
// or
    $username = 'admin';
//::::::::::::::::::::::::
 
    $data['my_profiles'] = $this->Profile_model->myProfileDetails($username); // my_profiles will become your view variable to fetch data basesd on its column name
    $this->load->view('admin/profile/index',$data);
}
 
}

最后,您的观点:

<div class="tab-content profile-tab" id="myTabContent">
                    <div class="tab-pane fade show active" id="About" role="tabpanel" aria-labelledby="home-tab">
                        <div class="row">
                          
                           <div class="form-group col-md-6">
                              <label>First Name</label>
                              <span class="form-control"><?= $my_profiles->firstname ?></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Last Name</label>
                              <span class="form-control"><?= $my_profiles->lastname ?></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Email Id</label>
                              <span class="form-control"><?= $my_profiles->email ?></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Phone</label>
                              <span class="form-control"><?= $my_profiles->mobile_no ?></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Sub-Company Name</label>
                              <span class="form-control"><?= $my_profiles->company_name ?></span>
                            </div>
                            <div class="form-group col-md-6">
                              <label>Role</label>
                              <span class="form-control"><?= $my_profiles->role ?></span>
                            </div>
                    </div>
                        
                </div>

就是这样。