Reputation:
My problem is my data are being fetched at once, all of them. I need to view each user's data when Im on different logged in user. For example: For user1
I need to have Product1
and user2
I need to have Product2
but right now all Im getting is I can fetch all user's data.
here is my Model
As you can see I added my join query but still fetching all my data
public function showAllReviewers(){
$this->db->join('teachers', 'teachers.id = reviewers.user_id');
$query = $this->db->get('reviewers');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
My Controller
public function showAllReviewers()
{
$result = $this->reviewer_model->showAllReviewers();
echo json_encode($result);
}
My View
<tbody id="showdata">
</tbody>
I'm using ajax/js to fetch my data so here is my ajax/js script for additional info
//function
function showAllReviewers(){
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>reviewers/showAllReviewers',
async: false,
dataType: 'json',
success: function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html +='<tr class="table-info">'+
'<td>'+data[i].subject+'</td>'+
'<td>'+data[i].category+'</td>'+
'<td>'+data[i].set_rev+'</td>'+
'<td>'+data[i].group_name+'</td>'+
'<td>'+
'<a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].id+'"> <span class="iconify" data-icon="bx:bx-edit" data-inline="false"></span> </a> '+
'<a href="javascript:;" class="btn btn-danger item-delete" data="'+data[i].id+'"> <span class="iconify" data-icon="bx:bx-trash" data-inline="false"></span> </a>'+
'</td>'+
'</tr>';
}
$('#showdata').html(html);
},
error: function(){
alert('Could not get Data from Database');
}
});
}
EDIT: Login Controller
// Log in teacher
public function login(){
$this->form_validation->set_rules('code', 'Code', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('teachers/login');
$this->load->view('templates/footer');
} else {
// Get code
$code = $this->input->post('code');
// Get and encrypt the password
$password = $this->input->post('password');
// Login user
$user = $this->teacher_model->login($code, $password);
if($user){
// Create session
$user_data = array(
'user_id' => $user->id,
'name' => $user->name,
'code' => $code,
'logged_in' => true
);
$this->session->set_userdata($user_data);
redirect('teacher/home');
} else {
redirect('teachers/login');
}
}
}
Upvotes: 1
Views: 719
Reputation: 17805
Well, you can store user data like id
etc in session when user logs in, like below in your controller:
$this->session->set_userdata('user_details', $user_data); // say user_details is the key we store it in
In your model, you can do like below:
<?php
public function showAllReviewers(){
$this->db->join('teachers', 'teachers.id = reviewers.user_id');
$this->db->where('reviewers.user_id',$this->session->userdata('user_details')['user_id']);
$query = $this->db->get('reviewers');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
Or an even better approach is to have a private variable say user_id
and set the value in the constructor. This would make sure that whenever you are accessing model instance in your controller, you already have it in your model instead of retrieving from session always.
<?php
class YourModel extends CI_Model{
private $user_id;
function __construct(){
$this->user_id = $this->session->userdata('user_details')['user_id'];
}
public function showAllReviewers(){
$this->db->join('teachers', 'teachers.id = reviewers.user_id');
$this->db->where('reviewers.user_id',$this->user_id);
$query = $this->db->get('reviewers');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
}
Upvotes: 1
Reputation: 598
You must add a where condition in showAllReviewers() function's Sql query by passing current logged in User/Teacher Id Or in case of CI you can fetch the same using isLogged/getId function which used to be present in system/library/customer.
Upvotes: 1