dongzhanlian6289 2015-11-30 13:24
浏览 48

如何使用'if'条件在'where'条件中的codeigniter中应用过滤器?

I am trying to apply a filter using codeigniter, but it's not working. It works for a single array record, but when I send multiple comma-separated source parameters it doesn't filter.

Here is my code in model.

    $user_id = $this->input->post('user_id');
    $source = $this->input->post('source');
    $source = explode(',', $source);

    $radius = $this->input->post('radius');

    $this->db->select('post.*, source.*, user.firstname, user.lastname');
    $this->db->from('post');
    $this->db->join('user', 'user.id = post.cop_id');
    $this->db->join('source', 'post.source_id = source.id');
    $this->db->where('post.cop_id', $user_id);
    if (in_array("1", $source)) {
        $this->db->where('post.source_id', 1);
    }
    if (in_array("2", $source)) {
        $this->db->where('post.source_id', 2);
    }

    if (in_array("3", $source)) {
        $this->db->where('post.source_id', 3);
    }

    if (in_array("4", $source)) {
        $this->db->where('post.source_id',4);
    }
    if (!empty($radius)) {
        $this->db->where('post.post_radius', $radius);
    }
    $this->db->where('post.source_id !=', 4);
    $query = $this->db->get();
    return $query->result();
  • 写回答

1条回答 默认 最新

  • doutao8774 2015-11-30 13:29
    关注

    Use where_in

     $this->db->select('post.*, source.*, user.firstname, user.lastname');
        $this->db->from('post');
        $this->db->join('user', 'user.id = post.cop_id');
        $this->db->join('source', 'post.source_id = source.id');
        $this->db->where('post.cop_id', $user_id);
        $this->db->where_in('post.source_id', $source);// it will produse  IN ('1', '2', '3')
        $this->db->where('post.post_radius', $radius);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么