简体   繁体   中英

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.

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>';

}

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>"
}

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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM