像 Codeigniter,or_like 不适用于 where

Codeigniter like, or_like doesn't work with where

我在我的 codeigniter 应用程序中发现了一个问题。我使用 4 个不同的 like 和 or_like 编写了一个简单的搜索查询。

$q = $this->db->select('*')
->from('table')
->like('col', $search_string1, both)
->or_like('col', $search_string2, both)
->or_like('col', $search_string3,  both)
->or_like('col', $search_string4, both)
->get()->results

它按我想要的方式工作,但我决定在我的 table 中添加新的名为 "active" 的列,我只想显示 active = 1 的行。所以我尝试添加我的查询在哪里,但它没有像我预期的那样工作。

$q = $this->db->select('*')
->from('table')
->where('active', 1)
->like('col', $search_string1, both)
->or_like('col', $search_string2, both)
->or_like('col', $search_string3,  both)
->or_like('col', $search_string4, both)
->get()->results

此查询还显示活动设置为 0 的行。如何修复它以在 codeigniter 中仅显示活动 = 1 的行?

您好,请问您可以使用下面的代码吗

$q = $this->db->select('*')
->from('table')
->where('active', 1)
->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
->get()->results;

谢谢

有关@Roshan 代码的解释,因为对于在 CI 中学习 SQL 的人来说,答案可能并不明显。 (我还修复了代码中的错误。)

$q = $this->db
              // ->select('*') // you don't need select('*') if you want everything
              // ->from('table') // Don't need this either
              ->where('active', 1)
              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
              ->get('table') // add your table name here
              ->result();  // it's not "->results"

因此您的最终查询将如下所示:

$q = $this->db->where('active', 1)
              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
              ->get('table')
              ->result(); 

这是上面的问题:

"get all results from 'table' 
    where active = 1 AND `col` = search_string1
 OR where active = 1 AND `col` = search_string2
 OR where active = 1 AND `col` = search_string3
 OR where active = 1 AND `col` = search_string4

 Then assign the result object to $q

一种更简单的查看方式,但更复杂的代码编写方式(但可能更容易理解):

$q = $this->db
            // where active=1 AND col LIKE %$search_string1%
             ->where('active', 1)
             ->like('col', $search_string1, 'both')

            // OR where active=1 AND col LIKE %$search_string2%
             ->or_where('active', 1)
             ->like('col', $search_string2, 'both')

             // OR where active=1 AND col LIKE %$search_string3%
             ->or_where('active', 1)
             ->like('col', $search_string3, 'both')

             // OR where active=1 AND col LIKE %$search_string4%
             ->or_where('active', 1)
             ->like('col', $search_string4, 'both')

            ->get('table')
            ->result();

有时您需要这样做。例如,if active 可能是几个状态。 onoffpending 并且您需要检查该字段的特定状态。在你的情况下没有必要,因为 active 总是 1,但是如果 active 可能 是你想要的不止一件事,你必须把它拼出来您的查询生成器。

您也可以使用 query grouping

代码将如下所示:

$this->db->where('active', 1);
$this->db->group_start();
$this->db->or_like('col', $search_string1, both)
$this->db->or_like('col', $search_string2, both)
$this->db->or_like('col', $search_string3,  both)
$this->db->or_like('col', $search_string4, both)
$this->db->group_end();

$q = $this->db->get('table')->result();

使用 group_start 和 group_end 确保 or_like 之间的这部分语句将进入括号。

如果您要使用数据表服务器端处理,您可能也会遇到与此相关的问题,但这是解决方法。

if(!empty($search_val)) {
        $x=0;
        $this->db->group_start();
        foreach($columns as $s_col) {
            if($x==0) {
                $this->db->like($s_col,$search_val);
            }else {
                $this->db->or_like($s_col,$search_val);
            }
            $x++;
        }
        $this->db->group_end();               
    }