5

I have a project i am working on that requires sending information from database through a php script using json feed to javascript. Here are the scripts:

This is the javascript:

<link rel= 'stylesheet' type='text/css' href='fullcalendar/fullcalendar/fullcalendar.css' />
<link rel="stylesheet" media="print" href="fullcalendar/fullcalendar/fullcalendar.print.css" />
<script type="text/javascript" src="fullcalendar/lib/jquery.min.js"></script>
<script type='text/javascript' src="fullcalendar/fullcalendar/fullcalendar.js"></script>
<script type="text/javascript" src="fullcalendar/lib/jquery-ui.custom.min.js" ></script>
<script>
$(document).ready(function() {
    $('#calendar').fullCalendar({
        header: {

                left: 'prev,next today',

                center: 'title',

                right: 'month,basicWeek,basicDay'

            },
            editable: true,

            events: "public_calendar.php"
    })
});
</script>
</head>

<body>

  <div id='calendar'></div>
</body>
</html>



 ?php require_once("includes/initialize.php"); ?>
<?php require_once(LIB_PATH.DS.'database.php'); ?>

<?php
    //Find all the events
    $events = Event::find_all();
         foreach($events as $event):

            $id = (int) $event->id;
            $title = "{$event->event_title}";
            $start = "{$event->start_date}" ." ". "{$event->start_time}";
            $end = "{$event->end_date}" ." ". "{$event->end_time}";
            $url = "event_detail.php";

            echo json_encode( array(
                'id'    => $id,
                'title' => "{$title}",
                'start' => "{$start}",
                'end'   => "{$end}",
                'url'   => "{$url}"
            ));         
        endforeach;

?>

This is how the php script should look like:

[ {"id":111,"title":"Event1","start":"2013-10-10","url":"http:\/\/yahoo.com\/"},
  {"id":222,"title":"Event2","start":"2013-10-20","end":"2013-10-22","url":"http:\/\/yahoo.com\/"}
]

This is how it looks like now:

{"id":12,"title":"Matriculation","start":"2013-11-5 08:00","end":"2013-11-5 17:00","url":"event_detail.php"}
{"id":13,"title":"Exam","start":"2013-11-30 09:00","end":"2013-11-30 16:00","url":"event_detail.php"}
{"id":2,"title":"Convocation","start":"2013-12-11 08:00","end":"2013-12-11 19:00","url":"event_detail.php"}

Thanks for your help in advance.

1
  • 1
    Do not output each array as a json String. Push all Arrays inside a main array and json_encode it. Commented Oct 23, 2013 at 23:37

1 Answer 1

3

The best way to achieve the result you want is to create an array in PHP, then use json_encode() to create the output. You're already doing some of this - you just need a little more:

<?php
//Find all the events
$events = Event::find_all();
$eventList = array();            // Assemble list of all events here

     foreach($events as $event):

       $eventList[] = array(              // Add our event as the next element in the event list
            'id'    => (int) $event->id,
            'title' => $event->event_title,
            'start' => $event->start_date." ".$event->start_time,
            'end'   => $event->end_date." ".$event->end_time,
            'url'   => "event_detail.php"
        );         
    endforeach;

    echo json_encode($eventList);       // encode and output the whole list.
?>

I've also simplified and shortened some of your code to remove the unnecessary double quotes.

Sign up to request clarification or add additional context in comments.

Comments

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.