简体   繁体   English

从MySQL数据库读取数据到HTML表?

[英]Reading data from a MySQL database into a HTML Table?

So i am working on a project and need some advice. 所以我正在做一个项目,需要一些建议。

I have a MySQL database that stores events, i know how to code this functionality in PHP but im just stuck of a few specifics. 我有一个存储事件的MySQL数据库,我知道如何用PHP编写此功能,但我只是停留在一些细节上。

As the project that I am creating is a timetable, the most important attributes are the day of the event, starting time and finishing time. 由于我正在创建的项目是一个时间表,因此最重要的属性是活动的日期,开始时间和结束时间。

Once i have read this data from the MYSQL database using my PHP script, how do i go about inserting these events in to a html timetable? 一旦使用PHP脚本从MYSQL数据库中读取了这些数据,如何将这些事件插入html时间表?

Lets say i have record like below in my events table: 可以说我在事件表中记录如下:

Event ID = 01 事件ID = 01
Event Day = Monday 活动日=星期一
Event Start = 12:00 活动开始= 12:00
Event End = 14:00 活动结束= 14:00

How would I then put that into a html table, bearing in mind that i may have mutiple events for a day? 考虑到我可能一天都会发生多起事件,我该如何将其放入html表中?

You can mix your result from PHP into HTML code: 您可以将PHP中的结果混合到HTML代码中:

<table>
<?php
foreach ($results as result){
  echo '<tr><td>'.$result->field.'</td></tr>';
}
?>
</table>

Are you having trouble determining how the PHP mixes with the HTML in this situation? 在这种情况下,您是否难以确定PHP如何与HTML混合使用? If so: 如果是这样的话:

<table>
<thead>
    <tr>
        <th>Event ID</th>
        <th>Event Day</th>
        <!-- etc... -->
    </tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_assoc($resultSet)) { ?>
    <tr>
        <td><!-- Event ID row data --></td>
        <td><!-- Event Day row data --></td>
        <!-- etc... -->
    </tr>    
<? } ?>
</tbody>
</table>

mysql_fetch_array($result) iterates the rows from your result. mysql_fetch_array($ result)迭代结果中的行。 Just do something like this: 只是做这样的事情:

while ($row = mysql_fetch_array($result)) {
    echo $row['fieldname'];
}

The above code would display every item in the result's column named "fieldname". 上面的代码将显示结果列中名为“ fieldname”的每个项目。 Use HTML to format the results however you like. 使用HTML随意设置结果格式。

<table>
<?php while ($row = mysql_fetch_assoc($result)) {?>
   <tr>
      <td>
           Event ID = <?php $row['id'] ?> <br />
           Event Day = <?php $row['day]?> <br/>
           Event Start = <?php $row['start_date']?> <br/>
           Event End = <?php $row['end_date']?> 
      </td>
   </tr>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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