Jump to content

Incorrect Array output.


Go to solution Solved by SalientAnimal,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/285794-incorrect-array-output/
Share on other sites

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

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.

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.

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 download

localhost/events/lib

  • jquery.min.js
  • jquery-ui.custom.min.js
  • moment.min.js

 

localhost/events/cupertino

  • jquery-ui.min.css

Also is the a possibility that there is a conflict with the files in my hdocs/js directory?

 

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.

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 by SalientAnimal

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 by mac_gyver
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.