php - Displaying the information on the profile page of a logged in user using Codeigniter?
Get the solution ↓↓↓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();
}
}
}
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.