繁体   English   中英

如何将JSON编码的数据写入json文件?

[英]How to write json encoded data into a json file?

我正在尝试将json编码的数据放入json文件中以便在事件日历中显示它们。我正在使用php codeigniter框架。我只想知道除我使用的方法外是否还有其他方法。 因为它不起作用。 但是当我回显它所显示的json_encode数据时,但我想将它们写入一个单独的events.json文件中。

提前致谢。

模型

function get_all_reservations_for_calendar(){
        $this->db->select('reservations.date,reservations.time,reservations.hall');
        $this->db->from('reservations');
        $this->db->where('is_deleted', '0');
        $query = $this->db->get();
        return $query->result();
    }

调节器

function index() {
        $reservation_service = new Reservation_service();
        $output['calendar_results'] = $reservation_service->get_all_reservations_for_calendar();
        $partials = array('content' => 'dashboard/dashboard_view');
        $this->template->load('template/main_template', $partials, $output);
    }

calendar_view

<head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <style type="text/css">
            #event_cal{
                width: 500px;
                height: 500px;
            }
        </style>

        <link rel="stylesheet" href="http://localhost/Calendar/backend_resources/css/eventCalendar.css">
        <link rel="stylesheet" href="http://localhost/Calendar/backend_resources/css/eventCalendar_theme_responsive.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
        <script src="http://localhost/Calendar/backend_resources/js/jquery.eventCalendar.min.js" type="text/javascript"></script>

        <?php //echo json_encode($calendar_results); ?>
        <?php
        //write to json file
        $fp = fopen('events.json', 'w');
        fwrite($fp, json_encode($calendar_results));
        fclose($fp);
        ?>

        <script type="text/javascript">
            $(function() {
                $("#event_cal").eventCalendar({
                    eventsjson: 'http://localhost/Calendar/backend_resources/json/events.json',
                    dateFormat: 'dddd MM-D-YYYY'
                });
            });
        </script>
    </head>
    <body>
        <div id="event_cal" ></div>
    </body>
</div>

您可以使用file_put_contents()放置文件数据

<?php
    //write to json file
     $jsonEvents=json_encode($calendar_results);
     file_put_contents('events.json',$jsonEvents);
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM