helloxielan 2015-05-03 13:31 采纳率: 0%
浏览 89

如何将存储在数据库中的事件显示在日历中?

如何将存储在数据库中的事件显示在日历中?我不知道如何用自己的日期替换随机日期.....是不是必须遍历数据库获得的数据?并把数据修改为JSON格式?我可以用前轮吗?

这是zabuto日历下载网站提供的代码:

<?php include_once 'includes/sessions.php'; ?>
<?php include_once 'includes/connect.php';
/**
 * Example of JSON data for calendar
*
* @package zabuto_calendar
*/


if (!empty($_REQUEST['year']) && !empty($_REQUEST['month'])) {
$year = intval($_REQUEST['year']);
$month = intval($_REQUEST['month']);
$lastday = intval(strftime('%d', mktime(0, 0, 0, ($month == 12 ? 1 : $month + 1), 0, ($month == 12 ? $year + 1 : $year))));


$dates = array();
for ($i = 0; $i <= (rand(4, 10)); $i++) {
    $date = $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-' . str_pad(rand(1, $lastday), 2, '0', STR_PAD_LEFT);

$dates[$i] = array(
        'date' => $date,
        'badge' => ($i & 1) ? true : false,
        'title' => 'Example for ' . $date,
        'body' => '<p class="lead">Information for this date</p><p>You can add <strong>html</strong> in this block</p>',
        'footer' => 'Extra information',
    );

    if (!empty($_REQUEST['grade'])) {
        $dates[$i]['badge'] = false;
        $dates[$i]['classname'] = 'grade-' . rand(1, 4);
    }

    if (!empty($_REQUEST['action'])) {
        $dates[$i]['title'] = 'Action for ' . $date;
        $dates[$i]['body'] = '<p>The footer of this modal window consists of two buttons. One button to close the modal window without further action.</p>';
        $dates[$i]['body'] .= '<p>The other button [Go ahead!] fires myFunction(). The content for the footer was obtained with the AJAX request.</p>';
        $dates[$i]['body'] .= '<p>The ID needed for the function can be retrieved with jQuery: <code>dateId = $(this).closest(\'.modal\').attr(\'dateId\');</code></p>';
        $dates[$i]['body'] .= '<p>The second argument is true in this case, so the function can handle closing the modal window: <code>myFunction(dateId, true);</code></p>';
        $dates[$i]['footer'] = '
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" onclick="dateId = $(this).closest(\'.modal\').attr(\'dateId\'); myDateFunction(dateId, true);">Go ahead!</button>
        ';
    }
}

echo json_encode($dates);

} else {
echo json_encode(array());
}
  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 19:11
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要将存储在数据库中的事件显示在日历中,你可以使用AJAX来动态更新日历。以下是一个简单的示例,展示了如何使用PHP和jQuery实现这个功能:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        <style>
            .event-container {
                border: 1px solid #ccc;
                padding: 10px;
                margin-bottom: 10px;
            }
        </style>
    </head>
    <body>
    
        <!-- Your existing HTML structure -->
        
        <div id="calendar" class="container-fluid">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Title</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody id="events-body"></tbody>
            </table>
        </div>
    
        <script>
            // Initialize your PHP code here
    
            $(document).ready(function() {
                // Replace this with your actual database query to get events
                $.getJSON('your_database_query_here', function(data) {
                    var eventContainer = $('#events-body');
    
                    $.each(data.events, function(index, event) {
                        var eventRow = $('<tr>');
                        
                        var dateCell = $('<td>').text(event.date);
                        eventRow.append(dateCell);
    
                        var titleCell = $('<td>').text(event.title);
                        eventRow.append(titleCell);
    
                        var actionsCell = $('<td>');
    
                        var deleteButton = $('<button>')
                            .addClass('btn btn-danger')
                            .text('Delete');
                        actionsCell.append(deleteButton);
    
                        eventRow.append(actionsCell);
    
                        eventContainer.append(eventRow);
                    });
                });
    
                // Update the calendar with new events
                setInterval(updateCalendar, 1000); // Update every second
            });
    
            function updateCalendar() {
                $.getJSON('your_database_query_here', function(data) {
                    // Clear previous events from the calendar
                    $('#events-body tr').remove();
    
                    // Add new events to the calendar
                    $.each(data.events, function(index, event) {
                        var eventRow = $('<tr>');
                        
                        var dateCell = $('<td>').text(event.date);
                        eventRow.append(dateCell);
    
                        var titleCell = $('<td>').text(event.title);
                        eventRow.append(titleCell);
    
                        var actionsCell = $('<td>');
    
                        var deleteButton = $('<button>')
                            .addClass('btn btn-danger')
                            .text('Delete');
                        actionsCell.append(deleteButton);
    
                        eventRow.append(actionsCell);
    
                        $('#events-body').append(eventRow);
                    });
                });
            }
        </script>
    </body>
    </html>
    

    在这个示例中,我们首先获取了数据库中的所有事件,并将其添加到一个HTML表格中。然后,我们在每个事件上都创建了一个按钮,当用户点击这些按钮时,会触发一个JavaScript函数,该函数会在后台调用你的PHP脚本来处理事件。

    注意:这只是一个基本的示例,实际应用可能需要根据你的具体需求进行调整。例如,你可能需要处理更复杂的事件(如拖拽、点击等),或者使用不同的方式来展示事件信息(如HTML或Markdown)。

    评论

报告相同问题?