虽然有问题的代码是PHP,但我需要第二眼,一般算法,伪代码或类似的解决方案。
我有一组具有属性的对象($events
):
event_name
date_from
date_to
我有一张工作日的桌子:
Mon Tue Wed Thu Fri Sat Sun
Starts * * * * * * *
Ends * * * * * * *
HTML中的相同表格:
<table>
<thead>
<tr>
<th></th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
<th>Sun</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Starts</strong></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td><strong>Ends</strong></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
</tbody>
</table>
Asterisk只是一个占位符,并不相关。
我想在{例如}下显示event_name
。周三如果活动开始或在周三结束。当然,如果它从星期三开始(date_from
是星期三),我希望它在星期三下的Starts
行,如果它在星期三结束(date_to
是星期三) ,我想在星期三下的Ends
行。
我有一个有效的解决方案,但是我的第二个想法甚至在这里写它是非常糟糕的。)
解决方案是采用该HTML并在每个<td>*</td>
而不是星号中放入这些PHP行(例如星期一的<td>
):
<td>
foreach($events as $event) {
if($event->date_to == $week_start) { //Monday is start of the week
echo $event->name;
}
}
</td>
对于星期二的<td>
,它将类似于:
<td>
foreach($events as $event) {
if($event->date_to == $week_start + 1) { //Tuesday is start of the week + 1 day
echo $event->name;
}
}
</td>
...依此类推,但是这些foreach
和if
中的14个每天两行可能是愚蠢的:)。当然,对于Ends
行,我将date_to
属性与相关日期进行比较。
有没有人有概念性的答案?如何以更好的方式解决这个问题?
P.S。我在Laravel,PHP MVC框架中工作,所以我在控制器中构建一个集合并将其传递给视图。日期是Carbon个实例,它是DateTime的PHP API扩展,因此可以使用各种方便的方法来确定日期(addDay()
,startOfWeek()
等)。可以更改有问题的HTML,如果需要,也可以更改$events
集合的结构。
提前感谢您的想法。
编辑:在表格单元格(event_name
)中,除了最后一个<td>
之外的每个boost
之后放置逗号的加分点。
答案 0 :(得分:0)
1)php模板语言唯一解决方案
<强>树枝强>
{% set days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] %}
{% set event_1 = {"name":"event_1","date_from" : "monday", "date_to" : "monday"} %}
{% set event_2 = {"name":"event_2","date_from" : "friday", "date_to" : "thursday"} %}
{% set event_3 = {"name":"event_3","date_from" : "wednesday", "date_to" : "tuesday"} %}
{% set event_4 = {"name":"event_4","date_from" : "saturday", "date_to" : "monday"} %}
{% set event_5 = {"name":"event_5","date_from" : "sunday", "date_to" : "thursday"} %}
{% set event_list = [event_1, event_2, event_3, event_4, event_5] %}
<div class="container">
<table class="table table-striped">
<thead>
<tr>
{% for day in days %}
<th>{{ day }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for day in days %}
<td>
{% for ev in event_list %}
{% if ev.date_from == day %}
{{ ev.name }}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
<tr>
{% for day in days %}
<td>
{% for ev in event_list %}
{% if ev.date_to == day %}
{{ ev.name }}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
2)php模板语言+ jQuery解决方案
<强>树枝强>
{% set days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"] %}
<div class="container">
<table class="table table-striped">
<thead>
<tr>
{% for day in days %}
<th>{{ day }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr id="event_start">
{% for day in days %}
<td name="{{ day }}">
</td>
{% endfor %}
</tr>
<tr id="event_end">
{% for day in days %}
<td name="{{ day }}">
</td>
{% endfor %}
</tr>
</tbody>
</table>
</div>
<强> jquery的:强>
<script>
$(document).ready(function(){
var event_list = {
event_1 : {event_name : "event_1", date_from : "monday", date_end : "sunday"},
event_2 : {event_name : "event_2", date_from : "wednesday", date_end : "tuesday"},
event_3 : {event_name : "event_3", date_from : "thursday", date_end : "friday"},
event_4 : {event_name : "event_4", date_from : "saturday", date_end : "saturday"},
event_5 : {event_name : "event_5", date_from : "friday", date_end : "tuesday"},
event_6 : {event_name : "event_6", date_from : "monday", date_end : "thursday"}
};
var first_row = $("#event_start");
var second_row = $("#event_end");
function loop (row, date) {
row.find("td").each(function(){
var t = $(this);
var name = $(this).attr("name");
$.each( event_list, function( key, value ) {
if (value[date] == name) {
t.text(value.event_name);
}
});
});
}
loop(first_row, "date_from");
loop(second_row, "date_end");
});
</script>