Mirza Chilman
Mirza Chilman

Reputation: 429

get user data from its session Codeigniter

TABLES

Pengguna Pengguna

MAHASISWA Mahasiswa

I have 2 tables, Pengguna and Mahasiswa, table penggunais a table contain data for user that can be log in into the website while table mahasiswa contain user data such as name, email, telephone etc.

then i want user that has been logged in to show their credential information that has been exist in table mahasiswa. let's say i logged in with username "123" with id_pengguna "3" that's mean that my name is "Mirza" and my nim is "123" and etc.

the question is how can i set the session of the logged user and get the user information from table mahasiswa? all i can do is show the first table from mahasiswa instead getting the data from the user logged in session, below are the controller, model and view

Model

<?php
   defined('BASEPATH') OR exit('No direct script access allowed');

   class MProfile extends CI_Model{
       function __construct(){
           parent::__construct();
           $this->load->database();
       }
       function read($cond, $ordField, $ordType){
          if($cond!=null){
             $this->db->where($cond);
          }
          if($ordField!=null){
             $this->db->order_by($ordField, $ordType);
          }
       $query = $this->db->get('mahasiswa');
       return $query;
       }
   }

CONTROLLER

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profil extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->load->model('MProfile');

    }

    public function index(){
        $data['menu4'] = true;
        /*$this->sessionOut();*/
        $this->session->userdata('idpengguna');
        $data['query'] = $this->MProfile->read(null, 'nim', 'ASC');

        $this->load->view('layouts/header');
        $this->load->view('profil_page', $data);
        $this->load->view('layouts/footer');
    }

}

VIEW

<?php 
    if(isset($this->session->userdata['logged_in'])){
        $nim = $this->session->userdata['loggedin']['nim'];
    }
?>
<ul class="category-t">
    <?php foreach ($query->result() as $result){}?> 
    <li><?php echo $result->nim;?></li>
    <li><?php echo $result->nama_mahasiswa;?></li>
    <li><?php echo $result->email;?></li>
    <li><?php echo $result->telepon;?></li>
</ul>

Upvotes: 1

Views: 2375

Answers (2)

Yoshioka
Yoshioka

Reputation: 815

Its simple, you can create a new query selecting data from your mahasiswa table.

Add this to your controller

$data["row"] = $this->Mprofile->getmahasiswa();

In your model

function getmahasiswa(){
   $idpengguna = $this->session->userdata('idpengguna');

   $this->db->where('id_pengguna', $idpengguna);
   $q = $this->db->get('mahasiswa');
   return $q->row();
}

And in your view

<ul class="category-t">
   <li><?php echo $nim;?></li>
   <li><?php echo $nama_mahasiswa;?></li>
   <li><?php echo $email;?></li>
   <li><?php echo $telepon;?></li>
</ul>

Add this at the top of your view

<?php
   if ($row){
       $nim= $row->nim;
       $nama_mahasiswa = $row->nama_mahasiswa;
       $email = $row->email;
       $telepon= $row->telepon;
   } else {
       $nim =
       $nama_mahasiswa =
       $email = 
       $telepon = "";
   }
?>

Upvotes: 1

NomanJaved
NomanJaved

Reputation: 1380

You can run this query in model and will get the data from both the tables and then can use it. send id to the model of the id_pengguna. This id will be the user id for comparison
Model

function getdata($id){
    $query = "select peng.*, masha.* 
    from pengguna peng join mashasiswa masha 
    on id_pengguna where masha.id_pengguna = $id";
    $query = $this->db->query($query);
    return $query->result_array();
}

Controller

$data['result'] = $this->model_name->getdata($id);

Hope this will help.

Upvotes: 1

Related Questions