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>
6
  • maybe this stack overflow post might help you. write json encoded values into a file Commented Jun 11, 2015 at 11:31
  • You could also do this inside the controller itself the process of writing it to the file. Since you would not have to mix the php and html together. Commented Jun 11, 2015 at 11:39
  • What is the return value of fwrite()? Commented Jun 11, 2015 at 12:28
  • @Harigovind R Thank you. I will refer that. Commented Jun 12, 2015 at 5:13
  • So 701 bytes were successfully written to the file. You need to specify more precisely what exactly is not working. Commented Jun 12, 2015 at 8:44

1 Answer 1

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);
    ?>
Sign up to request clarification or add additional context in comments.

3 Comments

k. are sure the file has permision to write?
Yes. I am using jquery.eventCalendar.min.js plugin. will it effect to this??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.