0

Hope someone can help me. Im trying to integrate Fullcalendar (jQuery) to my website. Its version 1.6.4.

I allready have a database with content I would like to add to the calendar. I my database the raws have the names: - dispalydate (a given date for the event given in the format 2014-03-23. It should be displayed that date) - Name (title of the event) - id (id in the database)

I can see that Fullcalendar use the below code (from the file json-events.php) to show something in the calender:

<?php
$year = date('Y');
$month = date('m');
echo json_encode(array(
    array(
        'id' => 111,
        'title' => "Event1",
        'start' => "$year-$month-10",
        'url' => "http://yahoo.com/"
    ),
    array(
        'id' => 222,
        'title' => "Event2",
        'start' => "$year-$month-20",
        'end' => "$year-$month-22",
        'url' => "http://yahoo.com/"
    )
));
?>

I have replaced the above code with my own code below. I have connection to the database but nothing is showed in the Fullcalendar

<?php
// Create connection
$con=mysqli_connect("localhost","*****","*****","*****");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
session_start();

$result = mysql_query("SELECT id, name, displaydate AS displaydate FROM caledar");
mysql_close();
$events = array();
while ($row=mysql_fetch_array($result)){            
$id =  $row['id'];
$title = $row['name'];
$start = $row['displaydate']; 

$events = array(
'id' =>  "$id",
'name' => "$title",
'displaydate' => "$start"
);

}
echo json_encode($events);
?>

Can someone advice or see what om doing wrong?

2 Answers2

3

you should have the same names in the array you create as in the example. Replace "displaydate" with "start", and "name" with "title"

$events[] = array(
    'id' =>  "$id",
    'title' => "$title",
    'start' => "$start"
);
nikos.svnk
  • 1,375
  • 1
  • 13
  • 24
  • I tried: `$id = $row['id']; $title = $row['name']; $start = $row['displaydate']; $events = array( 'id' => "$id", 'title' => "$title", 'start' => "$start" );` but still not working. Any other suggestions? – Gerner Christensen Mar 23 '14 at 15:58
  • sorry didn't notice before. It should be `$events[] = array(...` . otherwise it returns only the last event – nikos.svnk Mar 23 '14 at 16:15
  • So now the codes looks like: ` $events = array(); while ($row=mysql_fetch_array($result)){ $id = $row['id']; $title = $row['name']; $start = $row['displaydate']; $events[] = array( 'id' => "$id", 'title' => "$title", 'start' => "$start" );` but its still the same. Nothing show up. The dateformat in my database is 2014-03-23 do the Fullcalendar understand that? – Gerner Christensen Mar 23 '14 at 20:21
  • from fullCalendar documentation: "When specifying Event Objects for events or eventSources, you may specify a string in IETF format (ex: "Wed, 18 Oct 2009 13:00:00 EST"), a string in ISO8601 format (ex: "2009-11-05T13:15:30Z") or a UNIX timestamp." . So you either have to save them in different format, or convert them... – nikos.svnk Mar 23 '14 at 20:33
  • nikos.svnk so is that the problem? I have a CMS where everything is saved with the DATE formate in mysql like 2014-01-20 cant i use that? What do I do to make it work with fullCalendar? Please advice. – Gerner Christensen Mar 23 '14 at 21:19
  • a replace in $start so to become: `$start = $row['displaydate'] . 'T00:00:00Z' ;` will work for you. It's not the most professional way though, so if you want to take care of timezones you have to do it by creating a new date object http://stackoverflow.com/questions/5322285/how-do-i-convert-datetime-to-iso-8601-in-php – nikos.svnk Mar 23 '14 at 21:42
  • I have tried to add `$start = $row['displaydate'] . 'T00:00:00Z' ;` but its still the same, nothing show up. my codes look like ` $result = mysql_query("SELECT id, name, displaydate AS displaydate FROM caledar"); mysql_close(); //$events = array(); $events = array(); while ($row=mysql_fetch_array($result)){ $id = $row['id']; $title = $row['name']; $start = $row['displaydate'] . 'T00:00:00Z' ; $events[] = array( 'id' => "$id", 'title' => "$title", 'start' => "$start" ); } echo json_encode($events);` – Gerner Christensen Mar 24 '14 at 08:06
  • forgot to tell: In my database i have like 1 or 2 events everyday for the rest of the year that I would like it to show. At the moment nothing is showd in the fullCalendar, even not the first or the last event. You have an idea how to get this working? – Gerner Christensen Mar 24 '14 at 08:07
  • I found out that in the orginal file it looks like: `'id' => 111, 'title' => "Event1", 'start' => "$year-$month-10", 'url' => "http://yahoo.com/"` but if I change `$year-$month-10` to `2014-03-24` its still working and showing up on that date. This Means that I can use my date format as it is in the database. I now thig its because I dont get the right connection to the database. I know I have contact to the database but i dont get the info fro the database. Please see my first post to see if I have done something wrong - or what can it else be? – Gerner Christensen Mar 24 '14 at 10:07
  • you don't need mysql_close() in general, and especially before fetching the results – nikos.svnk Mar 24 '14 at 10:46
  • ok, I have removed that part compleatly so now my codes look like` session_start(); $result = mysql_query("SELECT id, name, displaydate AS displaydate FROM calendar"); $events = array(); while ($row=mysql_fetch_array($result)){ $id = $row['id']; $title = $row['name']; $start = $row['displaydate']; $events[] = array( 'id' => "$id", 'title' => "$title", 'start' => "$start" ); } echo json_encode($events);` But still nothing show up. can you see what im doing worng? – Gerner Christensen Mar 24 '14 at 11:17
  • Any help or suggestions? Im still facing issues – Gerner Christensen Mar 25 '14 at 09:12
0

I fixed the codes my self and its now working. If someone else should have the same issue, here is my code:

$query = "select * from calender";

$res = mysql_query($query) or die(mysql_error());
$events = array();
while ($row = mysql_fetch_assoc($res)) {
    $eventsArray['id'] =  $row['id'];
    $eventsArray['title'] = $row['name'];
    $eventsArray['start'] = date($row['displaydate']);
$eventsArray['url'] = 'eventlink.php?id=' . $row['id'];
$eventsArray['type'] = $row['type'];
$eventsArray['description'] = $row['description'];
    $events[] = $eventsArray;
}
echo json_encode($events);