0

This is my PHP/JSON script from localhost:

http://www16.zippyshare.com/v/6486125/file.html is the link if you need to download the PHP files to edit them in your answers if you want. (The link to the JSON file is mentioned in large-schedule.js in the file. Instructions on usage provided).

It partially works (as in the file echoes the data).

This is the code:

<?
header('Content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
$link = mysql_pconnect("localhost", "test", "test") or die("Could not connect");
mysql_select_db("radiostations") or die("Could not select database");

$arr = array();

$rs = mysql_query("SELECT * FROM radio1r1");

while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo '{"success":true,"error":"","data":{"schedule":['.json_encode($arr).'}';
echo isset($_GET['callback'])
 ? "{$_GET['callback']}($json)"
 : $json;

However, I cannot get the contents of the fields startminutes and endminutes (stored as DATETIME) to display as 01/02/2013 00:00:00 within the JSON, in order to display them as 01/02/\2013 00:00:00

The fields I have are in the SQL file above.

As a PHP/JSON file the code works at a basic level; I can do callbacks well, but is there an easier way to get success true error data to display without manually putting it in?

As for the query string callback, I intend to do it so it has these 4 stations with different results from the MySQL tables:

Radio 1

Anytown FM

Big City FM

so the callback would look like http://127.0.0.1/phpradiostation/radioschedule-json.php?callback=?&name=Anytown+FM

or

http://127.0.0.1/phpradiostation/radioschedule-json.php?callback=?&name=Big+City+FM

I have got it halfway there, with regard to the JSON but it displays a blank page despite there being data in the database!

PHP info: I'm using 5.4.1.0, on MAMP, OS X Mavericks, if that's relevant.

Basically, what I am asking is for help on actually getting it to display the data in the javascript.

Any help is appreciated!

avenas8808
  • 1,639
  • 3
  • 20
  • 33
  • tl;dr - try being less verbose – ddoor Dec 15 '13 at 14:02
  • [Please, don't use `mysql_*` functions in new code](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](http://php.net/mysql_connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. [Here is a good PDO tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – vascowhite Dec 15 '13 at 14:04
  • not sure whay you are sending jsonp on same domain and eneabling CORS. Why json_enclode part of output, but manually create the rest? `sql` isn't even looking for the variables sent in `GET`.... you have a lot of different issues here. WHat does `blank page` mean? You really should break this down into smaller more specific posts since you have so many problems – charlietfl Dec 15 '13 at 14:12
  • nobody will download your files....no point in posting download links – charlietfl Dec 15 '13 at 14:13

1 Answers1

0

As far as I can understand,

1st thing I notice is you are using json_encode in a wrong way. What you have to do is create a multi dimentional array and use json_encode to convert the particular array to JSON rather than manually doing it.

Answer to your question is yes JSON content should be escaped when they are passed. That why it shows as 01/02/\2013 00:00:00. What you have to do is decode the JSON data at the client-side. See the below two links.

How to JSON decode array elements in JavaScript?

Parse JSON in JavaScript?

Also use jsonlint to validate your JSON data.

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243