Reputation: 11
I've created a page which is in PHP, it emulates the look of http://www.radioborders.com/on-air/station-schedule (only the table part) for a radio station site I designed on localhost. It's a test site, for me to try out CSS/PHP/MySQL development.
My page is at http localhost/radiostation1/mytestpage (.php extension hidden by mod_rewrite)
This is the code for the page: http://pastebin.com/EvK7VzMy
However, it links to the external JSON, which is not what I want.
I'm thinking of creating a PHP/MySQL array, which uses my database that has the following tables:
presenter (varchar, 255) image (varchar, 255) - not sure if that's the best way to store it showdesc (varchar, 255) airtime (time) expiration (time)
I'm not sure how to get my code to select from a callback on localhost.
The original code from the site:
// load the schedule.
$.getJSON('http://radioplayer.bauerradio.com/schedule.php?callback=?', {
name: 'Radio Borders'
}, function(json){
// loop each show and append to our giant table.
My planned version:
// load the schedule.
$.getJSON('http://localhost/myradiotest1/schedule.php?callback=?', {
name: 'Radio1FM'
}, function(json){
// loop each show and append to our giant table.
(I do have the schedule.php page but it's just blank at the moment until i figure out what to do with it)
I am new to JSON and figured out that this would be a bit more dynamic (in script terms) than my previous usage of extracting data from MySQL via PHP as I've learnt about, which I can do fairly well.
What would I need to do to get a JSON callback to work and how is the best way to go about this? Any advice is much appreciated, and I'll try it out, see what happens.
In copyright terms, my usage of an existing JSON script, this is fair use, since it's only for my educational use, and not commercially distributed.
Upvotes: 1
Views: 256
Reputation: 95364
Build your array using PHP arrays then use json_encode()
to encode your data into a JSON array.
$dataArray = array('success' => true, 'error' => '', 'data' => getDBArray());
echo json_encode($dataArray) . ';' ;
Now, supporting JSONP at this point is trivial. You just need to modify the code as such:
$json = json_encode($dataArray);
// Check if $_GET['callback'] contains a valid JavaScript function name
if(preg_match('/^[$A-Za-z_][0-9A-Za-z_]*$/', $_GET['callback'])) {
echo $_GET['callback'] . '(' . $json . ');' ;
} else {
echo $json . ';' ;
}
Upvotes: 1