curious_coder
curious_coder

Reputation: 2468

Return value in foreach loop in CodeIgniter

I am having some trouble in returning values from model to controller Using CodeIgniter. I am passing two arrays from controller to model. I am checking whether both the arrays are not empty and looping them using foreach loop to fetch some values from database and returning the query to controller. Here's my current code

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()); works fine and i am able to view the results. Where should i put return $nearbytution; so that i can get all the fetched values? I tried it in Line 1 but i was getting only values of last array value.

Upvotes: 0

Views: 3393

Answers (3)

Robin Castlin
Robin Castlin

Reputation: 10996

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();
}

Seriously, do consider this approach. You only need to bother the poor Mysql server with one call and you get all the data you want at the same time.

Apart from that, practice consistent style in your code. It will save both your time and your hair in the long run.

Upvotes: 2

Deratrius
Deratrius

Reputation: 689

Unless I misunderstand your question why don't you just store all the results in a big array and then return that array?

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;
}

Now $data will be an array of results, each containing the query results. If your result set has to be cleaned up (to remove duplicates for instance) you can just loop through it and do that cleanup.

What you could also maybe do is build a large SQL query in your foreach loops and then just do that one big query and return the results.

Upvotes: 0

Mudshark
Mudshark

Reputation: 3253

Try this in the loop:

$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;may then be placed at your "Line 1". It will contain an array of query results.

Upvotes: 0

Related Questions