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

How to get Comma seperated string in Codeigniter and compare with session id

Guyz 我需要 CodeIgniter SQL 查询方面的帮助, 我有一个日历 table 和 id 存储像 1,2,3.... 所以现在我想记录那些与 CodeIgniter 中的会话 id 匹配的记录,所以我该怎么做 我把我的 table 图片放在这里,请检查一下 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

           }
         }

您可以使用 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
    }
}