Php 登录时查看/获取记录的用户数据-CodeIgniter
我的问题是,我的数据正在一次提取,所有这些数据。我需要查看每个用户的数据时,Im在不同的登录用户。例如:对于Php 登录时查看/获取记录的用户数据-CodeIgniter,php,codeigniter,Php,Codeigniter,我的问题是,我的数据正在一次提取,所有这些数据。我需要查看每个用户的数据时,Im在不同的登录用户。例如:对于user1我需要Product1和user2我需要Product2,但现在我得到的只是我可以获取所有用户的数据 这是我的型号 如您所见,我添加了我的联接查询,但仍然获取我的所有数据 我的控制器 public function showAllReviewers() { $result = $this->reviewer_model->showAllReviewers();
user1
我需要Product1
和user2
我需要Product2
,但现在我得到的只是我可以获取所有用户的数据
这是我的型号
如您所见,我添加了我的联接查询,但仍然获取我的所有数据
我的控制器
public function showAllReviewers()
{
$result = $this->reviewer_model->showAllReviewers();
echo json_encode($result);
}
我的视图
<tbody id="showdata">
</tbody>
您必须通过传递当前登录的用户/教师Id在showAllReviewers()函数的Sql查询中添加where条件,或者如果是CI,您可以使用isLogged/getId函数获取相同的条件,该函数以前存在于system/library/customer中。那么,当用户登录时,您可以在会话中存储用户数据,如
Id
等,在控制器中,如下所示:
$this->session->set_userdata('user_details', $user_data); // say user_details is the key we store it in
在您的模型中,您可以执行以下操作:
<?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;
}
}
Im在不同的登录用户上好的,你有这个用户在PHP中的某个地方的ID吗?您可以捕获它并将其传递到模型中,以便仅检索有关此登录用户的数据。您不需要在HTML或JS中更改任何内容。@Aksen P-据我所知,我没有更改。我使用会话检索名称,但事实并非如此。@AksenP-我将编辑我的代码并添加我的登录控制器。当然,我希望您在查看@vivek_23 answer后可以从DB获得有关此用户名的数据。他明白我的意思了您好,我明白您的想法,第二个选项似乎更容易实现-我执行它,然后它会说,
无法从数据库中获取数据
@SummerWinter您可以使用由$this->db->get_compiled_select()
形成的SQL查询进行检查吗?@SummerWinter或者我们可以使用简单的select查询?更多信息你认为问题也在查询中吗?嘿,我们也遇到了同样的问题哈哈,我也改变了它,然后你回答了,先生。谢谢,这很有效!
// 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');
}
}
}
$this->session->set_userdata('user_details', $user_data); // say user_details is the key we store it in
<?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;
}
}
<?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;
}
}
}