views:

48

answers:

1

I'm creating a CMS for my client to work with his photographs and to sell them on his site. For the CMS end as well as the front end, which both will be all AJAX, it'd be nice to get a JSON feed setup so that I can just use the same feed to generate new "pages" and "views" with JS.

So this example feed would have like {[name:'A Photo',description:'lorem ipsum...'],[...]} and then with jQuery or JS i can create a table of all his photographs, pages, etc. How can I set this up for myself?

Should I just create a PHP file that gets all the data from the MongoDB put's it in an array than convert the array to JSON?

A: 
    $cursor = $this->collection->find($params);
    $return = array();
    $i=0;
    while( $cursor->hasNext() )
    {

        $return[$i] = $cursor->getNext();
        $return[$i++]['_id'] = $cursor->key;
    }
    return json_encode($return);

That is how I return JSON frrom Mongo.

Gazler
Awesome, i'll have to check this when I get home. Seems like it would work. And also, thanks for letting me know that there is a hasNext() function. Still learning the MongoDB PHP driver :)
Oscar Godson
This seems to work fairly well, but why is it missing some stuff like the _id's and i have a "file" item in my collection with an _id that I stored from GridFS, but that is also not showing up?
Oscar Godson
Untested, but my additions should work.
Gazler
Notice: Undefined property: MongoCursor::$key in /var/www/extextures.com/dashboard/functions.php on line 51 -- Is what i get?
Oscar Godson
Also, my JSON is returning _id:null? Odd...
Oscar Godson