【问题标题】:Where clause based multiple dynamic values, CodeIgniterWhere 子句基于多个动态值,CodeIgniter
【发布时间】:2015-10-01 04:05:10
【问题描述】:

下拉框是根据用户分配给的客户动态填充的。问题是,当我想搜索分配给用户的客户时(如果他们选择查看All 客户的选项),我必须执行如下查询:

查询 1:

SELECT * FROM LotDispDashBoardDB.disp_dummy where (customer = 'DL1' or customer = 'BC1') and stage like 'T%' and lot_status like 'close%';

我不想做

查询 2:

SELECT * FROM LotDispDashBoardDB.disp_dummy where stage like 'T%' and lot_status like 'close%';

因为我希望用户只能看到他们自己分配的客户,所以我不希望他们看到所有可用的客户。

下拉框只有一个固定值All,其余为个别客户名称。如果用户选择All 表示他想查看All 他分配的客户。在下面的控制器中,如果他选择所有我会得到$all_customer 数组。如果不是,我只会选择客户。

在模型中知道它是否全部,我检测接收到的值是否是一个数组。如果它是一个数组,我会遍历它并使用 CodeIgniter 的活动记录$this->db->or_where('customer', $customer); 进行查询。 主要问题是查询。如前所述,我尝试像查询 1 一样。如何放置括号并动态编写 where 子句?

控制器:

public function load_lot_table()
{
    // Get customers from session
    $customer_session = $this->session->userdata['logged_in']['customers'];

    foreach($customer_session as $cust_object)
    {
        $all_customer[] = $cust_object->customer;
    }

    // Get form values
    if($this->input->get())
    {
        $selected_customer = $this->input->get('cust_drop_down');
        $stage = $this->input->get('area_drop_down');
        $lot_status = $this->input->get('status_drop_down');
        $search_lot = ltrim( $this->input->get('search_lot') );

        if($selected_customer == 'all')
        {
            $customer = $all_customer;
        }
        else
        {
            $customer = $selected_customer;
        }
    }
    else
    {
        // Default values when page is loaded because form is not submitted yet
        $customer = $all_customer;
        $stage = 'all';
        $lot_status = 'all';
        $search_lot = '';
    }

    // Keys are the column names
    $search = array(
    'customer' => $customer,
    'stage' => $stage,
    'lot_status' => $lot_status,
    'search_lot' => $search_lot
    );

    // Paginate retrieved lots
    $results = $this->paginate_create_table($search);

    /*** other codes ***/

}

private function paginate_create_table($search = null)
{
    if( is_array( $search['customer'] ) )
    {
        $customer = 'all';
    }
    else
    {
        $customer = $search['customer'];
    }

    // Set pagination configuration
    $config = array();
    $config['base_url'] = base_url()."index.php/Home/on_hold_lot";
    $config['suffix'] = '?cust_drop_down='.$customer.'&area_drop_down='.$search['stage'].'&status_drop_down='.$search['lot_status'].'&search_lot='.$search['search_lot'];
    $config['first_url'] = $config['base_url'].$config['suffix'];
    $config['total_rows'] = $this->home_model->fetch_lots('rows', $search);
    $config['per_page'] = 10;
    $config['uri_segment'] = 3;

    /*** Other codes ****/
}

型号:

public function fetch_lots($return, $search = null, $limit = null, $start = 0)
{
    if($limit != null)
    {
        $this->db->limit($limit, $start);
    }

    if($search != null)
    {
        if( $search['customer'] != 'all' && !is_array( $search['customer'] ) )
        {
            $this->db->where('customer', $search['customer']);
        }
        elseif( is_array( $search['customer'] ) )
        {
            foreach($search['customer'] as $customer)
            {
                $this->db->or_where('customer', $customer);
            }
        }

        if($search['stage'] != 'all')
        {
            $this->db->like('stage', $search['stage'], 'after');
        }

        if($search['lot_status'] != 'all')
        {
            $this->db->like('lot_status', $search['lot_status'], 'after');
        }

        if($search['search_lot'] != '')
        {
            $this->db->where('utac_lot', $search['search_lot']);
        }
    }
    /*** Other codes ***/
}

【问题讨论】:

    标签: php mysql arrays codeigniter activerecord


    【解决方案1】:

    Codeigniter 允许您将pass a string 转换为$this->db->where()

    例子:

    $where = "(this = $that)";
    $this->db->where($where);
    

    当您遍历$search['customer'] 时,您可以构建一个字符串以用作您的where 查询。

    if( $search['customer'] != 'all' && !is_array( $search['customer'] ) )
        {
            $this->db->where('customer', $search['customer']);
        }
        elseif( is_array( $search['customer'] ) )
        {
            for($i = 0, $i <= count($search['customer'], $i++)
            {
                // Use a FOR instead of FOREACH so we can count the loops.
                // 'WHERE' will be used on the first loop,
                // subsequent loops use 'OR'.
                if($i == 0) 
                {
                    $where = "( WHERE customer = $search['customer'][$i] ";
                } else {
                    $where .= " OR customer` = $search['customer'][$i] ";
                }
                if($i == count($search['customer']))
                {
                    $where .= " ) ";
                }
            }
            $this->db->where($where);
        }
    //... etc..
    }
    

    【讨论】:

    • 谢谢,但我想我找到了更简单的解决方案。将发布它,让我知道你的想法。
    【解决方案2】:

    这是我想出的解决方案。

    // If user selects a customer
    if( $search['customer'] != 'all' && !is_array( $search['customer'] ) )
    {
        $this->db->where('customer', $search['customer']);
    }
    // If user selects ALL
    elseif( is_array( $search['customer'] ) )
    {
        foreach($search['customer'] as $customer)
        {
            $all_customer[] = "customer = '$customer'";
        }
    
        // Example query would form: (customer = 'ABC' OR customer = 'BCD')
        $where_customer = "(";
        $where_customer .= implode(' OR ', $all_customer);
        $where_customer .= ")";
    
        $this->db->where($where_customer);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2014-05-10
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多