Search code examples
phphtmlconceptual

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.

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


Solution

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