Show Fullcalendar events using PHP POO

Asked

Viewed 333 times

0

I’m almost in a Karate Kid position with the notebook because of Fullcalendar :/

The thing is, I’m trying to get the data from a table through Fullcalendar. As I use the procedural mode, it stayed that way:

Fullcalendar

      var calendar = $('#calendar').fullCalendar({

     header: {
       left: 'prev,next today',
       center: 'title',
       right: 'month,agendaWeek,agendaDay'
     },
     defaultDate: Date(),
     navLinks: true, 
     editable: true,
     eventLimit: true, 
     eventClick: function(event) {
       $('#visualizar #id').text(event.id);
       $('#visualizar #title').text(event.title);
       $('#visualizar #start').text(event.start.format('DD/MM/YYYY HH:mm:ss'));
       $('#visualizar #end').text(event.end.format('DD/MM/YYYY HH:mm:ss'));
       $('#visualizar').modal('show');
       return false;
     },
     selectable: true,
     selectHelper: true,
     select: function(start, end){
       $('#cadastrar #start').val(moment(start).format('DD/MM/YYYY HH:mm:ss'));
       $('#cadastrar #end').val(moment(end).format('DD/MM/YYYY HH:mm:ss'));
       $('#cadastrar').modal('show');
     },
        events:<?php echo $metodos->visualizarAgenda($idEmpresa,$idUsuario); ?>,

     eventDrop:function(event)
     {
      var start = $.fullCalendar.formatDate(event.start, "Y-MM-DD HH:mm:ss");
      var end = $.fullCalendar.formatDate(event.end, "Y-MM-DD HH:mm:ss");
      var title = event.title;
      var id = event.id;

      $.ajax({
       url:"editar.php",
       type:"POST",
       data:{title:title, start:start, end:end, id:id},
       success:function()
       {
        $('#confirmar').modal('show');
        setTimeout(function() {
            $('.modal').modal('hide');
        }, 2000);
        calendar.fullCalendar('refetchEvents');
       }
      });
     }
   });
  });

Method to visualize Genda()

<?php
...
public function visualizarAgenda($idEmpresa,$idUsuario){

    $sqlVisualizar = mysqli_query($this->conexao,"SELECT * FROM pe_agenda WHERE IdEmpresa = '".mysqli_real_escape_string($this->conexao,$idEmpresa)."' AND IdUsuarios = '".mysqli_real_escape_string($this->conexao,$idUsuario)."';");

    $data = array();
    $peVisualizar = mysqli_fetch_array($sqlVisualizar);

    foreach($peVisualizar as $listar){
            $data[] = array(
              'id' => $listar["IdAgenda"],
              'title' => $listar["Titulo"],
              'start' => $listar["DataAgendamento"]
            );
   }
     return json_encode($data);
}

The problem is that events do not appear in the calendar, but if I put directly:

events: [
   id: '11',
   title: 'Vamos testar',
   start: '2018-06-11'
],

They appear normally and when I print a query and query directly in the BD, I see that there is nothing wrong with it.

The return appears this way:

[{"id:":"3","title:":"3","start:":"3"},{"id:":"3","title:":"3","start:":"3"},{"id:":"1","title:":"1","start:":"1"},{"id:":"1","title:":"1","start:":"1"},{"id:":"9","title:":"9","start:":"9"},{"id:":"9","title:":"9","start:":"9"},{"id:":"1","title:":"1","start:":"1"},{"id:":"1","title:":"1","start:":"1"},{"id:":"R","title:":"R","start:":"R"},{"id:":"R","title:":"R","start:":"R"},{"id:":"#","title:":"#","start:":"#"},{"id:":"#","title:":"#","start:":"#"},{"id:":"0","title:":"0","start:":"0"},{"id:":"0","title:":"0","start:":"0"},{"id:":"1","title:":"1","start:":"1"},{"id:":"1","title:":"1","start:":"1"},{"id:":"F","title:":"F","start:":"F"},{"id:":"F","title:":"F","start:":"F"},{"id:":"2","title:":"2","start:":"2"},{"id:":"2","title:":"2","start:":"2"}]

When I use it that way:

...
    $data = array();

    while($listar = mysqli_fetch_array($sqlVisualizar)) {
          $data[] .= "id:" .$listar["IdAgenda"].",";
          $data[] .= "title:" .$listar["Titulo"].",";
          $data[] .= "start:" .$listar["DataAgenda"].",";
    }
   return json_encode($data);

He returns this way:

"id:3","title:Reuni\u00e3o da diretoria","start:07\/06\/2018","id:4","title:Testes dos candidatos","start:08\/06\/2018","id:5","title:Festa Junina","start:09\/06\/2018"

1 answer

1

I managed to fix it. It stayed that way:

$data = array();

  while($listar = mysqli_fetch_array($sqlVisualizar)) {

       list($dia,$mes,$ano) = explode("/",$listar["DataAgenda"]);

       $dataEvento = $ano."-".$mes."-".$dia;
       $linhas['id'] = $listar["IdAgenda"];
       $linhas['title'] = $listar["Titulo"];
       $linhas['start'] = $dataEvento;

       array_push($data,$linhas);
}
return json_encode($data,JSON_UNESCAPED_SLASHES);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.