0

I want to add an IF inside the events in full calendar, like, if date is not null use date, else, use dow. Something like this, but this gives me white screen.

         events: [<?php
        while($row_events = mysqli_fetch_array($resultado_events)){
                            ?>
                            {
        title: '<?php echo $row_events['title'];?>', 
            if(<?php echo $row_events['date']; ?> != '')
            {
                start: '<?=$row_events['date'].' '.$row_events['start'];?>',
                end: '<?=$row_events['date'].' '.$row_events['end'];?>',
            }
            else {
               start: '<?php echo $row_events['start']; ?>',
               end: '<?php echo $row_events['end']; ?>',
               dow: [ <?php echo $row_events['dow']; ?> ],
                }
         },<?php
                        }
                    ?>],

So basically, it is supposed to work like this. If it has a date, it will use the date and only show on that day, if date in database is null, use the dow and recurre the event.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Gpsi Turma
  • 11
  • 3
  • I think it would be better to prepare the json data before calling the full calendar code, and assign json data to events. For example: $emparray = array(); while($row =mysqli_fetch_assoc($result)) { $emparray[] = $row; } var jdata = json_encode($emparray); – Khurram Ishaque May 16 '19 at 11:11
  • Basically, you would need jquery ajax to fetch records, than on document.ready or DOMContentLoaded eventListener, you would assign the json data to Full Calendar event – Khurram Ishaque May 16 '19 at 11:15

1 Answers1

1

"gives me white screen"

This could be a PHP error. Or, if there is nothing else in your page except fullCalendar, it could be a JavaScript error preventing the calendar code from running to display it. Did you enable PHP error reporting to check for server errors? Did you look in your browser's Console to check for JavaScript errors? If so, you haven't reported it here. It would help you to solve your problems more easily if you check for this, and learn to debug.


Anyway from looking at your code I can see it'll be a JavaScript syntax error - you can't put an if statement inside an object literal - it's a data structure, not a code block.

A more reliable and maintainable way to create your event data would be to create the necessary object in pure PHP, and then encode it to JSON to output into the JavaScript (where you can inject it as if it was an object literal and have it treated as code directly without any parsing required). Here's an example:

First create the event array in PHP:

$events = array();

while($row_events = mysqli_fetch_array($resultado_events))
{
    $event = array();
    $event["title"] = $row_events['title'];

    if($row_events['date'] != "")
    {
         $event["start"] = $row_events['date'].' '.$row_events['start'];
         $event["end"] = $row_events['date'].' '.$row_events['end'];,
    }
    else {
       $event["start"] = $row_events['start'];
       $event["end"] = $row_events['end'];
       $event["dow"] = explode(",", $row_events['dow']); //N.B. exactly how you do this might depend on the precise content of your "dow" field, e.g. if values are separated by just comma, or comma and a space
    }
}
$eventsJSON = json_encode($events);

Then, further down your script, where you add the JavaScript code, you can simply write:

events: <?php echo $eventsJSON; ?>,
ADyson
  • 57,178
  • 14
  • 51
  • 63