1

I am trying to get json encoded data into a json file inorder to show them in an event calendar.I am using php codeigniter framework.I just want to know whether is there any other method other than the method i have used. Because it is not working. But when i echo the json_encode data it displays.But I want to write them into a separate file calls events.json.

Thanks in advance.

model

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();
    }

Controller

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>
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
Ishani Pathinayake
  • 177
  • 1
  • 2
  • 15

1 Answers1

1

You can put your file data using file_put_contents();

<?php
    //write to json file
     $jsonEvents=json_encode($calendar_results);
     file_put_contents('events.json',$jsonEvents);
    ?>
Lal krishnan S L
  • 1,684
  • 1
  • 16
  • 32