0
votes

I'm using Mysql, Pdo, Apache, InnoDB, php 5.4.16. I need to display events >=CURDATE(). The events could have a duration of 1 day (tipo_id=1) or more (tipo_id=4). Each event has a date I stored in data_evento column as start date. For events having a duration of more than one day I use a fine_data_evento column where I stored the end date. Because not all the events have an end date I set fine_data_evento as NULL. So I have no problem to display every kind of event starting from today with single date or a "from-to" range of date. For example:

Title:Event 01
Date: 12 September 2019
========================
Title:Event 02
Date: From 12/09/2019 to 10/10/2019
========================
Title:Event 03
Date: 13 September 2019

The problem is when I try to display Event 02 the day after (13/09) its starting date (12/09 in the example), as SQL condition is data_evento>=CURDATE().

So the question is: how to continue to display events which starts before today and ends some days later, where all the others events start from today?

Maybe Do i need some if in SQL? I red other posts but I could not find an answer. So I can't understand which is a way to work well. I know it is simple but I can't. Thank you.

On the following I have some code.

tutti_eventi.php

....
$sql = 'SELECT `id_evento`,`titolo_evento`, `desc_evento`,`data_evento`,`fine_data_evento`, `nome_image`, `nome_categoria`,`comune_nome`,`nome_provincia`,`tipo_id` FROM `eventi`, `images`,`categoria`,`comuni`,`provincia`
              WHERE
              `id_image`=`id_img`
              AND
              `id_categoria`=`cat_id`
              AND
              `id_comune`=`com_id`
              AND
              `id_provincia`=`id_prov`
              AND
              `tipo_id` in (1,4)
              AND
              `data_evento` >= CURDATE()
              ORDER BY
              `data_evento` ASC';
 ....

tutti_eventi.html.php

 ....
 while ($evento = $eventi->fetch(PDO::FETCH_ASSOC)) {
                            ....
                            <div class="col-sm-12 col-md-12" style="padding-top:5px;">

                              <?php

                              if (isset($evento['fine_data_evento'])) {

                                $tipo=$evento['tipo_id'];

                                if ($tipo = 4) {

                                echo '<p><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
                                          <span class="glyphicon-class">';

                                            echo '<span>Dal ';
                                              Setlocale(LC_ALL, 'it_IT');
                                              $date3= new DateTime($evento['data_evento']);
                                              echo $date3->format('d/m/Y');
                                            echo '</span>';
                                            echo '<span> al ';
                                              Setlocale(LC_ALL, 'it_IT');
                                              $date4= new DateTime($evento['fine_data_evento']);
                                              echo $date4->format('d/m/Y');
                                            echo '</span>';

                                          echo '</span></p>';
                                        }


                                } else {

                                  echo '<p><span class="glyphicon glyphicon-calendar" aria-hidden="true"></span>
                                            <span class="glyphicon-class">';

                                              echo '<span>';
                                                Setlocale(LC_ALL, 'it_IT');
                                                $date= new DateTime($evento['data_evento']);
                                                echo $date->format('l');
                                              echo '</span><br>';
                                              echo '<span>';
                                                Setlocale(LC_ALL, 'it_IT');
                                                $date1= new DateTime($evento['data_evento']);
                                                echo $date1->format('d F Y');
                                              echo '</span>';
                                            echo '</span><br>';

                                              echo '<span class="glyphicon glyphicon-time" aria-hidden="true"></span>
                                                    <span class="glyphicon-class">';
                                                        $date2= new DateTime($evento['data_evento']);
                                                        echo $date2->format('H'.':'.'i');

                                                    echo '</span></p>';

                                }

                                echo '</div>';

                              echo '<div class="col-sm-12 col-md-12">'.$evento['desc_evento'].'</div>';

                      echo '</div>';

                  }
                            ?>

Thanks to @GMB in particular I am very close to the solution. I added COALESCE(fine_data_evento, data_evento) >= CURDATE() in my SQL query. But I need to ORDER BY ASC. When I use DESC it works fine: Event 2 is displayed and Events are displayed starting by the last date on the top of the page, while Event 2 is the first at the bottom. When I use ASC instead Event 2 can't display and the events are correctly displayed starting by today on the top. Watch at the new query:

    $sql = 'SELECT `id_evento`, `titolo_evento`, `desc_evento`, `data_evento`, `fine_data_evento`, `nome_image`, `nome_categoria`, `comune_nome`, `nome_provincia`, `tipo_id` FROM `eventi`,  `images`, `categoria`, `comuni`, `provincia`
                  WHERE
                  `id_image`=`id_img`
                  AND
                  `id_categoria`=`cat_id`
                  AND
                  `id_comune`=`com_id`
                  AND
                  `id_provincia`=`id_prov`
                  AND
                  `tipo_id` in (1,4)
                  AND
                  COALESCE(`fine_data_evento`, `data_evento`) >= CURDATE()
                  ORDER BY `data_evento` ASC';

And the following is the solution. I red at this post:

How to order by 2 columns combining COALESCE?

The solution is to add:

COALESCE(fine_data_evento, data_evento) >= CURDATE() - INTERVAL 1 DAY ORDER BY COALESCE (fine_data_evento) ASC, data_evento ASC

It works: the Event 2 is placed at the bottom of the page when its start date is before today, while events with null end date are sorted by today on the top of the page.

Thank you to all

1
Share your table schema.Dark Knight
I'm sorry, I know this doesn't help, but...PHP 5.4? Don't use PHP 5.4. Relevant - youtu.be/wCZ5TJCBWMg?t=2376GTCrais

1 Answers

0
votes

If I followed you correctly, you could phrase the condition as:

( fine_data_evento IS NULL AND data_evento >= CURDATE() )
OR (fine_data_evento >= CURDATE() AND data_evento <= CURDATE() )

This means: if there is no end date, then the start date must be greater or equal than today. If there is an end date, then it must be greater or equal than today, while the start_date must be less or equal than today.

Demo on DB Fiddle:

Given the following sample data:

| title   | data_evento | fine_data_evento |
| ------- | ----------- | ---------------- |
| Event 1 | 2019-09-11  |                  |
| Event 2 | 2019-09-10  | 2019-10-10       |
| Event 3 | 2019-09-13  |                  |
| Event 4 | 2019-09-01  |                  |
| Event 5 | 2019-09-01  | 2019-09-10       |
| Event 6 | 2019-10-01  | 2019-10-10       |

Current date is '2019-09-11', and the query returns:

| title   | data_evento | fine_data_evento |
| ------- | ----------- | ---------------- |
| Event 1 | 2019-09-11  |                  |
| Event 2 | 2019-09-10  | 2019-10-10       |
| Event 3 | 2019-09-13  |                  |