简体   繁体   English

PHP HTML 动态创建时间表的时间表

[英]PHP HTML dynamically create table for time class schedule

在此处输入图片说明 im trying to create a class scheduling table, i want to plot the schedules from may result set to create a tabular representation.我正在尝试创建一个课程安排表,我想绘制可能结果集中的时间表以创建表格表示。

i have a fixed header or days MONDAY-SATURDAY on top and 7:00 AM-9:00 pm on the left side of the table.我有一个固定的标题或星期一至星期六的顶部和表格左侧的上午 7:00 至晚上 9:00。

$scheduledet = json_decode(json_encode($data['scheduledet']), true);
print_r($scheduledet);
$week_days = array('M','T','W','TH','F','S');
$range=range(strtotime("07:00"),strtotime("22:00"),30*60);
$rowspan = 0;
$rangetime = [];
foreach($range as $time){
    $rangetime[] = date("H:i",$time);
}
foreach($week_days as $key => $wday){
    $schedfortheday = array_filter($scheduledet, function ($item) use ($wday) {
        if ($item['day'] == $wday) {
            return true;
        }
        return false;
    });

    foreach ($schedfortheday as $key => $value) {
        $rowspan = 0;
        $count = 1;
        foreach ($rangetime as $k => $v) {

            if(strtotime($v) >= strtotime($value['fromtime']) && strtotime($v) <= strtotime($value['totime'])){
                $rowspan ++;
            }
        }
    }
}



<table border="2" style= "width:100%; margin: 0 auto;" cellpadding="0" cellspacing="0">
    <tr>
        <th scope="col">&nbsp;</th>
        <th scope="col">Monday</th>
        <th scope="col">Tuesday</th>
        <th scope="col">Wednesday</th>
        <th scope="col">Thursday</th>
        <th scope="col">Friday</th>
        <th scope="col">Saturday</th>
    </tr>
        <tr>
        <td>7:00 AM</td>
    </tr>
    <tr>
        <td>7:30 AM</td>
    </tr>
    <tr>
        <td>8:00 AM</td>
    </tr>
    <tr>
        <td>8:30 AM</td>
    </tr>
    <tr>
        <td>9:00 AM</td>
    </tr>
    <tr>
        <td>9:30 AM</td>
    </tr>
    <tr>
        <td>10:00 AM</td>
    </tr>
    <tr>
        <td>10:30 AM</td>
    </tr>
    <tr>
        <td>11:00 AM</td>
    </tr>
    <tr>
        <td>11:30 AM</td>
    </tr>
    <tr>
        <td>12:00 PM</td>
    </tr>
    <tr>
        <td>12:30 PM</td>
    </tr>
    <tr>
        <td>1:00 PM</td>
    </tr>
    <tr>
        <td>1:30 PM</td>
    </tr>
    <tr>
        <td>2:00 PM</td>
    </tr>
    <tr>
        <td>2:30 PM</td>
    </tr>
    <tr>
        <td>3:00 PM</td>
    </tr>
    <tr>
        <td>3:30 PM</td>
    </tr>
    <tr>
        <td>4:00 PM</td>
    </tr>
    <tr>
        <td>4:30 PM</td>
    </tr>
    <tr>
        <td>5:00 PM</td>
    </tr>
    <tr>
        <td>5:30 PM</td>
    </tr>
    <tr>
        <td>6:00 PM</td>
    </tr>
    <tr>
        <td>6:30 PM</td>
    </tr>
    <tr>
        <td>7:00 PM</td>
    </tr>
    <tr>
        <td>7:30 PM</td>
    </tr>
    <tr>
        <td>8:00 PM</td>
    </tr>
    <tr>
        <td>8:30 PM</td>
    </tr>
    <tr>
        <td>9:00 PM</td>
    </tr>

</table>
Array(Array
    (
        'id' => '4545',
        'classid' => '1842',
        'fromtime' => '09:30:00',
        'totime' => '11:30:00',
        'day' => 'M',
        'room' => 'B01',
        'code' => 'BCS 1',
        'subdesc' => 'IT APPLICATION TOOLS IN BUSINESS',
        'units' => '3'
    ), Array(
        'id' => '4460',
        'classid' => '1596',
        'fromtime' => '10:30:00',
        'totime' => '11:30:00',
        'day' => 'M',
        'room' => '122A',
        'code' => 'PS 101',
        'subdesc' => 'FUNDAMENTALS OF POLITICAL SCIENCE',
        'units' => '3'
    ),Array
    (
        'id' => '4463',
        'classid' => '1598',
        'fromtime' => '13:00:00',
        'totime' => '14:00:00',
        'day' => 'M',
        'room' => 'TBA',
        'code' => 'PSOE 1',
        'subdesc' => 'PHILIPPINE POLITICAL THOUGHT',
        'units' => '3'
    ),Array(
        'id' => '3881',
        'classid' => '1597',
        'fromtime' => '14:00:00',
        'totime' => '15:00:00',
        'day' => 'M',
        'room' => 'TBA',
        'code' => 'PS 102',
        'subdesc' => 'INTRODUCTION TO PHILIPPINE POLITICS AND GOVERNANCE',
        'units' => '3'
    ),Array(
        'id' => '4540',
        'classid' => '1609',
        'fromtime' => '16:00:00',
        'totime' => '17:00:00',
        'day' => 'M',
        'room' => '205',
        'code' => 'CFE 3',
        'subdesc' => 'CATHOLIC FOUNDATION OF MISSION',
        'units' => '3'
    ))

this is my schedule array from my result set, if there are conflict schedule it will ovelap each rowspan and display the information of conflict schedule.这是我的结果集中的计划数组,如果有冲突计划,它将重叠每个行跨度并显示冲突计划的信息。 i cannot plot the result set in the table.我无法绘制表中的结果集。 any help will be appreciated thanks in advance任何帮助将不胜感激提前感谢

it allows schedule overlap and it will still be displayed in the table as merged table cells.它允许时间表重叠,并且它仍将作为合并的表格单元格显示在表格中。 the image with red cell represents 2 subjects are in conflict and with same schedule带有红色单元格的图像表示 2 个主题存在冲突且日程相同

在此处输入图片说明

I guess your problem is how you can plot the given data in that week table.我想您的问题是如何在该周表中绘制给定的数据。 First you have to create another set of array data in a way that will make it easier for you to loop over it.首先,您必须以一种更容易循环遍历的方式创建另一组数组数据。

Since you have a fixed days and hours you can read it in two ways:由于您有固定的日期和时间,您可以通过两种方式阅读:

  1. array of days with set times in it设置时间的天数数组
  2. array of times with set days in it设置日期的时间数组

Below is an example of final data structure that you'll use to loop and generate your table data.以下是您将用于循环和生成表数据的最终数据结构示例。

Array data with time as key:以时间为键的数组数据:

$formatted_data1 = array( 
"sunday" => 
   array( "7:00:00" => <data here>, 
         "7:30:00" => <data here>
         ..."21:30:00"=> <data> )
"monday" => 
   array( and so on..)
)

Array data with time as key:以时间为键的数组数据:

$formatted_data1 = array( 
    "7:00:00" => 
       array( "Monday" => <data here>, 
             "Tuesday" => <data here>
             ..."Sunday"=> <data here> )
    "7:30:00" => 
       array( "Monday" => <data here>,
        and so on..)
    )

I suggest to go for first option since it is easier to plot horizontally which will be derived from your sample data and proceed on iterating each day to plot in your table.我建议选择第一个选项,因为它更容易从您的样本数据中得出水平绘图,并继续每天迭代以绘制在您的表格中。

  1. Sort by fromtime按时间排序
  2. Rearrange array by day of week like按星期几重新排列数组,如
$arr["M"] = [{
 fromtime: 1400,
 totime: 1530,
 subject: "foo"
}, {
  fromtime: 1430,
  totime: 1630,
  subject: "bar"
}]
  1. Iterate and compare two time range foo(f1, t1) bar(f2, t2) If f2 >= f1 and f2 < t1 then merge them as (f1, max(t1,t2))迭代并比较两个时间范围 foo(f1, t1) bar(f2​​, t2) 如果 f2 >= f1 和 f2 < t1 然后将它们合并为 (f1, max(t1,t2))
$merged["M"] = [{
 fromtime: 1400,
 totime: 1630,
 subject: "foo bar"
}]

For merge code, see Algorithm to combine / merge date ranges对于合并代码,请参阅组合/合并日期范围的算法

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

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