用日历日期链接的MySQL数据填充HTML表格 [英] Populating an HTML table with MySQL data, linked by calendar date

查看:93
本文介绍了用日历日期链接的MySQL数据填充HTML表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码生成一个简单的HTML表格,显示接下来的90个日历日。每天在这张简单的表格中都有一排。

  $ now = time(); 
回显< table>; ($ i = 0; $ i <90; $ i ++)
{
$ thisDate = date(d / m / Y,$ now +($ i * 86400) );
echo< tr>< td>。$ thisDate。< / td>< / tr> \ n;
}
echo< / table>;

另外,我有一个包含以下字段的MySQL表:

  event varchar(1000)
datescheduled date

如何在前面提到的HTML表格中创建第二列,其中包含MySQL表中匹配日期的event?

解决方案

这可以通过多种方式解决。考虑这个例子:

PHP

 <?php 
$ con = mysqli_connect(localhost,dbuser,dbpass,database);
$ query = mysqli_query($ con,SELECT * FROM event);

//首先构建事件数组,将日期放在键中,然后将值作为事件
$ events = array();
while($ result = mysqli_fetch_assoc($ query)){
$ events [$ result ['datescheduled']] = $ result ['event'];
}

?>






  //结构应该是这样的:
Array

[2014-05-02] => Lorem
[2014-06-02] =>日期
[2014-07-02] =>天数






HTML

 <! - 比较选定值在当前循环中,检查它的键 - > 

<?php $ now = time(); ?>
< table border =1cellpadding =10>
<?php for($ i = 0; $ i <90; $ i ++):?>
<?php $ thisDate = date(Y-m-d,$ now +($ i * 86400)); ?>
< tr>
< td><?php echo $ thisDate; ?>< / TD>
< td><?php echo array_key_exists($ thisDate,$ events)? $ events [$ thisDate]:''; ?>< / TD>
< / tr>
<?php endfor; ?>
< / table>


I am using the code below to generate a simple HTML table that displays the next 90 calendar days. Each day is a row in this simple table.

$now = time();
echo "<table>";
for ($i=0;$i<90;$i++)
{
   $thisDate = date("d/m/Y",$now + ($i*86400));
   echo "<tr><td>".$thisDate."</td></tr>\n";
}
echo "</table>";

Also, I have a MySQL table with the following fields:

event           varchar(1000)   
datescheduled   date    

How can I make a second column in the aforementioned HTML table, containing "event" from the MySQL table, matched by date?

解决方案

This can be tackled in numerous ways. Consider this example:

PHP

<?php
$con = mysqli_connect("localhost","dbuser","dbpass","database");
$query = mysqli_query($con, "SELECT * FROM event");

// First build the array of events, put the dates in keys, then the values as events
$events = array();
while($result = mysqli_fetch_assoc($query)) {
    $events[$result['datescheduled']] = $result['event'];
}

?>


// Structure should be something like this:
Array
(
    [2014-05-02] => Day of lorem
    [2014-06-02] => Day of ipsum
    [2014-07-02] => Day of days
)


HTML

<!-- the compare selected values on the current loop, check its keys -->

<?php $now = time(); ?>
<table border="1" cellpadding="10">
<?php for($i=0;$i<90;$i++): ?>
    <?php $thisDate = date("Y-m-d", $now + ($i*86400)); ?>
    <tr>
        <td><?php echo $thisDate; ?></td>
        <td><?php echo array_key_exists($thisDate, $events) ? $events[$thisDate] : ''; ?></td>
</tr>
<?php endfor; ?>
</table>

这篇关于用日历日期链接的MySQL数据填充HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆