Jump to content

FullCalendar not displaying data from MySQL


 Share

Go to solution Solved by Barand,

Recommended Posts

Hi guys,

I am trying to display tour  information in fullcalendar but nothing is happening and no errors.

Here is the php code from fetch-tours.php

<?php
    require_once "config.php";

    $json = array();
    $sqlQuery = "SELECT * FROM jobs ORDER BY id";

    $result = mysqli_query($con, $sqlQuery);
    $eventArray = array();
    while ($row = mysqli_fetch_assoc($result)) {
    $title = isset($row['name']);
    $start = isset($row['dep_date']);
    $end = isset($row['ret_date']);
    $eventsArray['title'] = $title;
    $eventsArray['start'] = $start;
    $eventsArray['end'] = $end;
        array_push($eventArray, $row);
    }
    mysqli_free_result($result);

    mysqli_close($con);
    echo json_encode($eventArray);
    ?>

And here is the Javascript that displays the calendar:

<script type="text/javascript">
    $(document).ready(function() {
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();

        $('#calendar').fullCalendar(
        {
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,basicWeek,basicDay'
            },
            editable :true,
            events: "includes/fetch-tours.php",
        });
    })
</script>

The calendar displays fine but just no data.

Any help is greatly appreciated.

Cheers,

Dan

Link to comment
Share on other sites

Posted (edited)
20 minutes ago, requinix said:
    $title = isset($row['name']);
    $start = isset($row['dep_date']);
    $end = isset($row['ret_date']);

Are you sure that's what you want to do with those variables?

I have changed that now.

All I want to do is display the name, start date and finish date in the calendar.

$data = array();
$query = "SELECT * FROM tours ORDER BY id";
$statement = $con->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach ($result as $row) {
    $data[] = array(
        'id' => $row["id"],
        'title' => $row["name"],
        'start' => $row["dep_date"],
        'end' => $row['ret_date']
    );
}
echo json_encode($data);

I have never used calendars before and am clueless with Javascript etc so any help with this would be appreciated.

Edited by DanEthical
Link to comment
Share on other sites

Seems alright, but there are 5 versions of FullCalendar and I don't know which one you're using.

Check the browser's error console for any messages, and use its request monitoring tools to make sure that your fetch-tours.php really is being called and returning the data you think it is.

Link to comment
Share on other sites

5 minutes ago, requinix said:

Seems alright, but there are 5 versions of FullCalendar and I don't know which one you're using.

Check the browser's error console for any messages, and use its request monitoring tools to make sure that your fetch-tours.php really is being called and returning the data you think it is.

Hi mate,

Nothing in console. Just a few things with datatables, which I am aware of.

Is there an alternative calendar script that I can use that you would recommend?

Thanks for your help.

Link to comment
Share on other sites

  • Solution

Do not use SELECT *

Specify what you want and your (PDO) code becomes

$query = "SELECT id 
               , name as title
               , dep_date as start
               , ret_date as end
          FROM tours 
          ORDER BY id";
$result = $con->query($query);
echo json_encode($result->fetchAll());

 

Link to comment
Share on other sites

5 hours ago, requinix said:

What I would recommend is not abandoning this because you're having a small problem.

Did you confirm that fetch-tours.php is executing and returning the correct data?

Hi mate,

Did a Var dump (I think) and this is what it showed:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> array(1) { [0]=> int(1) } ["num_rows"]=> int(1) ["type"]=> int(0) }

 

Link to comment
Share on other sites

Got is working

<?php
    require_once "config.php";

    $json = array();
    $sqlQuery = "SELECT id, name AS title, dep_date AS start, ret_date AS end FROM jobs ORDER BY id";

    $result = mysqli_query($con, $sqlQuery);
    $eventArray = array();
    while ($row = mysqli_fetch_assoc($result)) {
        array_push($eventArray, $row);
    }
    mysqli_free_result($result);

    mysqli_close($con);
    echo json_encode($eventArray);
    ?>

Thank you both so much for your help. Love your work. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.