0

I am looking for clarification on while loop and foreach loop...

say I have 2 tables both table have a field called MYID, MYID will be the same for both tables..

How would you accomplish echo certain fields once while another field multiple times.

example

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>;
   }
}  
Case
  • 281
  • 4
  • 26
  • Can you add examples of input data and result you want/expect to get? – E_p Apr 27 '17 at 23:06
  • `$Images` contains all the images from all the rows you've processed so far, not just the current row. – Barmar Apr 27 '17 at 23:07
  • If `$row['Images']` contains a comma-separated list of images, you should use `explode()` to split it into an array, and then loop over that. – Barmar Apr 27 '17 at 23:08
  • example here I'm trying to create how each profile user there images is under there profile http://bodyelectrictattoo.com/our-team/ – Case Apr 27 '17 at 23:09
  • I want to create a profile page that displays just like that page with data from the database but the info will echo out the first profile 10 times for each image added.. and not other profile shows. – Case Apr 27 '17 at 23:11
  • Are you joining a table with user information with a 1-to-many table of images? Then each image will be in a different row, unless you're using `GROUP_CONCAT` to combine them into one row. – Barmar Apr 27 '17 at 23:11
  • You need to show sample input data and the query. – Barmar Apr 27 '17 at 23:11
  • This answer should be helpful: http://stackoverflow.com/questions/27575562/how-can-i-list-has-same-id-data-with-while-loop-in-php/27575685#27575685 – Barmar Apr 27 '17 at 23:12
  • @Barmar I don't use PDO, also I did do group_concat it wouldn't show images just the name of the image – Case Apr 27 '17 at 23:13
  • What does PDO have to do with it? The general approach is the same with mysqli. – Barmar Apr 27 '17 at 23:16

1 Answers1

1

If you're using GROUP_CONCAT, $row['Images'] will be a comma-separated list, which you can split with 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>;
    }
}  
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • that work as needed but I only shows 1 profile now there are 2 in the database – Case Apr 27 '17 at 23:37
  • Sounds like a problem with your query. Did you use `GROUP BY` properly? I asked above for you to add the query to the question. – Barmar Apr 28 '17 at 14:39