php - Where_in 在 codeigniter 中

标签 php codeigniter codeigniter-3 query-builder

我正在使用 Codeigniter 3 构建网站。但是现在我在从数据库中获取数据时遇到了问题。

这是我的模型:

public function Get($arr_condition = null)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}

在 Controller 中,我使用数组 ($arr_condition) 获取条件列表:

$arr_condition['Cus_team'] =  1;
$arr_condition['Cus_user != '] =  2;
$arr_condition['Cus_phone LIKE'] = 09900000;
$arr_condition['Cus_role >'] = 1;

$result = $this->M_cus_dal->Get($arr_condition);

在我需要 Where_in 条件之前,我的代码工作正常,我试过:

$arr_condtion['Cus_id IN']= array('1,2,3');

但是,它不起作用。

请给我一个解决这个问题的理想。

最佳答案

希望这对您有帮助:

你可以这样做:

在 Controller 中用另一个参数传递你的 where_in 条件

$where_in = array('1,2,3');
$result = $this->M_cus_dal->Get($arr_condition,$where_in);

在模型方法中,将 $where_in 作为第二个参数添加到 where_in 子句中,如下所示:

public function Get($arr_condition = null,$where_in = NULL)
{
     if(!is_null($arr_condition))
     {
        $this->db->where($arr_condition);
     }
     if(! empty($where_in))
     {
       $this->db->where_in('Cus_id',$where_in);
     }
     $query = $this->db->get('cus');
     if($query->num_rows()>0)
     {
        return $query->result_array();
     }
     return false;
}

关于php - Where_in 在 codeigniter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50861360/

相关文章:

php - 在 CodeIgniter 中的 View 中包含 View 的最佳方法

php - Codeigniter-如何在删除index.php后加载新页面

javascript - 使用 PHP 从较大字符串中的 URL 获取查询字符串值

php - 运行 PHP - 使用 Node 实例和端口时

php - 如何从 jquery ajax 调用 codeigniter 函数

.htaccess - 用于 SSL 插入 index.php 的 Codeigniter .htaccess 文件

php - 我们可以合并两个查询,将结果返回为行

php - 在 Codeigniter + PostgreSql 中使用 $this->db->num_rows()

php - 从 mySQL JOIN 语句返回重复值。帮助?

php - Cakephp加入查询属于