Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Looking for a way to setup a server-side datatable using PHP to parse XML json?

Okay, I'm getting the data from wufoo and so I am also able to pull json. How can I get started with the following data?

{"Entries":[{"EntryId":"33","Field71":"","Field41":"John","Field42":"Smith","Field55":"","Field34":"2004","Field375":"Arts, Design, Entertainment, Sports and Media","Field378":"Select One","Field4":"Kayak Outfitter","Field3":"Kayak Tours, Inc","Field7":"123 Main Street","Field8":"","Field9":"New York","Field10":"New York","Field11":"54209","Field12":"United States","Field19":"(555)555-5555","Field23":"[email protected]","Field46":"http:\/\/www.website.com","Field21":"","Field49":"","Field6":"May>September","Field65":"","Field69":"","Field25":"","Field37":"Its all about Self-Motivation.","Field30":"Yes","Field31":"Yes","Field172":"","Field39":"","DateCreated":"2009-01-30 05:46:02","CreatedBy":"public","DateUpdated":"2010-08-08 22:23:30","UpdatedBy":"User"}]}
share|improve this question
 
Can you show us what you have so far, please? –  Charles Mar 18 '11 at 0:07
 
Just the code on this page... datatables.net/examples/server_side/server_side.html –  Jeffrey Mar 18 '11 at 1:00
 
So it looks like that particular plugin can only work with either HTML tables or json data. Your best bet is likely going to be transforming your XML into either an HTML table or into json. Is this where you're getting stuck? –  Charles Mar 18 '11 at 1:39
 
Okay, I provided an example of json data... can you give me an example of how to get started? –  Jeffrey Mar 18 '11 at 18:28
add comment

2 Answers

As Charles suggests DataTables will currently only accept a JSON input with a specific format. The reason for this is that supporting abstract formats would add a lot of overhead to both the internals and the initialisation (i.e. you'd need to tell it that you want it to use //articles/book@author or whatever).

So one option is to use fnServerData ( http://datatables.net/usage/callbacks#fnServerData ) to make your own Ajax call and get the XML - than transform it into the JSON format that DataTables needs with a simple loop.

Allan

share|improve this answer
 
json data is now above in the question. how do I get started? –  Jeffrey Mar 18 '11 at 18:29
add comment

Thanks for the sample data.

You're going to need to convert the data slightly.

DataTables can take a Javascript data structure (JSON), but it has to be an array of arrays.

Your sample data has a root element called Entries, which contains an array. That's great. Unfortunately each element in that array is currently a hash -- a key/value pair.

You need only the values from that pair, not the keys.

This Javascript will convert your Entries array-of-hashes into a plain old array-of-arrays. I'm using the Javascript 1.6 for each ... in syntax here because I had a brainfart and didn't remember that we're talking about a jQuery plugin here, and wrote it without that dependency.

var entries = /* JSON as provided in question */;
var new_array = new Array();
var el = entries['Entries'].length;
for(i = 0; i < el; i++) {
    var inner_array = new Array();
    for each (var value in entries['Entries'][i]) {
        inner_array[ inner_array.length ] = value;
    }
    new_array[ new_array.length ] = inner_array;
}

You can then pass new_array into the initial options hash's aaData option, as documented in the link I provided above. You will need to work out how to present the column headings yourself, given that you seem to have fallen into the anti-pattern of useless key names.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.