The example PHP code would actually echo a series of JSON objects all
concatenated together, each one having two properties named file and byline.
You wouldn't get any kind of valid JSON out of this. It would look something
like this (probably without the whitespace):

{ "file": something, "byline": something }
{ "file": something, "byline": something }
{ "file": something, "byline": something }
{ "file": something, "byline": something }

json_encode looks at an array you give it and decides whether to render it
as a JSON array or a JSON object depending on whether it has named
properties or only numeric indices.

jfryman, if you want to return an *array* of these objects, then make an
array of them.                 Perhaps:

$array = array();
while( $row = mysql_fetch_array($result) ) {
        $array[] = array("file"=>$row['name'],"byline"=> $row['byline']);
}
echo json_encode($array);

Then, in your jQuery code, replace this:

alert( data );

with:

console.log( data );

And load your page in Firefox with Firebug.

After the data downloads, you should have an entry in the Firebug console.
Click on that and you can see the details of the array and drill down into
its elements.

-Mike

> From: Ariel Flesler
> 
> If you are indeed returning an array from PHP, then the 
> received JSON should be a js array.
> Got this online ?

> > From: jfrymann
> > Hi,
> >   I have just started using jQuery and am trying to get 
> > data back from a mysql database in json format.  I am
> > using PHP to query the database and returning it with
> > the json_encode method.  (Basically I am following
> > the example in the documentation 
> > http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype
> >
> > The problem I am having is that the example only returns one json 
> > object rather than an array of them and I'm not sure how to 
> > modify the calls to either the php encode json or how to access the 
> > data when it is returned through jquery.
> >
> > At the moment my php looks like this:
> >
> > while($row = mysql_fetch_array($result)) {
> >         echo json_encode(array("file"=>$row['name'],"byline"=>
> > $row['byline']));
> >
> > }
> >
> > and the jQuery:
> >
> > $.post("getimages.php", { location: opt_choice, curr_img: img_cnt,
> > limit: img_lim },
> >                                           function(data){
> >                                             alert(data);
> >                                           }, "json");
> >
> > How can I set it up to access multiple objects?
> > Thanks for the help!

Reply via email to