douyi7283 2013-05-16 14:03
浏览 92
已采纳

用php内容创建html表

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.

  • 写回答

3条回答 默认 最新

  • dqby43944 2013-05-16 14:26
    关注

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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值