Assigning column names in JSON server side code

Assigning column names in JSON server side code

gordyrgordyr Posts: 35Questions: 0Answers: 0
edited December 2011 in DataTables 1.8
I have come accress a need to have the JSON ouptut that that datatables spits out to have uniquely named column identifiers output along with the data. e.g. The standard server side script produces output like this:-

[code]
{
"sEcho":1,
"total":"1710",
"aaData":[
[
"Help",
"http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76fab1e95bd.mp3?King_of_Spain_Entropy_02_Animals_Part_1.mp3",
"1784",
"3",
0,
null,
"0000-00-00 00:00:00"
],
[
"A Day In The Life",
"http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76f5fc253a1.mp3?JenWood_Zeppelin.mp3",
"3573",
"3",
0,
null,
"0000-00-00 00:00:00"
]
}
[/code]

I need to give each column a name so that it outputs the following;-

[code]
{
"sEcho":1,
"total":"1710",
"aaData":[
[
name: "Help",
link: "http:\/\/www.mysite.com\/wp-content\/uploads\/2011\/09\/dt_intfc4e732d1f1276d_4e76fab1e95bd.mp3?King_of_Spain_Entropy_02_Animals_Part_1.mp3",
songid: "1784",
artistid: "3",
rating: 0,
favourite: null,
time: "0000-00-00 00:00:00"
]
}
[/code]

The JSON encoding part of my server side code is as follows:-

[code]
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Have a look at this blog post: http://datatables.net/blog/Extended_data_source_options_with_DataTables . It shows how you can use mDataProp to use a JSON object rather than an array.

    Allan
  • gordyrgordyr Posts: 35Questions: 0Answers: 0
    I really must get better at using your search feature.... Thanks as allways allan. Great support for a geat product. :-)
This discussion has been closed.