doufuhao0566 2014-08-06 18:38 采纳率: 100%
浏览 50
已采纳

如何返回具有相同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 - .....
  • 写回答

1条回答 默认 最新

  • dqdtgiw4736 2014-08-16 15:07
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂