DataTable with JSON and MYSQL
DataTable with JSON and MYSQL
Please help
My JSON request looks like:
[code]
<?php
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("accounting", $con);
$sth = mysql_query("SELECT DATE_FORMAT(date,'%Y-%b') AS EventDate, sum(ksi2k), sum(hepspec), sum(cpu_used), sum(elapsed_ksi2k), sum(elapsed_hepspec), sum(elapsed_cpu_used), sum(jobs) FROM accounting where date like '2012%' and site like 'UA-KNU' group by year(date), month(date)");
$rows = array();
while($r = mysql_fetch_assoc($sth)){
$rows[] = $r;
}
echo json_encode($rows);
mysql_close($con);
[/code]
The JSON result looks like:
[code]
[{"EventDate":"2012","sum(ksi2k)":"54","sum(hepspec)":"216","sum(cpu_used)":"34","sum(elapsed_ksi2k)":"334","sum(elapsed_hepspec)":"1336","sum(elapsed_cpu_used)":"209","sum(jobs)":"6964"},{"EventDate":"2012","sum(ksi2k)":"71","sum(hepspec)":"284","sum(cpu_used)":"44","sum(elapsed_ksi2k)":"275","sum(elapsed_hepspec)":"1100","sum(elapsed_cpu_used)":"173","sum(jobs)":"5794"},{"EventDate":"2012","sum(ksi2k)":"29","sum(hepspec)":"116","sum(cpu_used)":"17","sum(elapsed_ksi2k)":"317","sum(elapsed_hepspec)":"1268","sum(elapsed_cpu_used)":"198","sum(jobs)":"4376"}]
[/code]
I tested it with "The JSON Validator" and it is valid:
[code]
[
{
"EventDate": "2012-Jan",
"sum(ksi2k)": "54",
"sum(hepspec)": "216",
"sum(cpu_used)": "34",
"sum(elapsed_ksi2k)": "334",
"sum(elapsed_hepspec)": "1336",
"sum(elapsed_cpu_used)": "209",
"sum(jobs)": "6964"
},
{
"EventDate": "2012-Feb",
"sum(ksi2k)": "71",
"sum(hepspec)": "284",
"sum(cpu_used)": "44",
"sum(elapsed_ksi2k)": "275",
"sum(elapsed_hepspec)": "1100",
"sum(elapsed_cpu_used)": "173",
"sum(jobs)": "5794"
},
{
"EventDate": "2012-Mar",
"sum(ksi2k)": "29",
"sum(hepspec)": "116",
"sum(cpu_used)": "17",
"sum(elapsed_ksi2k)": "317",
"sum(elapsed_hepspec)": "1268",
"sum(elapsed_cpu_used)": "198",
"sum(jobs)": "4376"
}
]
[/code]
My HTML, DataTables has most features enabled by default:
[code]
<!DOCTYPE html>
Overview Accounting
@import "data/portal.css";
@import "data/datatables/media/css/demo_table.css";
$(document).ready(function(){
$('#example').dataTable({
"sAjaxSource": "json/json_table_overview_cluster_progress_chronicles_2013.php",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "EventDate" },
{ "mData": "sum(ksi2k)" },
{ "mData": "sum(hepspec)" },
{ "mData": "sum(cpu_used)" },
{ "mData": "sum(elapsed_ksi2k)" },
{ "mData": "sum(elapsed_hepspec)" },
{ "mData": "sum(elapsed_cpu_used)" },
{ "mData": "sum(jobs)" }
]
});
})
EventDate
Norm.CPU.time kSI2K hours
Norm.CPU.time kHSPEC06 hours
Total CPU time used hours
Elapsed.time kSI2K hours
Elapsed.time kHSPEC06 hours
Total CPU elapsed time hours
Task
EventDate
Norm.CPU.time kSI2K hours
Norm.CPU.time kHSPEC06 hours
Total CPU time used hours
Elap.time kSI2K hours
Elap.time kHSPEC06 hours
Total CPU elapsed time hours
Task
[/code]
When I refresh the page with the table, the browser gives an error, then you see the table with no data. The text of the error message:
[quote]
DataTables warning (table id = 'example'): Requested unknown parameter '0' from the data source for row 0
[/quote]
I would be very grateful for the help
My JSON request looks like:
[code]
<?php
$con = mysql_connect("localhost","user","password");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("accounting", $con);
$sth = mysql_query("SELECT DATE_FORMAT(date,'%Y-%b') AS EventDate, sum(ksi2k), sum(hepspec), sum(cpu_used), sum(elapsed_ksi2k), sum(elapsed_hepspec), sum(elapsed_cpu_used), sum(jobs) FROM accounting where date like '2012%' and site like 'UA-KNU' group by year(date), month(date)");
$rows = array();
while($r = mysql_fetch_assoc($sth)){
$rows[] = $r;
}
echo json_encode($rows);
mysql_close($con);
[/code]
The JSON result looks like:
[code]
[{"EventDate":"2012","sum(ksi2k)":"54","sum(hepspec)":"216","sum(cpu_used)":"34","sum(elapsed_ksi2k)":"334","sum(elapsed_hepspec)":"1336","sum(elapsed_cpu_used)":"209","sum(jobs)":"6964"},{"EventDate":"2012","sum(ksi2k)":"71","sum(hepspec)":"284","sum(cpu_used)":"44","sum(elapsed_ksi2k)":"275","sum(elapsed_hepspec)":"1100","sum(elapsed_cpu_used)":"173","sum(jobs)":"5794"},{"EventDate":"2012","sum(ksi2k)":"29","sum(hepspec)":"116","sum(cpu_used)":"17","sum(elapsed_ksi2k)":"317","sum(elapsed_hepspec)":"1268","sum(elapsed_cpu_used)":"198","sum(jobs)":"4376"}]
[/code]
I tested it with "The JSON Validator" and it is valid:
[code]
[
{
"EventDate": "2012-Jan",
"sum(ksi2k)": "54",
"sum(hepspec)": "216",
"sum(cpu_used)": "34",
"sum(elapsed_ksi2k)": "334",
"sum(elapsed_hepspec)": "1336",
"sum(elapsed_cpu_used)": "209",
"sum(jobs)": "6964"
},
{
"EventDate": "2012-Feb",
"sum(ksi2k)": "71",
"sum(hepspec)": "284",
"sum(cpu_used)": "44",
"sum(elapsed_ksi2k)": "275",
"sum(elapsed_hepspec)": "1100",
"sum(elapsed_cpu_used)": "173",
"sum(jobs)": "5794"
},
{
"EventDate": "2012-Mar",
"sum(ksi2k)": "29",
"sum(hepspec)": "116",
"sum(cpu_used)": "17",
"sum(elapsed_ksi2k)": "317",
"sum(elapsed_hepspec)": "1268",
"sum(elapsed_cpu_used)": "198",
"sum(jobs)": "4376"
}
]
[/code]
My HTML, DataTables has most features enabled by default:
[code]
<!DOCTYPE html>
Overview Accounting
@import "data/portal.css";
@import "data/datatables/media/css/demo_table.css";
$(document).ready(function(){
$('#example').dataTable({
"sAjaxSource": "json/json_table_overview_cluster_progress_chronicles_2013.php",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "EventDate" },
{ "mData": "sum(ksi2k)" },
{ "mData": "sum(hepspec)" },
{ "mData": "sum(cpu_used)" },
{ "mData": "sum(elapsed_ksi2k)" },
{ "mData": "sum(elapsed_hepspec)" },
{ "mData": "sum(elapsed_cpu_used)" },
{ "mData": "sum(jobs)" }
]
});
})
EventDate
Norm.CPU.time kSI2K hours
Norm.CPU.time kHSPEC06 hours
Total CPU time used hours
Elapsed.time kSI2K hours
Elapsed.time kHSPEC06 hours
Total CPU elapsed time hours
Task
EventDate
Norm.CPU.time kSI2K hours
Norm.CPU.time kHSPEC06 hours
Total CPU time used hours
Elap.time kSI2K hours
Elap.time kHSPEC06 hours
Total CPU elapsed time hours
Task
[/code]
When I refresh the page with the table, the browser gives an error, then you see the table with no data. The text of the error message:
[quote]
DataTables warning (table id = 'example'): Requested unknown parameter '0' from the data source for row 0
[/quote]
I would be very grateful for the help
This discussion has been closed.
Replies
Allan
[code]
$(document).ready(function(){
$('#example').dataTable({
"sAjaxSource": "json/json_table_overview_cluster_progress_chronicles_2013.php",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "EventDate" },
{ "mData": "sum(ksi2k)" },
{ "mData": "sum(hepspec)" },
{ "mData": "sum(cpu_used)" },
{ "mData": "sum(elapsed_ksi2k)" },
{ "mData": "sum(elapsed_hepspec)" },
{ "mData": "sum(elapsed_cpu_used)" },
{ "mData": "sum(jobs)" }
]
});
})
[/code]
Allan
http://goo.gl/mtxCv
That's how I work without JSON:
http://goo.gl/tcRHm