I'm trying to generate a JSON file that I can use with DataTables. After looking through these forums I guess my knowledge of javascript is too limited (honestly: non existant) in order to understand it. The thing is like this: I have a php file that generates a json file from a mysql query. It looks like this:
this generates the following (valid) json (I simplified this to 4 rows, its actually +2000 rows and growing):
From what I read in the forums and in the tutorials, the above json should look like the following, in order to work with DataTables:
now, how do I have to change my php in order to get the above format? I'm really new to programming, so my knowledge of php is pretty limited and absolutely zero for js.
I thank you very much for your answer in advance!
Edit: maybe I should also add the page where the DataTable is generated too:
<?php
require "mysql.con.php";
$query = "SELECT spel_id, locatie, speler, sum(punten)
FROM `whiteboard_games`
group by spel_id, speler";
$result = mysql_query($query);
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = array('data' => $r);
}
echo json_encode($rows);
?>
this generates the following (valid) json (I simplified this to 4 rows, its actually +2000 rows and growing):
[
{
"data": {
"spel_id": "2012-09-24 15:43:56",
"locatie": "white room",
"speler": "Arne",
"sum(punten)": "17"
}
},
{
"data": {
"spel_id": "2012-09-24 15:43:56",
"locatie": "white room",
"speler": "Bjorg",
"sum(punten)": "26"
}
},
{
"data": {
"spel_id": "2012-09-24 15:43:56",
"locatie": "white room",
"speler": "Bram",
"sum(punten)": "-11"
}
},
{
"data": {
"spel_id": "2012-09-24 15:43:56",
"locatie": "white room",
"speler": "Filip",
"sum(punten)": "-32"
}
}
]
From what I read in the forums and in the tutorials, the above json should look like the following, in order to work with DataTables:
{
"aaData": [
[
"2012-09-24 15:43:56",
"white room",
"Arne",
"17"
],
[
"2012-09-24 15:43:56",
"white room",
"Bjorg",
"26"
],
[
"2012-09-24 15:43:56",
"white room",
"Bram",
"-11"
],
[
"2012-09-24 15:43:56",
"white room",
"Filip",
"-32"
]
]
}
now, how do I have to change my php in order to get the above format? I'm really new to programming, so my knowledge of php is pretty limited and absolutely zero for js.
I thank you very much for your answer in advance!
Edit: maybe I should also add the page where the DataTable is generated too:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test DataTables</title>
<script src="jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="../datatables/media/js/jquery.dataTables.js"></script>
<link href="jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "json.php",
"bDeferRender": true
} );
} );
</script>
</head>
<body>
<div style="width:800px">
<table id="example">
<thead>
<tr>
<th>kaarting</th>
<th>locatie</th>
<th>kaarter</th>
<th>punten</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</html>