SalientAnimal Posted January 30, 2014 Share Posted January 30, 2014 Hi All, How do I get my array to output: [{id:"7",title:"Test Event",agent:"Leon",start:"2014-01-01 09:00:00",end:"2014-01-01 18:00:00",url:"",allDay:"false"},{id:"8",title:"Test Event 2",agent:"Leon",start:"2014-01-02 11:00:00",end:"2014-01-02 23:00:00",url:"",allDay:"false"}] instead of [{"id":"7","title":"Test Event","agent":"Leon","start":"2014-01-01 09:00:00","end":"2014-01-01 18:00:00","url":"","allDay":"false"},{"id":"8","title":"Test Event 2","agent":"Leon","start":"2014-01-02 11:00:00","end":"2014-01-02 23:00:00","url":"","allDay":"false"}] Here is my php code: <?php include_once '../includes/db_connect.php'; include_once '../includes/functions.php'; include_once '../includes/session_management.php'; // get the records from the database if ($result = $mysqli->query("SELECT * FROM schedule ORDER BY id")); $arr = array(); while($row = mysqli_fetch_assoc($result)) { $arr[] = $row; } echo json_encode(array_values($arr)); ?> Thanks Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 30, 2014 Share Posted January 30, 2014 The json you want doesn't have quotes around the keys. This you cant prevent json_encode from doing. Where is this json being used? I guess this is for a response to an ajax request, but JavaScript is not decoding the json due to the quotes around the array keys. You can use regex to strip the quotes http://stackoverflow.com/questions/8837659/remove-double-quotes-from-a-json-encoded-string-on-the-keys?answertab=votes#tab-top Or if you're using jQuery for the ajax you can use $.parseJSON. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 I'm using the json to populate a calendar. How would I use the two examples you are suggesting? I'm looking at the suggested solutions, but not entirely sure of the implementation. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 This is what I tried from your one suggestion: $json = json_encode($arr); $json = preg_replace('/"([^"]+)"\s*:\s*/','$1:',$json); echo json_encode($json); and the output I am getting is: "[{id:\"7\",title:\"Test Event\",agent:\"Leon\",start:\"2014-01-01 09:00:00\",end:\"2014-01-01 18:00:00\",url:\"\",allDay:\"false\"},{id:\"8\",title:\"Test Event 2\",agent:\"Leon\",start:\"2014-01-02 11:00:00\",end:\"2014-01-02 23:00:00\",url:\"\",allDay:\"false\"}]" So now I have the extra slashes and extra quotes at the start and finish. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 30, 2014 Share Posted January 30, 2014 You are encoding the json again after the preg_replace. You should be echo'ing it. $json = json_encode($arr); // json encode the array $json = preg_replace('/"([^"]+)"\s*:\s*/','$1:',$json); // strip quotes from keys echo $json; // echo the json Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 Cool thanks, at least now the resut is what it should be, but its still not showing in the calendar. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 30, 2014 Share Posted January 30, 2014 What Javascript errors are being thrown up in the browsers console. And how are you passing the json to the calendar? Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 It's not throwing up any errors. on the java script side as far as I can see. If I manually put the events into calendar.php it works correctly. the json is being passed from events.php to calendar.php Here is the calendar.php code: <?php include_once '../includes/db_connect.php'; include_once '../includes/functions.php'; include_once '../includes/session_management.php'; ?> <!doctype html> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <html> <head> <link rel='stylesheet' href='cupertino/jquery-ui.min.css' /> <link href='fullcalendar/fullcalendar.css' rel='stylesheet' /> <link href='fullcalendar/fullcalendar.print.css' rel='stylesheet' media='print' /> <script src='lib/jquery.min.js'></script> <script src='lib/jquery-ui.custom.min.js'></script> <script src='fullcalendar/fullcalendar.min.js'></script> <script> $(document).ready(function() { var date = new Date(); var d = date.getDate(); var m = date.getMonth(); var y = date.getFullYear(); $('#calendar').fullCalendar({ editable: true, header: { left: 'prev,next today', center: 'title', right: 'month,agendaWeek,agendaDay' }, events: 'http://localhost/events/events.php', // Convert the allDay from string to boolean eventRender: function(event, element, view) { if (event.allDay === 'true') { event.allDay = true; } else { event.allDay = false; } }, selectable: true, selectHelper: true, select: function(start, end, allDay) { var title = prompt('Event Title:'); var url = prompt('Type Event url, if exits:'); if (title) { start = $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss"); end = $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss"); $.ajax({ url: 'add_events.php', data: 'title='+ title+'&start='+ start +'&end='+ end +'&url='+ url , type: "POST", success: function(json) { alert('Added Successfully'); location.reload(); } }); calendar.fullCalendar('renderEvent', { title: title, start: start, end: end, allDay: allDay }, true ); } calendar.fullCalendar('unselect'); }, editable: true, eventDrop: function(event, delta) { start = $.fullCalendar.formatDate(event.start, "yyyy-MM-dd HH:mm:ss"); end = $.fullCalendar.formatDate(event.end, "yyyy-MM-dd HH:mm:ss"); $.ajax({ url: 'update_events.php', data: 'title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id , type: "POST", success: function(json) { alert("Updated Successfully"); } }); }, eventResize: function(event) { start = $.fullCalendar.formatDate(event.start, "yyyy-MM-dd HH:mm:ss"); end = $.fullCalendar.formatDate(event.end, "yyyy-MM-dd HH:mm:ss"); $.ajax({ url: 'update_events.php', data: 'title='+ event.title+'&start='+ start +'&end='+ end +'&id='+ event.id , type: "POST", success: function(json) { alert("Updated Successfully"); } }); } }); }); </script> <style> body { margin-top: 40px; text-align: center; font-size: 13px; font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif; } #calendar { width: 680px; margin: 0 auto; } </style> </head> <body> <div id='calendar'></div> </body> </html> I'm only working on the top section of this code at the moment as I haven't even gotten to the add event / update event section yet. This is the fullCallendar that is available for download from many sources. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 30, 2014 Share Posted January 30, 2014 Ok did some testing and the calander loads up fine if you dont strip the quotes from the json echo json_encode($arr); // just echo the json Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 I've done this, is still not loading up. Could it have something to do with the PHP version I'm using or something else? I've been trying to so many sources on Google with possible solutions but no luck. Or could it be that I'm using mysqli instead of mysql? <?php include_once '../includes/db_connect.php'; include_once '../includes/functions.php'; include_once '../includes/session_management.php'; // get the records from the database if ($result = $mysqli->query("SELECT id, title, start, end, url, allDay FROM schedule ORDER BY id")); $arr = array(); while($row = mysqli_fetch_assoc($result)) { $arr[] = $row; } if($arr) { echo json_encode($arr); } else{ // here echo whatever you want to signify there were no results. } ?> That's my events source. Directory structure I am using is as follows: localhost/events/ events.php calendar.php localhost/events/fullcallendar all the css and js files that comes with the downloadlocalhost/events/lib jquery.min.js jquery-ui.custom.min.js moment.min.js localhost/events/cupertino jquery-ui.min.cssAlso is the a possibility that there is a conflict with the files in my hdocs/js directory? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 30, 2014 Share Posted January 30, 2014 Could it have something to do with the PHP version I'm using or something else? Or could it be that I'm using mysqli instead of mysql? I wouldn't of thought so. Just make sure in events.php it is strickly outputting the json, and not any text/html as well. To check this open up http://localhost/events/events.php and right click > view source what is displayed? There should only be the json encoded array and nothing else. Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 30, 2014 Author Share Posted January 30, 2014 (edited) Ah ok, my file source is pulling through some comments that I also have in there. Which would explain my mysqli isn't working. When I change to just a mysql connection my calendar also populated. Will remove all the comments etc tomorrow as unfortunately I'm at the end of my day and will see if that resolves the mysqli issue. Thanks again for your help. Really appreciate it. Will mark the thread as solved tomorrow once everything is confirmed. Edited January 30, 2014 by SalientAnimal Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 30, 2014 Share Posted January 30, 2014 (edited) your if(condition){ ... } construct syntax is incorrect. time to go back to the basics. edit: which is also the same problem you had in this thread - http://forums.phpfreaks.com/topic/285546-submit-form-not-processing/page-2?do=findComment&comment=1466125 Edited January 30, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Solution SalientAnimal Posted January 31, 2014 Author Solution Share Posted January 31, 2014 Ch0cu3r thanks for all your help on this, as mentioned yesterday, I went back and made the changes on my includes files. The Mysqli is now also working and populating my calendar. My json result was pulling through additional information that had been commented out in each of these files. This was why the calendar was not reading / displaying the file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.