如何在Codeigniter中获取逗号分隔的字符串并与会话ID进行比较

时间:2018-12-08 09:06:34

标签: php mysql codeigniter ipad

小伙子们,我在CodeIgniter SQL查询中需要帮助, 我有一个日历表和ID存储区,例如1,2,3....。所以现在我想要记录与CodeIgniter中的会话ID相匹配的记录,因此我该如何做 我在这里放了桌子图片,所以请检查一下 Calender Table 那么我如何拆分此ID并与会话ID进行比较

    <?php
public function projectuser()
{
    $data['test'] = $this->main_model->getproject();
    $data['get_cat'] = $this->main_model->getprojectid();
    $this->load->view('projectuser',$data);
}

?>

型号:

    public function getproject() {

  $data = $this->session->userdata('user_id');
  $this->db->select("*");
  $this->db->from("user_registration");
  $this->db->join('calendar','user_registration.user_id=calendar.user_id', 'INNER');
  $this->db->where('user_registration.user_id', $data);
  $this->output->enable_profiler(TRUE);
  $query = $this->db->get();
  return $query->result();
}

public function getprojectid() {
    $this->db->select("*");
    $this->db->from("calendar");
    $query = $this->db->get();
    return $query->result();
}

查看:

   foreach ($test as $val) {
            $array = explode(",", $val->user_id); // GET ALL IS
            echo $val->title .'&nbsp;&nbsp;';
          }

          foreach ($get_cat as $key => $value) {

            if (in_array($value->user_id, $array)) {
             echo $value->user_id .'&nbsp;&nbsp;<br/>'; //COMPARE WITH SESS

           }
         }

1 个答案:

答案 0 :(得分:0)

您可以使用explode将逗号分隔的字符串转换为数组,然后使用in_array检查会话ID是否在该数组中

如果已经查询过该字符串

$array = explode(',', $comma_separated_values);
if(in_array($session_id, $array)
{
    // do your magic
}

但是如果您想执行sql查询,可以尝试以下操作:

SELECT whatever WHERE FIND_IN_SET($session_id, comma_separated_field)


我已在此处更正您的代码:

foreach ($get_cat as $key => $value) 
{
    $array = explode(',', $value->user_id);
    if (in_array($data->user_id, $array))
    {
        echo $value->user_id .'&nbsp;&nbsp;<br/>'; //COMPARE WITH SESS
    }
}