phpmysqlcodeigniter-2grocery-crud

How do I return an array all values with the same id?


This is my code:

function getPhoto($primary_key , $row){

   $this->db->select('id_report, path_photo');
   //$this->db->join('report', 'report.id = id_report');
   //$this->db->where('id_report', 'id_report');
   $this->db->from('report r, photo p');
   $where = "p.id_report = r.id AND id_report=".$row->id."";
   $this->db->where($where);
   $query = $this->db->get()->result_array();


  foreach ($query as $row2) {

     return " ".$row2['id_report']." - ".$row2['path_photo']."<br>";
     //echo " ".$row2['id_report']." - ".$row2['path_photo']."<br>";


}

function admin(){

      $crud->callback_column('POTHOS', array($this,'getPhoto')); 

.......

I need get from my db the path of my photos but a report can have multiple photos, so I use returns and echo:

(For this case I have the id and url for more explicit)

echo:

1 - a.jpeg
1 - b.jpeg
2 - ....
3 - ....
4 - ....
5 - ....
6 - ....
7 - ....
8 - pic1.jpeg
8 - pic2.jpeg
9 - .....
10 - ....

return:

1 - b.jpeg (Where is the other photo? a.jpeg)
2 - ......
3 - .....
4 - .....
5 - .....
6 - .....
7 - .....
8 - pic2.jpeg (pic1.jpeg?)
9 - .....
10 - .....

Solution

  • You can't have the "return" statement inside the loop... or the loop will only run once.

    Instead, create a concatenated string (using .=), or an array of values inside the loop, then return it AFTER the loop:

    $myString = '';
    
    foreach ($query as $row2) {
    
        $myString .= " ".$row2['id_report']." - ".$row2['path_photo']."<br>";
    
    }
    
    return $myString;