Cédric Vandelaer
Cédric Vandelaer

Reputation: 153

create html-table with php content

I have created a mysql database containing information about movies and at which hour they play in the movie theater. Now, I'm pretty new to this and was wondering what is the best way to make a html table with the content from the database, something like:

<table>
    <thead>
        <tr><th colspan="5">Programma</th></tr>
    </thead>
    <tbody>
        <tr>
            <td style="opacity:1">MovieName</td>
            <td>14u00</td>
            <td>17u00</td>
            <td>20u00</td>
            <td>22u30</td>
        </tr>
        <tr>
            <td style="opacity:1">MovieName</td>
            <td>14u00</td>
            <td>17u00</td>
            <td>20u00</td>
            <td>22u30</td>
        </tr>
    </tbody>
</table>

I want the cells containing the hours to only be visible if that hour is in the database. I know how to make a connection with the database but I'm not sure where to implement the php code and how to make the cells only visible when they are mentioned in the database for that movie. Thanks in advance.

Upvotes: 1

Views: 252

Answers (3)

Amar Gharat
Amar Gharat

Reputation: 154

It depends on how you save data in your tables. I assume you have two tables called "movies" and "showtimes"

movies
=============
id | movie
-------------
1  | Movie 1
2  | Movie 2
3  | Movie 3
___________________________________

showtimes
===================================
id | movie_id | showtime
-----------------------------------
1  | 1        | 9.00 am - 12 pm 
2  | 2        | 12.00 pm - 3 pm
3  | 1        | 3.00 pm - 6 pm
4  | 2        | 6.00 am - 9.00 pm
5  | 1        | 9.00 am - 12.00 am
___________________________________
  $sql = 'SELECT m.movie, group_concat(s.showtime) FROM movies m left join showtimes s on m.id=s.movie_id group by m.movie';
  $rs = mysql_query($rs);
  if($rs){

echo '<table>
       <thead>
          <tr><th colspan="5">Programma</th></tr>
       </thead>
       <tbody>';

 while($row = mysql_fetch_object($rs)) {
      echo '<tr>
            <td style="opacity:1">'.$row->movie.'</td>';

      $showtimes = explode(',', $row->showtime);
      if($showtimes) {
         foreach($showtimes as $showtime) {    
             echo '<td>'.$showtime.'</td>';
         }
      }
      echo '</tr>';    
 }

 echo '</tbody>
       </table>';

}

Upvotes: 2

Orangepill
Orangepill

Reputation: 24645

What you want is to use a nested foreach, will work for any two dimensional array

echo "<table>";
foreach($rows as $row){
    echo "<tr>";
    foreach ($row as $fieldName=>$field){
        echo "<td>",$field, "</td>";
    }
    echo "</tr>";
}
echo "</table>";

Upvotes: 1

Sameer Anand
Sameer Anand

Reputation: 274

try using the foreach loop and inside it creating a new table row for each record and a table column for each attribute in the record

foreach($resultset as $row)
{
    echo "<tr>"
    echo "<td>" . $row['value'] . "</td>"
    echo "</tr>"
}

Upvotes: 1

Related Questions