Reputation: 328
I am making a booking system in php using codeigniter
. Now I want to populate the events in full calender depending on the value I am fetching from the database.
My code :
<script src='<?php echo base_url();?>assets/fullcalendar/fullcalendar.min.js'></script>
<?php
$var=0;
foreach($query->result() as $row) {
$var++;
$booking_date = $row->booking_date;
$booking_start_time = $row->booking_start_time;
}
?>
<script>
$(document).ready(function() {
// page is now ready, initialize the calendar...
var booking_date = '<?php echo $booking_date; ?>';
var booking_start_time = '<?php echo $booking_start_time; ?>';
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
events: [{
title: 'House Cleaning',
start: booking_date + 'T' + booking_start_time,
allDay: false
}]
});
});
</script>
Using above I am able get the last record populated on the full calender. I want to populate each record fetched from the database to the full calender js
. How do I go about it.
Thanks in advance and apologies if I am being vague but I am always ready to explain further based on the question.
Thanks,
Upvotes: 2
Views: 1593
Reputation: 2978
Build an array in your php and format it as json in your javascript. Pass the formatted json to the event property like this :
events: buildRecordsToJson(calendar_records)
function buildRecordsToJson(calendar_records){
var employees = [];
for(var record in calendar_records) {
var item = calendar_records[record];
employees.push({
"title" : "Case # " + item.case_number,
"record" : item.id,
"start" : item.case_due_date
});
}
return employees;
}
Edit :
Assuming this is your result
$result=mysqli_query($con,$sql);
Iterate the result sets from your database and push it to the main array.
$list = array();
while ($row = mysqli_fetch_row($result))
{
$entry = array();
$entry["booking_date"] = $row["booking_date"];
$entry["booking_start_time"] = $row["booking_start_time"];
array_push($list,$entry);
}
mysqli_free_result($result);
Return $list array to your javascript and feed it to your calendar events after you formatted it to json (might not be necessary).
Upvotes: 4