我正在为我的网站编写一个模块,它给出了测试结果的细目,我需要得到一些类似的东西,
测试标题
平均分数85%测试题目2
平均马克12%
我从数据库中得到的数组如下所示,
Array
(
[0] => Array
(
[result_id] => 11
[test_taken] => 2011-10-04 16:22:59
[mark] => 5
[retaken] => false
[tests_test_id] => 4
[test_title] => Website Development CSS Basics
[test_slug] => website-development-css-basics
[mark_needed] => 90
[retake] => Yes
[topic_id] => 402
[topics_topic_id] => 402
)
[1] => Array
(
[result_id] => 12
[test_taken] => 2011-10-04 16:30:02
[mark] => 50
[retaken] => false
[tests_test_id] => 5
[test_title] => Another Test
[test_slug] => another-test
[mark_needed] => 10
[retake] => No
[topic_id] => 402
[topics_topic_id] => 402
)
)这是我目前的查询,
$this->db->select('results.result_id, results.test_taken, results.mark, results.retaken, results.tests_test_id, tests.test_title, tests.test_slug, tests.mark_needed, tests.retake, topics.topic_id, tests.topics_topic_id')
->from('results')
->join('tests', 'tests.test_id = results.tests_test_id', 'left')
->join('topics', 'topics.topic_id = tests.topics_topic_id', 'left');
$query = $this->db->get();
return $query->result_array();我需要以某种方式将所有的结果分组--有相同的测试id --这样我就可以使用标记键得到结果的平均值。
这在PHP中是可能的吗?
发布于 2011-10-13 19:38:43
这就是你应该让数据库为你做的事情。与…有关的东西:
SELECT AVG(mark) FROM test_results GROUP BY tests_test_id发布于 2011-10-13 19:41:09
创建一个新的数组,然后循环-添加吗?
$sumArray = array();
foreach ($myArray as $key=>$val) {
$sumArray[$key]+=$val;
}
print_r($sumArray['test_id']);https://stackoverflow.com/questions/7759413
复制相似问题