简体   繁体   English

在表中列出一周中每一天的事件

[英]Listing events in table for each day of the week

Although the code in question is PHP, I need a second look, a general algorithm, pseudocode or something similar as a solution. 尽管所讨论的代码是PHP,但我需要再看一遍,一般的算法,伪代码或类似的解决方案。

I have a collection of objects ( $events ) with properties: 我有一个具有属性的对象( $events )集合:

  • event_name
  • date_from
  • date_to

I have a table with weekdays: 我有一个有工作日的桌子:

         Mon   Tue   Wed   Thu   Fri   Sat   Sun  
Starts    *     *     *     *     *     *     *
Ends      *     *     *     *     *     *     *

Same table in HTML: 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 is just a placeholder and it isn't relevant. 星号只是一个占位符,与它无关。

I would like to display event_name under eg Wednesday if the event starts or ends on Wednesday. 如果事件在星期三开始或结束,我想在例如星期三下显示event_name Of course, if it starts on Wednesday ( date_from is a Wednesday), I would like it in the Starts row under Wed, and if it ends on Wednesday ( date_to is a Wednesday), I would like it in the Ends row under Wed. 当然,如果它在星期三开始( date_from是星期三),我希望它在Wed下的“ Starts行中;如果它在星期三结束( date_to是一个星期三),我希望它在Wed下的“ Ends行中。

I have a working solution, but it's so bad that I have second thoughts of even writing it here :) 我有一个可行的解决方案,但是这太糟糕了,甚至我在这里写了第二个想法:)

The solution was to take that HTML and put in each <td>*</td> instead of asterisk these lines of PHP (example for Monday's <td> ): 解决方案是采用该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>

For Tuesday's <td> , it would be something like: 对于星期二的<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>

...and so on, but 14 of these foreach 's and if 's for each day times two rows is probably as stupid as it gets :). ...依此类推,但是每天foreach 14个foreachifif连续两行,它可能会变得很愚蠢:)。 Of course, for Ends row I compare date_to property to the day in question. 当然,对于“ Ends行,我将date_to属性与相关日期进行比较。

Does anyone have a conceptual answer? 有人有概念上的答案吗? How to aproach this problem in a better way? 如何更好地解决这个问题?

PS I'm working in Laravel, PHP MVC framework, so I'm building a collection in a controller and passing it on to the view. PS我正在Laravel,PHP MVC框架中工作,因此我正在控制器中构建集合并将其传递给视图。 Dates are Carbon instances, which is a PHP API extension for DateTime so various handy methods for determining days are available ( addDay() , startOfWeek() , etc.). 日期是Carbon实例,它是DateTime的PHP API扩展,因此可以使用各种方便的方法来确定日期( addDay()startOfWeek()等)。 The HTML in question can be changed, as can be the structure of $events collection, if necessary. 如有必要,可以更改所讨论的HTML,以及$events集合的结构。

Thank you in advance for your ideas. 预先感谢您的想法。

EDIT: Bonus points for putting comma after each event_name except last, in a table cell ( <td> ). 编辑:在表单元格( <td> )中,除最后一个event_name之外,每个event_name之后都放置逗号的加分。

1) php template language only solution 1)仅PHP模板语言解决方案

twig: 枝条:

{% 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 template language + jQuery solution 2)php模板语言+ jQuery解决方案

twig 枝条

{% 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: 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>

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

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