CodeIgniter中foreach循环中的返回值

时间:2013-04-16 13:15:29

标签: php codeigniter

我在使用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中尝试过,但我只得到最后一个数组值的值。

3 个答案:

答案 0 :(得分: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服务器,就可以同时获得所需的所有数据。

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

答案 1 :(得分: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行”。它将包含一系列查询结果。

答案 2 :(得分: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查询,然后只执行一个大查询并返回结果。