title: Dynamic table with php pdo and two foreach
body: <p>So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward. </p>
  
  <p><img src="https://i.stack.imgur.com/Qttcv.jpg" alt="screenshot of the error"></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link   href="css/bootstrap.min.css" rel="stylesheet"&gt;
      &lt;link   href="css/db.css" rel="stylesheet"&gt;
      &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div class="container"&gt;
              &lt;div class="row text-center"&gt;
                  &lt;h3&gt;Schedule of Ms. Kim &amp; Ms. Morly&lt;/h3&gt;
              &lt;/div&gt;
  
  &lt;?php
    include 'database.php';
    $pdo = Database::connect();
  
      //Start of table Kim - ID=10
      $sql = 'SELECT * FROM kim ORDER BY id';
      $q = $pdo-&gt;prepare($sql);
          $q-&gt;execute(array($schd_tut_id));
          $data = $q-&gt;fetch(PDO::FETCH_ASSOC);
  
      $sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
          $q2 = $pdo-&gt;prepare($sql2);
          $q2-&gt;execute(array($tut_profile_id));
          $data2 = $q2-&gt;fetch(PDO::FETCH_ASSOC);
          $imgkim = $data2['tut_img'];
      //End of Table Kim
  
      //Start of table Morly - ID=16
      $sql3 = 'SELECT * FROM morly ORDER BY id';
      $q3 = $pdo-&gt;prepare($sql3);
      $q3-&gt;execute(array($schd_tut_id));
      $data3 = $q3-&gt;fetch(PDO::FETCH_ASSOC);
  
      $sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
      $q4 = $pdo-&gt;prepare($sql4);
      $q4-&gt;execute(array($tut_profile_id));
      $data4 = $q4-&gt;fetch(PDO::FETCH_ASSOC);
      $imgmorly = $data4['tut_img'];
      //End of table Morly
  ?&gt;    
  
  
  &lt;table class="table table-striped table-bordered"&gt;
   &lt;thead&gt;
  &lt;th style="width:20px;"&gt;Time&lt;/th&gt;
  &lt;th&gt;&lt;img class="img-responsive" src="&lt;?php  echo $imgkim  ?&gt;"&lt;/th&gt;
  &lt;th&gt;&lt;img class="img-responsive" src="&lt;?php  echo $imgmorly  ?&gt;"&lt;/th&gt;
  &lt;th&gt;&lt;img class="img-responsive" src="&lt;?php  echo $imgmorly  ?&gt;"&lt;/th&gt;
  
  
   &lt;/thead&gt;
  
     &lt;tbody&gt;
  
  &lt;?php
      //tutor kim
      foreach ($pdo-&gt;query($sql) as $row ) {
          echo '&lt;tr&gt;';
          //echo '&lt;td hidden&gt;'. $row['id'] . '&lt;/td&gt;';
          //echo '&lt;td hidden&gt;'. $row['schd_tut_id'] . '&lt;/td&gt;';
  
          echo '&lt;td&gt;'. $row['time'] . '&lt;/td&gt;';
          if($row['status']=='available')
            { echo '&lt;td&gt;'. '&lt;a href="read.php?id='.$row['id'] . '&amp;s_t_id='.$row['schd_tut_id']   .'"&gt;available&lt;/a&gt;';} 
          else {echo '&lt;td&gt;'. $row['status'] . '&lt;/td&gt;';}   
      }
      //echo '&lt;/tr&gt;';
      //tutor morly
      foreach ($pdo-&gt;query($sql3) as $row ) {
          //echo '&lt;tr&gt;';
  
          //echo '&lt;td hidden&gt;'. $row['id'] . '&lt;/td&gt;';
          //echo '&lt;td hidden&gt;'. $row['schd_tut_id'] . '&lt;/td&gt;';
          echo '&lt;td&gt;'. $row['time'] . '&lt;/td&gt;';
          if($row['status']=='available')
            { echo '&lt;td&gt;'. '&lt;a href="read.php?id='.$row['id'] . '&amp;s_t_id='.$row['schd_tut_id']   .'"&gt;available&lt;/a&gt;';} 
          else {echo '&lt;td&gt;'. $row['status'] . '&lt;/td&gt;';}   
          echo '&lt;/tr&gt;';
      }
  Database::disconnect();
  ?&gt;
         &lt;/tbody&gt;
      &lt;/table&gt;
  
          &lt;/div&gt;
      &lt;/div&gt; &lt;!-- /container --&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>