I have been using JSON objects encoded in PHP from my MySQL database in many places with no problems. I am now trying to create one from a resultset using COUNT(column) in the syntax and I think it is causing a the problem but I have no idea how to fix it.
My JS looks like this...
createTeacherStatsTab = function(){
$('#main').append('<div id="teacherStatsTab></div>"');
$.getJSON("php/countMarkingPerTeacher.php", function(data) {
$.each(data, function(key, val) {
$('#teacherStatsTab').append(val.teacher + ' : ' + val.count(teacher));
});
});
}
And my PHP looks like this...
$result = mysql_query("
SELECT teacher, COUNT(teacher)
FROM Assessment
GROUP BY teacher
ORDER BY COUNT(teacher) DESC
");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
The data that come back looks like this...
[
{
teacher: "SDe",
COUNT(teacher): "413"
},
{
teacher: "OJe",
COUNT(teacher): "297"
},
{
teacher: "AMi",
COUNT(teacher): "257"
},
{
teacher: "ASt",
COUNT(teacher): "218"
},
{
teacher: "VJa",
COUNT(teacher): "194"
},
{
teacher: "SLa",
COUNT(teacher): "125"
},
{
teacher: "MCr",
COUNT(teacher): "99"
}
]
It feels like this is probably something to do with the GROUP BY stripping ID's or something?
Can someone please help?
Thanks!