Search code examples
phpcodeigniterurlprofile

codeigniter : user profile page


Hello i have problem with the user profile page in codeigniter . So here is my user controller :

  <?php
class User extends CI_Controller {
    function users() {
        parent::user();
    }
    function index($id = null) {
    if($id == null) {
      redirect('/', 'refresh');      
    }
else {
    $data['title']  = 'User Page';
    $data['result'] = $this->users_model->get_user_info();
    $data['id'] = $id; 
    $data['main_content'] = "main/profile_view" ;
    $this->load->view('home',$data);
}
}
}
?>

And this is the function in the model :

public function get_user_info(){
        $this->db->where('id' , 'id');
        $q = $this->db->get('users');
        if ($q->num_rows > 0) {
            return $q->result();
        } else {
            return false;

        }
    }

And this is the routes file :

$route['user/(:any)'] = "user/index/$1";

i get this error in localhost/cc/user/1 
Fatal error: Call to a member function get_user_info() on null in C:\xampp\htdocs\cc\application\controllers\user.php on line 12

And i want to know how to display the user data in the view


Solution

  • in your controller i think this line:

    $data['result'] = $this->users_model->get_user_info();
    

    should be:

    $data['result'] = $this->users_model->get_user_info($id);
    

    and in your model this:

    public function get_user_info(){
            $this->db->where('id' , 'id');
    

    should be this:

    public function get_user_info($id){
            $this->db->where('id' , $id);
    

    // ================= next question

    i tried it , but when i enter a non exist id after user/. , it displays the profile vue

    yeah i was going to wrap it in an IF but then got lazy -- here's one way check if the result did not come back -- so after you check to see if $id is NULL -- if $data['result'] did not come back from the model then go to a method like showUserNotFound($id).

    elseif ( ! $data['result'] = $this->users_model->get_user_info($id) ){
    
         $this->showUserNotFound($id); } 
    
    else {    $data['title']  = 'User Page';
        $data['id'] = $id; 
        $data['main_content'] = "main/profile_view" ;
        $this->load->view('home',$data); }