【问题标题】:How do I return an array all values with the same id?如何返回具有相同 id 的所有值的数组?
【发布时间】:2014-08-06 18:38:29
【问题描述】:

这是我的代码:

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')); 

.......

我需要从我的数据库中获取照片的路径,但一个报告可以有多张照片,所以我使用返回和回显:

(对于这种情况,我有更明确的 id 和 url)

回声:

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

返回:

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

【问题讨论】:

  • 你的例子没有任何意义。 1407180296732 在“之前”中没有任何文字,但突然在之后有 pic1/pic2 的内容,而这些照片所附加的 1407181835925 现在是空的。

标签: php mysql codeigniter-2 grocery-crud


【解决方案1】:

循环内不能有“return”语句...否则循环只会运行一次。

相反,创建一个连接字符串(使用 .=)或循环内的值数组,然后在循环后返回它:

$myString = '';

foreach ($query as $row2) {

    $myString .= " ".$row2['id_report']." - ".$row2['path_photo']."<br>";

}

return $myString;

【讨论】:

  • 是的,先生。这是我的解决方案,但感谢您发布。
猜你喜欢
  • 2021-07-10
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多