使用Array& Foreach在一起

时间:2017-04-27 23:04:54

标签: php

我正在寻找关于while循环和foreach循环的澄清......

说我有2个表,两个表都有一个名为MYID的字段,两个表的MYID都相同..

如果多次在另一个字段中回复某些字段,您将如何完成回显。

例如

while ($row = mysqli_fetch_assoc($result)) {

        $MYID = $row['MYID'];        
        $FullName = $row['FullName'];    
        $Photo = $row['Photo'];        


    // Display this info once     
    echo "<div>$FullName<br>Photo</div>";   


   // Array for table 2 row    
   $Images[] = array($row['Images']);

   foreach ($Images as $img){       
   //Display this row as many times as needed by data in this row.

       echo <div>$img</div>;
   }
}  

1 个答案:

答案 0 :(得分:1)

如果您正在使用GROUP_CONCAT,则$row['Images']将是以逗号分隔的列表,您可以使用explode()进行拆分。

while ($row = mysqli_fetch_assoc($result)) {

    $MYID = $row['MYID'];        
    $FullName = $row['FullName'];    
    $Photo = $row['Photo'];        
    // Display this info once     
    echo "<div>$FullName<br>Photo</div>";   

    // Array for table 2 row    
    $Images = explode(',', $row['Images']);
    foreach ($Images as $img){       
    //Display this row as many times as needed by data in this row.
       echo <div>$img</div>;
    }
}