Fecting event in Full Calender using codeigniter? - php

I successfully store and retrive event from database using ajax now how to show this into events in fullcalnder??
Saving event in Database: Using Ajax.
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultDate: '2015-02-12',
selectable: true,
selectHelper: true,
select: function(start, end) {
var title = prompt('Event Title:');
var eventData;
if (title) {
eventData = {
title: title,
start: start,
end: end
};
$.ajax ({
url: "http://localhost/phplec/sms/calendar/test",
type: 'POST',
data: {'title': title, 'start' : start.format(), 'end': end.format()},
success: function(msg) {
alert(msg);
}
});
Controller: Here i Accept Ajax request and connect to DB
function test() {
$this->load->model('calendar_model');
$data = array(
'title' => $_POST['title'],
'start' => $_POST['start'],
'end' => $_POST['end']
);
$this->calendar_model->add_record($data);
echo "working";
}
Now I want To show my database in fullCalander on page laod.
I successfully get event from database and convet it into json now how to show it on view.
function get_records()
{
$data = $this->db->get("calendar_test");
$json_data = json_encode($data->result_array());
var_dump($json_data);
}
How i Show this event Dynamically
$.ajax ({
url: "http://localhost/phplec/sms/calendar/get_records",
type: 'GET',
dataType: "html", //expect html to be returned
success: function(response){
alert(response)
}
I successfully get event json in fullcalander.js now how to pass this to events.

OK from what I understand you want to click on a calendar day, it prompts you for a title, then you submit the data to the DB and then you want to see it again. These are the steps you need to take:
you need to use the events method so that fullcalendar knows how to fetch events when its loaded, like this:
$('#calendar').fullCalendar({
// ....your other methods...
events: 'http://localhost/phplec/sms/calendar/get_records' //this should echo out JSON
});
after your ajax succesfully saves the events in the database, then call the refetchEvents method like this:
$('#calendar').fullCalendar('refetchEvents');
In the end, this is how I would do it:
var myCalendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: 'http://localhost/phplec/sms/calendar/get_records',
defaultDate: '2015-02-12',
selectable: true,
selectHelper: true,
select: function(start, end) {
var title = prompt('Event Title:');
if (title) {
var eventData = {
title: title,
start: start.format(),
end: end.format()
};
$.ajax({
url: "http://localhost/phplec/sms/calendar/test",
type: 'POST',
data: eventData,
success: function(result) {
if(result === "working"){
myCalendar.fullCalendar('refetchEvents');
}else{
alert("Error Message");
}
},
error: function(xhr, status, msg) {
alert(msg);
}
});
}
}
});

Related

how to display data from database on full calendar?

i have the following data
then i want to display in full calendar
<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek'
},
events: function(start, end, timezone, callback) {
$.ajax({
url: '<?php echo site_url("absentMonthYearController/absentYearController/calendarShow/" . $nik) ?>',
type: 'GET',
dataType: 'JSON',
success: function(data) {
var events = [];
if (data != null) {
$.each(data, function(i, item) {
events.push({
start: item.date,
title: 'Present',
display: 'background'
})
})
}
console.log('events', events);
}
})
}
});
calendar.render();
});
the data doesn't appear in the calendar, but it does appear in console.log
There are two issues here:
events: function(start, end, timezone, callback) { is wrong for fullCalendar 5. That looks like the syntax from an earlier version. https://fullcalendar.io/docs/v5/events-function shows the function's signature in v5, which is
function( fetchInfo, successCallback, failureCallback ) {
You forgot to use the provided callback to return the events to fullCalendar. So your function is downloading the events and logging them, but they never make it to the calendar.
Therefore this:
events: function( fetchInfo, successCallback, failureCallback ) {
$.ajax({
url: '<?php echo site_url("absentMonthYearController/absentYearController/calendarShow/" . $nik) ?>',
type: 'GET',
dataType: 'JSON',
success: function(data) {
var events = [];
if (data != null) {
$.each(data, function(i, item) {
events.push({
start: item.date,
title: 'Present',
display: 'background'
})
})
}
console.log('events', events);
successCallback(events);
}
})
}
should fix it.
P.S. Really you should also be using the start and end dates provided inside the fetchInfo object to send to your server, and the server should be using them to restrict the events it returns to only those which occur (or overlap) within those dates. That way you don't return a large number of events which mostly never get viewed, instead you only return what's relevant to the date range bein displayed.

How to use jQuery Ajax to fetch the data from MS-SQl database?

I'm developing an Event Calendar in PHP, saving the data in MS-SQL.
I was successful in sending the data to the database, however, I'm not able to fetch the data from it.
Here's what I've done so far:
$(document).ready(function() {
var calendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay'
},
navLinks: true, // can click day/week names to navigate views
editable: true,
eventLimit: true,
events: "all_events.php",
displayEventTime: false,
eventRender: function(event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
var title = prompt('Event Detail:');
if (title) {
var start = $.fullCalendar.formatDate(start, "Y-MM-DD HH:mm:ss");
var end = $.fullCalendar.formatDate(end, "Y-MM-DD HH:mm:ss");
$.ajax({
url: 'add_event.php',
data: 'title=' + title + '&start=' + start + '&end=' + end,
type: "POST",
success: function(data) {
displayMessage("Added Successfully");
}
});
calendar.fullCalendar('renderEvent', {
title: title,
start: start,
end: end,
allDay: allDay
}, true);
}
calendar.fullCalendar('unselect');
}
});
});
all_events.php
<?php
require_once("connection.php");
$conn = DB::databaseConnection();
$json = array();
$sql = "SELECT * FROM Events ORDER BY id";
$result = $conn->prepare($sql);
$result->bindParam(':id', $id);
$alldata = array();
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
array_push($alldata, $row);
}
echo json_encode($alldata);
?>
I'm just getting a '[]' in the console. No other error and the calendar is blank, it is not displaying any data from the database.

FullCalendar IO not showing Database Results

I want to implement a calendar option that can be able to show the list of events in a specific day. I query my results from the Database and it comes as a JSON format below :
[
{
title: "No of Appointments : 48",
START: "1970-01-01"
},
{
title: "No of Appointments : 2",
START: "2017-06-14"
},
{
title: "No of Appointments : 2",
START: "2017-06-15"
},
{
title: "No of Appointments : 1",
START: "2017-06-20"
},
{
title: "No of Appointments : 1",
START: "2017-06-21"
}
]
And my jquery file is as below :
var cal = jQuery.noConflict();
cal(document).ready(function () {
function get_data() {
cal.ajax({
url: "<?php echo base_url(); ?>Reports/get_count_apointments/",
type: 'GET',
dataType: 'JSON',
success: function (data) {
var appointment_info = data;
draw_calendar(appointment_info);
}, error: function (jqXHR) {
console.log(jqXHR);
}
});
}
get_data();
function draw_calendar(appointment_info) {
cal('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: appointment_info
});
}
});
However the following opens the calendar with no marked event date.
Hence a blank page with no event on a specific date.
How can I solve this issue?

Full calendar loop with description

I have a problem with full calendar, it's not showing the description i dont know why.. my code is:
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
buttonText: {
today: 'hoy',
month: 'mes',
week: 'semana',
day: 'día'
},
//Random default events
events: function(start, end, timezone, callback) {
$.ajax({
url: "<?php echo base_url(); ?>calendar/retrieve",
type: 'POST',
dataType: 'html',
data: {
start: start.format(),
end: end.format()
},
success: function(doc)
{
var events = [];
var eventRender = [];
if(!doc.result)
{
$.each($.parseJSON(doc), function() {
events.push({
title: this.description,
start: this.start_date,
end: this.end_date,
backgroundColor: "#0073b7",
description: 'second description',
borderColor: "#0073b7"
});
});
}
callback(events);
}
});
},
I'm getting the information from a database and then I show it in the calendar... I put description: 'some description...', but when I see the calendar it is not displaying that, why? or if you know another way to put a description, a tooltip maybe i dunno... I have seen examples but without a loop and i need how to put with a loop..
I am using the fullcalendar version 3.4.0
Thanks!
You can try to change $.each loop like this:
$.each(JSON.stringify(doc), function (i, d) {
events.push({
title: doc[i].description,
start: doc[i].start_date,
end: doc[i].end_date,
backgroundColor: "#0073b7",
description: 'second description',
borderColor: "#0073b7"
});
});

How can i pass $_GET to another php via ajax?

Im going to make a kind of explanation about my problem:
Trying to integrate fullcalendar on a web that takes data via mysql & ajax, i want to display booking dates on calendar only if its related with one "house" in particular when this one is clicked.
So, in my php i receive $pisoid = $_GET['piso']; then i use in the same page a js script and i pass that data via post ajax:
var pisoid = "<?php echo $pisoid; ?>" ;
$.ajax({
url: 'process.php',
type: 'POST', // Send post data
data: 'type=fetch',
async: false,
success: function(s){
json_events = s;
}
});
Also i use this:
$('#calendar').fullCalendar({
events: JSON.parse(json_events),
//events: [{"id":"14","title":"New Event","start":"2015-01-24T16:00:00+04:00","allDay":false}],
utc: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true,
slotDuration: '00:30:00',
eventReceive: function(event){
var title = event.title;
var start = event.start.format("YYYY-MM-DD[T]HH:mm:SS");
$.ajax({
url: 'process.php',
data: 'type=new&title='+title+'&startdate='+start+'&zone='+zone+'&pisoid='+pisoid,
type: 'POST',
dataType: 'json',
success: function(response){
event.id = response.eventid;
$('#calendar').fullCalendar('updateEvent',event);
},
error: function(e){
console.log(e.responseText);
}
});
$('#calendar').fullCalendar('updateEvent',event);
console.log(event);
});
Then, on my process.php i try to make:
$space = $_POST['pisoid'];
$events = array();
$query = mysqli_query($con, "SELECT * FROM calendar where pisoid='$space'");
while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC))
{
$e = array();
$e['id'] = $fetch['id'];
$e['title'] = $fetch['title'];
$e['start'] = $fetch['startdate'];
$e['end'] = $fetch['enddate'];
$allday = ($fetch['allDay'] == "true") ? true : false;
$e['allDay'] = $allday;
array_push($events, $e);
}
echo json_encode($events);
Where i put a number instead of $space in my sql query everythings its ok, but in this $space i dont receive a proper number, So how can i make this stuff???
You're passing the data as if it was a URL in the fullcalendar function
Change this:
data: 'type=new&title='+title+'&startdate='+start+'&zone='+zone+'&pisoid='+pisoid,
To:
data: {type: 'new', title: title, startdate: start, zone: zone, pisoid: pisoid}
Like this you're passing different variables (type, title, etc...) To the superglobal you're using in the PHP file.
Explanation:
The data array is constructed as such: 'nameofattribute': variable
And JavaScript will take care of creating the URL/posting the data
Your problem is that you're trying to get the "GET" parameters of the data you're passing (without a key might I add) with the POST variable. Try switching them around, it should help

Categories