将设定日期的mysql记录合并到时间表中?

时间:2013-11-21 06:34:12

标签: php html mysql timetable

我正在努力为5人制作一个小型预约时间表供日常使用。我现在唯一的问题是让时间表显示同一个员工的所有预约到一列。我尝试了几种不同的方法但到目前为止没有运气。我将发布最好的两次尝试。

下面是我为第一次尝试编写的代码获得的示例输出。

http://jsfiddle.net/yy885/CXcjR/2/

它单独显示约会,因为此输出的查询只是指定日期该员工的正常查询。我的问题是我找不到合并这3条记录的方法,并在此时间表中将其输出到一列。至于现在,如果我有5个约会,那么这将显示25个记录(25列),这将无法正常工作。基于我到目前为止的代码,我可以想到一个快速的解决方法,但我不想使用它。这将一次只显示一名员工。但是这种方式对于要约会的女孩来说很难,因为她可能会忽略时间表并向客户提供错误的日期/时间。

<?php

include("connect.php");//connect to the database.
//$todaydate = date('m/d/Y');
$todaydate = "11/22/2013";
$staffd = "Jerry";
$query = mysql_query('SELECT * FROM appointment2 where date="'.$todaydate.'" AND      staff="'.$staffd.'"');
$num_row = mysql_num_rows ($query);

if ($num_row > 0 ) {
$i=0;
while($i < $num_row){  

$date = mysql_result($query,$i,"date");
$clientid = mysql_result($query,$i,"clientid");
$staff = mysql_result($query,$i,"staff");
$status = mysql_result($query,$i,"status");
$paid = mysql_result($query,$i,"paid");
$comment = mysql_result($query,$i,"comment");

$t0900 = mysql_result($query,$i,"0900");
$t0915 = mysql_result($query,$i,"0915");
$t0930 = mysql_result($query,$i,"0930");
$t0945 = mysql_result($query,$i,"0945");
$t1000 = mysql_result($query,$i,"1000");
$t1015 = mysql_result($query,$i,"1015");
$t1030 = mysql_result($query,$i,"1030");
$t1045 = mysql_result($query,$i,"1045");
$t1100 = mysql_result($query,$i,"1100");
$t1115 = mysql_result($query,$i,"1115");
$t1130 = mysql_result($query,$i,"1130");
$t1145 = mysql_result($query,$i,"1145");
$t1200 = mysql_result($query,$i,"1200");
$t1215 = mysql_result($query,$i,"1215");
$t1230 = mysql_result($query,$i,"1230");
$t1245 = mysql_result($query,$i,"1245");
$t1300 = mysql_result($query,$i,"1300");
$t1315 = mysql_result($query,$i,"1315");
$t1330 = mysql_result($query,$i,"1330");
$t1345 = mysql_result($query,$i,"1345");
$t1400 = mysql_result($query,$i,"1400");
$t1415 = mysql_result($query,$i,"1415");
$t1430 = mysql_result($query,$i,"1430");
$t1445 = mysql_result($query,$i,"1445");
$t1500 = mysql_result($query,$i,"1500");
$t1515 = mysql_result($query,$i,"1515");
$t1530 = mysql_result($query,$i,"1530");
$t1545 = mysql_result($query,$i,"1545");
$t1600 = mysql_result($query,$i,"1600");
$t1615 = mysql_result($query,$i,"1615");
$t1630 = mysql_result($query,$i,"1630");
$t1645 = mysql_result($query,$i,"1645");
$t1700 = mysql_result($query,$i,"1700");
$t1715 = mysql_result($query,$i,"1715");
$t1730 = mysql_result($query,$i,"1730");
$t1745 = mysql_result($query,$i,"1745");
$t1800 = mysql_result($query,$i,"1800");
$t1815 = mysql_result($query,$i,"1815");
$t1830 = mysql_result($query,$i,"1830");
$t1845 = mysql_result($query,$i,"1845");
$t1900 = mysql_result($query,$i,"1900");
$t1915 = mysql_result($query,$i,"1915");
$t1930 = mysql_result($query,$i,"1930");
$t1945 = mysql_result($query,$i,"1945");
$t2000 = mysql_result($query,$i,"2000");
$t2015 = mysql_result($query,$i,"2015");
$t2030 = mysql_result($query,$i,"2030");
$t2045 = mysql_result($query,$i,"2045");


echo       '<table border="1">';
echo       '<tr><td style="word-wrap: break-word">'.$date.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">&nbsp;&nbsp;&nbsp;'.$staff.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t0900.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t0915.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t0930.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t0945.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1000.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1015.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1030.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1045.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1100.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1115.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1130.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1145.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1200.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1215.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1230.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1245.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1300.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1315.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1330.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1345.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1400.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1415.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1430.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1445.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1500.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1515.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1530.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1545.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1600.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1615.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1630.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1645.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1700.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1715.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1730.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1745.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1800.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1815.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1830.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1845.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1900.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1915.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1930.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t1945.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t2000.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t2015.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t2030.'</td></tr>';
echo       '<tr><td style="word-wrap: break-word">'.$t2045.'</td></tr>';
echo       '</table>';

++$i; } } else { echo "No record found"; } 
?>

第二次尝试。 我用日期,时间,人员和事件重写了mysql。我使用concat将时间和事件放入一个字符串中。所以我使用的查询只是调用它,日期和员工的名字。然后我将这些结果放入一个数组并提取它们。

结果如下。

0945 Event A part 1
1115 Event A part 2
1315 Event B part 1
1630 Event B part 2
1745 Event B part 3
1900 Event C part 1
1945 Event C part 2
2015 Event B part 4

但后来我不知道我能做些什么才能很好地将它们纳入时间表。我可以将它们放入一张桌子中,但之后它不会显示那些会让它难以阅读的空位。

以下是我尝试使用的代码。

<?php
include("connect.php");//connect to the database.
$todaydate = "11/22/2013";
$staffd = "Jerry";

$query = 'SELECT CONCAT(time," ",event) FROM appointment3 where date="'.$todaydate.'" AND staff="'.$staffd.'"';
$result = mysql_query($query) or die ("no query");

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $new_arr[] = $row['CONCAT(time," ",event)'];    
}
sort($new_arr);
foreach ($new_arr as $value) {
    echo $value."<br />\n";
}
?>

欢迎任何建议,因为我仍然认为我的方式不会很好......

1 个答案:

答案 0 :(得分:0)

首先:不要让500行回显<tr>...</tr>而只是循环遍历它们。

如果空窗口打扰你,只需生成它们!它不像它们包含任何东西,所以你可以用来填补缺失的时间。

如果您的时间表存储在关联数组中,可以使用类似

的方式完成
$lines = ...;

$hours = [];
foreach ($lines as $line)
{
    $hour = substr($line, 0, 4);
    $hours[$hour] = $line;
}

然后,您可以使用以下方法生成默认的空事件:

for ($i = 0; $i < 24; $i ++)
    for ($j = 0; $j < 60; $j += 5)
    {
        $hour = sprintf('%02d%02d', $i, $j);
        if (!isset($hours[$hour]))
            $hours[$hour] = "$hour (empty)";
    }

var_dump($hours);