Full Calendar does not run PHP file

Asked

Viewed 185 times

0

I created an agenda with fullcalendar using this project website, but when I use PHP to get the events from the database, they are not displayed in the calendar, but if I call a json shows the event normally. In the PHP file I understood that a json is generated, but I don’t know how to save it to use. If anyone knows what is wrong, I appreciate the help. Follow the code below:

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>

php events.

<?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);

2 answers

0

You’re passing a JSON "like string" in:

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

You have some options, one would use:

$vetor[] = $linha;

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

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

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


Another, redundant way would be to use:

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

I recommend you see the documentation of json_encode() to know how this works.

  • I do not understand where this created json goes, I would like to save it paa use in the agenda, but while searching I did not find how to do this

  • The json "goes" to the client when it requests, in your first code, you define events: 'eventos.php',, soon it calls this page. Your server executes the query, mounts JSON and returns to fullCalendar, which is on the client’s side and executes the fullCalendar. The problem with your code is the error when generating JSON, this is what I mentioned in the first line.

  • But before using like this, I had tried the way you suggested, because the site I used base is used like this. It doesn’t work the way you suggested

  • You changed the query so that Mysql informs the id with the value of event_id? Se não você vai criar um json contendo event_id=1eid=1, em resumo. No seu código original você tenta utilizar id, por isso pressuponho que ele queira o ide nãoevent_id`.

  • Yes, I changed the id na in the query call

  • I can test this later, but I can’t see any other problems. See in F12 > Console if any problem is pointed out and also in F12 > Network if JSON is returned correctly.

  • Okay, I’ll test to see if it’s returning.

Show 2 more comments

0


I managed to resolve, I changed the connection to the PDO type and the while also, after the events appeared normally 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);

Browser other questions tagged

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