Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

php - Populating events in full calender javascript from the database

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...