...

View Full Version : $.getJSON parsing with PHP



Rebelj12a
07-19-2010, 12:25 AM
I want to use jquery $.getJSON to retrieve data from a php page. However there is no documentation or examples that show how to format the PHP page to send the data properly to the $.getJSON function(data). If anyone had any help with this i would appreciate it.

Fumigator
07-19-2010, 05:00 PM
Yeah that JSON-P deal seems to be a home-grown idea that is still in the embyronic stage at best. However you should be able to format the data in PHP as json though. Use PHP's json_encode() function.

http://us3.php.net/manual/en/function.json-encode.php

Rebelj12a
07-19-2010, 05:26 PM
Yeah that JSON-P deal seems to be a home-grown idea that is still in the embyronic stage at best. However you should be able to format the data in PHP as json though. Use PHP's json_encode() function.

http://us3.php.net/manual/en/function.json-encode.php

Yeah i checked that already, its not very helpful, the real issue here was getting the correct format for the page, that was readable by the jquery plugin i was using which is Aino's Galleria.

I was unsure if it would read a variable returned by the php or simply the page itself. Apparantly when formatted correctly the page outputs the JSON.

The format that worked for me was encoding each individual array with the json encode and stripping the slashes.

If anyone is wondering this is the code I ran from the $.getJSON after querying the database and getting the first row.



echo "[";

do
{

$json_items = array();


if($row[8] == 1) // if published
{
$jsonstring .= "{";
//store row into value
$image = $row[5];
// push data into items array
$json_items['image'] = $image;
//$jsonstring .= "'image':'".$image."',";

$thumb = $row[6];
$json_items['thumb'] = $thumb;
//$jsonstring .= "'thumb':'".$thumb."',";

$title = $row[3];
if($title != "Default Title") // if other than default
{
$json_items['title'] = $title;
//$jsonstring .= "'title':'".$title."',";
}
else
{
$categID = $row[2];
$resultcat = make_query($oConnection, $database, 'categName', 'tbl_category', 'categ', $categID);
$row2 = db_fetch_row($resultcat);
$json_items['title'] = $row2[0];
//$jsonstring .= "'title':'".$row2[0]."',";
}

$description = $row[4];
if($description != "Default Description") // if other than default
{
$json_items['description'] = $description;
//$jsonstring .= "'description':'".$description."',";
}


$link = $row[5];
$json_items['link'] = $link;
//$jsonstring .= "'link':'".$link."'";

//array_push($json_array, $json_items);
//$json_array = array($json_items);
print_r(stripslashes(json_encode($json_items)));



//echo ",\n";
//$jsonstring .= "}";
}

$row = db_fetch_row($results);
if(!$row)
{

}
else
{
if($row[8] == 1) // if published
{
echo ",\n";

//$jsonstring .= ",";
}
}
}
while($row);

echo "]";


Alot of the notated markup there is what did not work. It involved pushing it all into a string, then sending it through the json_encode(), which showed up in the javascript alert() back on the calling page, however it did not work.

the proper alert() markup (the one that worked right) should look like this in your javascript alert


[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

And just another note for $.getJSON you must place your gallery code within the function


$.getJSON(getaddress and ?query=, function(data){

$('#gallery').galleria({

data_type: 'json',
data_source: data

});

});


It has to be like that or it will not read the (data), and i have been unsuccessful in saving that (data) to a global variable.

If anyone had suggestions for this that would be great, since I was hoping to be able to change that variable based on different calls to the database.

Hope that helps some.

Fumigator
07-19-2010, 05:43 PM
That's rather bizzare, that you're using print_r() to produce output for your jQuery. You should be able to build your multi-dimensional array in PHP and simply "echo json_encode();".

Rebelj12a
07-21-2010, 11:30 PM
$.getJSON(getaddress and ?query=, function(data){

$('#gallery').galleria({

data_type: 'json',
data_source: data

});

});


It has to be like that or it will not read the (data), and i have been unsuccessful in saving that (data) to a global variable.

If anyone had suggestions for this that would be great, since I was hoping to be able to change that variable based on different calls to the database.

Hope that helps some.

Update, you can pass the variable if you place a function in the code



//namespacing - very helpful
var GA = {
galL: function(){
$.getJSON(getaddress and ?query=, function(data){

GA.galInit(data);

});
},
galInit: function(galdata){

$('#gallery').galleria({

data_type: 'json',
data_source: data

});

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum