2012-11-10 81 views
2

我已經使用在許多地方沒有問題,PHP從我的MySQL數據庫編碼的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的什麼?

有人可以幫忙嗎?

謝謝!

回答

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); 
+0

這已經修復了它,我知道它將成爲SQL語法中的一些東西,但它是我最薄弱的地方。我也意識到,這可能是COUNT()中的括號引起的一個問題,但我多次轉了一圈,並且我不能看到這一切。謝謝。 – Crin

-2

你得到的json.so你必須讓零個元素,比你有那麼你有這樣

​​

做,如果你的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" 
} 
+0

val.count在這裏不是一個函數 - 最終只會出錯。除此之外,數據已經是一個數組了 - 像數據[0]做的事情就會抓住第一個元素,這個元素將會是{teacher:「SDe」,COUNT(teacher):「413」) – Stephen

3

我認爲對象不允許其性質括號,所以當你打電話val.count(teacher)你試圖調用val對象的inexisting count方法。嘗試撥打val["count(teacher)"],或者只更改屬性名稱。

+0

這是值得的提到,如果查詢稍微改變爲'SELECT老師,COUNT(老師)爲teacherCount',那麼整個問題就會消失:) [編輯 - 哎呀..你確實..對不起] – Stephen

+0

這很有道理,但我有接受了另一個答案,因爲它對於如何解決它稍微清楚一些,就像@Stephen上面的評論一樣。感謝您的幫助! :) – Crin

2

總的來說,問題是這樣t 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(而不必stringificate鍵)和您現有的功能應該正常工作(在將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); 
     }); 
    }); 
} 
+0

這太棒了,謝謝。我已經接受了另一個答案,但這個問題已經很清楚地解釋了感謝您的時間。 – Crin

+0

很高興幫助。 :) – pete