0

I am trying to get my user information from "profile_details" table and display in on profile.php view. Here is my Profile.php controller which just display the view for now

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

 }

function index()
{

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

And here is 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();

}

}    

And here is my view (profile) 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>

And below is profile_details table enter image description here

Can you please help me how could i get the informations display in my view.

Roxana Slj
  • 311
  • 1
  • 5
  • 23
  • 1
    You should **NEVER** store password unencrypted! You're also wide open to SQL injections. This would mean that everyone with very little knowledge of SQL injections could get access to all you users details and unencrypted passwords. [read more about storing password](https://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords). [read more about SQL injections](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –  Aug 24 '20 at 09:14
  • You are 50% of the way there. This is something that it clearly explained in the CI 3 User guide. So your best bet would be to read that first. https://codeigniter.com/userguide3/general/views.html – TimBrownlaw Aug 24 '20 at 20:12
  • yea that is not my problem right now , i already encrypted my password in login.php controller, my question here is how to get user information( except password, and username )on user profile – Roxana Slj Aug 25 '20 at 09:01

1 Answers1

1

One question: Did you want to fetch all users, or only the authenticate user that you put on view?? Well, Let me explain how to make myProfile (Authenticate User).

Suppose that you haven't inserted your session userdata.

Your model should something like this :

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

}

}  

After you create your model (taking necessary column to fetch to view), you should call your model inside your controller:

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

Last, Your view:

<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>

That's it.