在MySQL COUNT()创建的JSON对象上使用$ .each

时间:2012-11-10 12:55:40

标签: php javascript jquery mysql json

我一直在许多地方使用我的MySQL数据库中用PHP编码的JSON对象,没有任何问题。我现在尝试使用语法中的COUNT(列)从结果集创建一个,我认为它导致了问题,但我不知道如何解决它。

我的JS看起来像这样......

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));
        });
    });
}

我的PHP看起来像这样......

$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);

回来的数据看起来像这样......

[
{
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"
}
]

感觉这可能与GROUP BY剥离ID有关吗?

有人可以帮忙吗?

谢谢!

4 个答案:

答案 0 :(得分:3)

我认为该对象不允许在其属性上使用括号,因此当您调用val.count(teacher)时,您试图调用count对象的不存在的val方法。请尝试改为调用val["count(teacher)"],或只更改属性名称。

答案 1 :(得分:2)

总的来说,问题是json_encode似乎返回了无效的JSON。您的对象键包含(),它们是对象键的无效字符(除非它们包含在字符串中,如下例所示)。

[{
    "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"
}]

最简单的解决方法是为您的COUNT(teacher)列添加别名:

$result = mysql_query("
SELECT teacher, COUNT(teacher) AS count
FROM Assessment
GROUP BY teacher
ORDER BY COUNT(teacher) DESC
");

这应该将您的JSON渲染为

[{
    teacher: "SDe",
    count: "413"
}, {
    teacher: "OJe",
    count: "297"
}, {
    teacher: "AMi",
    count: "257"
}, {
    teacher: "ASt",
    count: "218"
}, {
    teacher: "VJa",
    count: "194"
}, {
    teacher: "SLa",
    count: "125"
}, {
    teacher: "MCr",
    count: "99"
}]

这是有效的JSON(无需对密钥进行字符串验证),您的现有函数应该可以正常工作(将val.count(teacher)更改为val.count后):

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);
        });
    });
}

答案 2 :(得分:1)

这是因为JS因为括号而将您的属性访问解释为函数调用:

val.count(teacher)

我想最简单的解决方案是给你的COUNT()结果一个别名:

$result = mysql_query("
SELECT 
  teacher, 
  COUNT(teacher) AS teacher_count
FROM Assessment
GROUP BY teacher
ORDER BY COUNT(teacher) DESC
");

并访问它:

$('#teacherStatsTab').append(val.teacher + ' : ' + val.teacher_count);

答案 3 :(得分:-2)

你正在获取json的数组。所以你必须获得第0个元素而不必迭代它所以你必须这样做

$.getJSON("php/countMarkingPerTeacher.php", function(data) {
    var myData = data[0];
    $.each(myData, function(key, val) {
        $('#teacherStatsTab').append(val.teacher + ' : ' + val.count(teacher));
    });
});

如果您的json采用的格式比您的代码有效

{
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"
}