【问题标题】:CodeIgniter Building LIKE Query with n Number of Array ElementsCodeIgniter 使用 n 个数组元素构建 LIKE 查询
【发布时间】:2016-04-24 00:19:15
【问题描述】:

考虑这个代码块,其中 switch 语句从 ajax 请求中获取参数,然后将其转换为列名,并将列名传递给 CI or_like 查询。但是请注意,如果参数是“地址”,我将要比较多个查询参数,包括街道地址、方向指示符、街道类型(街道、大道、大道等)、城市、邮政编码、以及地址的所有粒子,每个粒子在名为“assets”的表中都有对应的列名:

    $field = array();
    switch($params['field']){
        case "Owner" :
                        $field[0] = "owner_name";
                        break;
        case "Owner phone" :
                        $field[0] = "owner_phone";
                        break;
        case "Listing agent" :
                        $field[0] = "listing_agent";
                        break;
        case "Listing date" :
                        $field[0] = "list_date";
                        break;
        case "Address" :
                        $field[0] = "as_st_number";
                        $field[1] = "as_st_dir";
                        $field[2] = "as_st_name";
                        $field[3] = "as_st_desig";
                        $field[4] = "as_unit_num";
                        $field[5] = "as_city";
                        $field[6] = "as_city";
                        $field[7] = "as_state";
                        break;
    }
    $array = array( 
                    $field => $match
                   );
    $query = $this->db->select('*')->from('assets')->or_like($array)->get();

执行查询会导致此错误:

Error: 200 , "
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Illegal offset type</p>
<p>Filename: models/Assets_model.php</p>
<p>Line Number: 743</p>


    <p>Backtrace:</p>

显然查询语句不喜欢$field =&gt; $match 中的$field 是一个数组,而是需要它是单个值。

我可以用这样的东西填充每个案例:

        case "Address" :
                        $field[0] = "as_st_number";
                        $field[1] = "as_st_dir";
                        $field[2] = "as_st_name";
                        $field[3] = "as_st_desig";
                        $field[4] = "as_unit_num";
                        $field[5] = "as_city";
                        $field[6] = "as_city";
                        $field[7] = "as_state";
                        $array = array(
                                        $field[0] => $match,
                                        $field[1] => $match,
                                        $field[2] => $match,
                                        $field[3] => $match,
                                        $field[4] => $match,
                                        $field[5] => $match,
                                        $field[6] => $match,
                                        $field[7] => $match
                                      );
                        break;

...但这似乎不优雅。如果问题需要澄清,请说出来。

【问题讨论】:

    标签: mysql codeigniter codeigniter-2


    【解决方案1】:

    试试这个:

      switch($params['field'])
      {
        case "Owner" :
          $this->db->like("owner_name", $match);
          break;
        case "Owner phone" :
          $this->db->like("owner_phone", $match);
          break;
        case "Listing agent" :
          $this->db->like("listing_agent", $match);
          break;
        case "Listing date" :
          $this->db->like("list_date", $match);
          break;
        case "Address" :
          // setup the first like
          $this->db->like("as_st_number", $match);
          // figure out the or_like particles
          $field_names = [
              "as_st_dir", "as_st_name", "as_st_desig",
              "as_unit_num", "as_city", "as_city", "as_state"
          ];
          foreach($field_names as $name)
          {
            $others[$name] = $match;
          }
          $this->db->or_like($others);
          break;
      }
    
      $query = $this->db->get('assets'); //does a select * from assets
    

    或者这可能更简洁一些。

    if($params['field'] === "Address)
    {
       $this->db->like("as_st_number", $match);
    
       // setup the or_like array
       $field_names = ["as_st_dir", "as_st_name", "as_st_desig",
                     "as_unit_num", "as_city", "as_city", "as_state"];
    
       foreach($field_names as $name)
       {
          $others[$name] = $match;
       }
       $this->db->or_like($others);
    }  
       else 
    {
      switch($params['field'])
      {
        case "Owner" :
          $field = "owner_name";
          break;
        case "Owner phone" :
          $field = "owner_phone";
          break;
        case "Listing agent" :
          $field = "listing_agent";
          break;
        case "Listing date" :
          $field = "list_date";
          break;
      }
      $this->db->like($field, $match);
    }
    
    $query = $this->db->get('assets');
    

    【讨论】:

    • 我看到了。另一种可能的变体可以满足我的要求,谢谢。
    【解决方案2】:

    可能最好循环遍历数组来构建查询

    $this->db->select('*')->from('assets');
    foreach($array as $field => $search){
        $this->db->or_like($field,$search);
    }
    $results = $this->db->get->result();
    

    【讨论】:

    • 我看到了逻辑。给我一天时间考虑一下。感谢您提供有用的意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多