0

我在使用 CodeIgniter 将值从模型返回到控制器时遇到了一些麻烦。我将两个数组从控制器传递到模型。我正在检查两个数组是否不为空,并使用 foreach 循环循环它们以从数据库中获取一些值并将查询返回给控制器。这是我当前的代码

if (!empty($search_for_area) && !empty($search_for_requirement))
      { foreach($search_for_requirement as $search_value_1)
          { 
            foreach($search_for_area as $search_value_2)
              { 
                if($search_value_2 != null && $search_value_1 != null)
                  {
                     $nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
                     print_r($nearbytution->result()); 
                  }
              }
          }

         //Line 1
      }

print_r($nearbytution->result());工作正常,我可以查看结果。我应该放在哪里return $nearbytution;以便我可以获得所有获取的值?我试过了,Line 1但我只得到最后一个数组值的值。

4

3 回答 3

2
function returnStuff($search_for_area,$search_for_requirement) {
    $arr_area = array();
    $arr_filter = array();
    if ( ! empty($search_for_area) and ! empty($search_for_requirement)) {
        foreach($search_for_requirement as $search_value_1) {
            foreach($search_for_area as $search_value_2) { 
                if($search_value_2 != null && $search_value_1 != null) {
                    $arr_area[] = $this->db->escape($search_value_2);
                    $arr_filter[] = $this->db->escape_like_str($search_value_1);
                }
            }
        }
    }

    $str_area = 'NULL';
    if ($arr_area)
        $str_area = implode(', ', $arr_area);

    $str_filter = "'^-$'";
    if ($arr_filter)
        $str_filter = "'(".implode('|', $arr_filter).")'";

    $query = $this->db->query("
        SELECT name, area, contactno 
        FROM tut_listing 
        WHERE area IN ({$str_area}) AND categoryfilter REGEXP {$str_filter} and partner > '' group by name
    ");

    return $query->result();
}

说真的,请考虑这种方法。你只需要一个调用来打扰可怜的Mysql服务器,你就可以同时获得所有你想要的数据。

除此之外,在你的代码中练习一致的风格。从长远来看,这将节省您的时间和头发。

于 2013-04-16T14:59:04.057 回答
0

在循环中试试这个:

$nearbytution[] = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name')->result();

return $nearbytution;然后可以放置在您的“第 1 行”。它将包含一个查询结果数组。

于 2013-04-16T13:30:33.733 回答
0

除非我误解了您的问题,否则您为什么不将所有结果存储在一个大数组中然后返回该数组?

function returnStuff($search_for_area,$search_for_requirement) {
    $data = array();
    if (!empty($search_for_area) && !empty($search_for_requirement)) {             
        foreach($search_for_requirement as $search_value_1) { 
        foreach($search_for_area as $search_value_2) { 
            if($search_value_2 != null && $search_value_1 != null) {
                 $nearbytution = $this->db->query('select name,area,contactno from tut_listing where area = "'.$search_value_2.'" and categoryfilter like "%'.$search_value_1.'%" and partner > "" group by name');
                 $data[] =$nearbytution->result()); 
              }
          }
      }
    }
    return $data;
}

现在 $data 将是一个结果数组,每个都包含查询结果。如果必须清理您的结果集(例如删除重复项),您可以遍历它并进行清理。

您还可以做的是在您的 foreach 循环中构建一个大型 SQL 查询,然后只执行一个大型查询并返回结果。

于 2013-04-16T13:35:53.990 回答