dre75230 2016-02-27 14:32
浏览 44
已采纳

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

Although the code in question is PHP, I need a second look, a general algorithm, pseudocode or something similar as a solution.

I have a collection of objects ($events) with properties:

  • 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:

<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 e.g. Wednesday if the event starts or ends on Wednesday. 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.

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>):

<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>
  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 :). Of course, for Ends row I compare date_to property to the day in question.

Does anyone have a conceptual answer? How to aproach this problem in a better way?

P.S. I'm working in Laravel, PHP MVC framework, so I'm building a collection in a controller and passing it on to the view. Dates are Carbon instances, which is a PHP API extension for DateTime so various handy methods for determining days are available (addDay(), startOfWeek(), etc.). The HTML in question can be changed, as can be the structure of $events collection, if necessary.

Thank you in advance for your ideas.

EDIT: Bonus points for putting comma after each event_name except last, in a table cell (<td>).

  • 写回答

1条回答 默认 最新

  • doujia7094 2016-02-27 19:17
    关注

    1) php template language only solution

    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

    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:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算