how to make an array using the foreign key php

advertisements

I have a question about PHP query. I have 2 tables.
First Table:

|id_table1 | first_name | last_name |
|----------|------------|-----------|
|    1     |    John    |    Doe    |
|    2     |    Doe     |    John   |

Second Table:

|id_table2 | hobby  | age | id_table1|
|----------|--------|-----|----------|
|    1     |football| 17  |    1     |
|    2     |swimming| 18  |    2     |

I want to make table like this:

|    John Doe    |       |   Doe John    |
|----------------|       |--------|------|
|   Hobby  | Age |       |  Hobby |  Age |
|----------|-----|       |--------|------|
| football | 17  |       |swimming|  19  |
|basketball| 18  |

What is the syntax to make that table in php? May be using foreach, but how? Thanks.

The code

<?php
$ftable="select * from ftable";
$stable="select * from stable";
$ftable1= mysqli_query($conn, $ftable);
$stable1= mysqli_query($conn, $stable);
foreach ($ftable as $row) {
 echo "
  <tr>
   <th style='text-align: center' colspan='2'>".$row['first_name']."</th>
  </tr>
  <tr>
   <th>Hobby</th>
   <th>Age</th>
   </tr>";

 foreach ($ftable1 as $row1) {
  echo "
   <tr>
    <td>".$row1['hobby']."</td>
    <td>".$row1['age']."</td>
    <tr>";
 }
}
?>


Just save the id number of ftable, then test for it in your 'for' loop for the second table, like this:

    <?php
    $ftable="select * from ftable";
    $stable="select * from stable";
    $ftable1= mysqli_query($conn, $ftable);
    $stable1= mysqli_query($conn, $stable);
    foreach ($ftable1 as $row) {
      $id_table1 = $row["id_table1"];
      echo "<tr><th style='text-align: center' colspan='2'>"
         .$row['first_name']."</th></tr>"
             ."<tr><th>Hobby</th>"
             ."<th>Age</th></tr>";

       foreach ($stable1 as $row1) {
           if ($row1["id_table1"] == $id_table1) {
              echo "<tr><td>$row1['hobby']</td><td>$row1['age']</td></tr>";
           }
       }
    }