Full calendar does not execute PHP file

0

I've created a calendar with fullcalendar using the project of this site , but when I use PHP to get the bank events, they are not displayed in the schedule, but if I call a json it shows the event normally. In the PHP file I understand that a json is generated, but I do not know how to save it to use. If anyone knows what's wrong, I appreciate the help. Here is the code:

calendar.php

  <script>
           $(document).ready(function() {   

            //CARREGA CALENDÁRIO E EVENTOS DO BANCO
            $('#calendario').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    //right: 'month,agendaWeek,agendaDay'
                },
                //defaultDate: '2016-01-12',
                editable: true,
                eventLimit: true, 
                events: 'eventos.php',          
                eventColor: '#dd6777'
            }); 
       }); 

    </script>

    <style>
        #calendario{
            position: relative;
            width: 70%;
            margin: 0px auto;
        }        
    </style>

</head>
<body>    
    <div id='calendario'>
    </div>
</body>

events.php

<?php
  $conexao = mysqli_connect('', '', '', ''); 

    $consulta = $conexao->query("SELECT event_id, title, start, end FROM eventos"); 
    while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)){
        $vetor[]= "{id:{$linha['event_id']}, title: {$linha['title']}, start: {$linha['start']}, end:{$linha['end']}}";
     }
    //Passando vetor em forma de json
    echo json_encode($vetor);
    
asked by anonymous 03.04.2017 / 15:00

2 answers

0

I was able to resolve, I changed the connection to the PDO type and the while also, after the events usually appeared through the PHP file.

$conexao = new PDO('mysql:host=$host;dbname=$db', '$user', '$senha');
        $consulta = $conexao->query("SELECT event_id as id, title, start, end FROM eventos"); 
        while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) { 
            $vetor[] = $linha;
         }
        //Passando vetor em forma de json
        echo json_encode($vetor);
    
03.04.2017 / 20:26
0

You are passing a JSON "as string " into:

$vetor[]= "{id:{$linha['event_id']}, title: {$linha['title']}, start: {$linha['start']}, end:{$linha['end']}}";

You have some options, one would be to use:

$vetor[] = $linha;

For this to work use event_id as id , so an array with the key id will be created with the value of event_id , by security set all columns :

SELECT 'event_id' as 'id', 'title', 'start', 'end' FROM eventos

This way you will return an array with exactly all the columns.

Another redundant way would be to use:

$vetor[] = [
   'id' => $linha['event_id'], 
   'title' => $linha['title'], 
   'start' => $linha['start'], 
   'end' => $linha['end']
];

I recommend that you view the json_encode() documentation to find out how this works.

    
03.04.2017 / 15:10