【问题标题】:Retrieve data based on user id (CodeIgniter)根据用户 id 检索数据(CodeIgniter)
【发布时间】:2018-11-01 04:51:07
【问题描述】:

我对 php 还很陌生,所以请多多包涵。我的数据库中有两个表,usersonlineform 表。 onlineform 表将userid 字段作为users 表中id 字段的外键。我试图根据当前用户登录显示来自onlineform 表的数据。问题是,它显示的是所有用户的数据,而不是登录用户的数据。我在form_model 中设置了一个查询,但我不知道查询是否正确

这是模型

form_model.php

function getEmployees() 
{
    $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
        onlineform.out1, onlineform.in2, onlineform.out2");
    $this->db->from('users');
    $this->db->join('onlineform','users.id = onlineform.userid', 'left');
    $this->db->order_by('date asc');
    $query = $this->db->get();
    return $query->result();
}

这是控制器

form.php

public function table()
{
    if($this->session->userdata('username') != '')  
       {     
            $this->load->model('form_model');
            $query = $this->form_model->getEmployees();
            $data['EMPLOYEES'] = null;
            if($query) {
                $data['EMPLOYEES'] =  $query;
            }
       } 

    else  
       {  
            redirect(base_url() . 'index.php/form/login');  
       }

            $this->load->view('table.php', $data);
}

这是我的观点

table.php

<?php 
        foreach($EMPLOYEES as $employee)
          {?>
         <tr>
          <td align="center" 
          <?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
            <strong><?=$employee->day;?></strong>
          </td>
          <td align="center" 
          <?php if($employee->day === "SAT" || $employee->day === "SUN"): ?> style="background-color:#A9A9A9; color: white;" <?php endif; ?>>
            <strong><?=$employee->date;?></strong>
          </td>
          <td><?=$employee->out1;?></td>
          <td><?=$employee->in1;?></td>
          <td><?=$employee->out2;?></td>
          <td><?=$employee->in2;?></td>
         </tr>     
        <?php }?>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    可以通过getEmployees($userid)将当前登录的userId传入模型

    $query = $this->form_model->getEmployees($userid); 
    

    然后在查询中使用它来获取与登录用户匹配的当前 userId,到数据库中的用户;

    function getEmployees($userid) 
    {
        $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
        onlineform.out1, onlineform.in2, onlineform.out2");
        $this->db->from('users');
        $this->db->where('onlineform','users.id',$userid); //add this line
        $this->db->join('onlineform','users.id = onlineform.userid', 'left');
        $this->db->order_by('date asc');
        $query = $this->db->get();
        return $query->result();
    }
    

    然后正常传递给你的视图。

    希望这会有所帮助。

    【讨论】:

    • 现在它给了我这个错误,消息:为 foreach() 提供的参数无效
    • 谢谢你。它可以工作,只需要对代码进行一些调整。非常感谢老兄
    【解决方案2】:

    您可以将当前登录的用户 ID 保存在会话中,然后在从数据库中检索数据时使用它。 将您的 form_model.php 代码更改为此

    function getEmployees() 
    {
        $this->db->select("users.id, onlineform.date, onlineform.day, onlineform.in1, 
            onlineform.out1, onlineform.in2, onlineform.out2");
        $this->db->from('users');
        $this->db->join('onlineform','users.id = onlineform.userid', 'left');
        $this->db->where("users.id", $this->session->userdata('current_user_id'));
        $this->db->order_by('date asc');
        $query = $this->db->get();
        return $query->result();
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2016-02-27
      • 2014-01-02
      相关资源
      最近更新 更多