2011-10-13 4 views
0

내가 시험 결과에 대한 분석을 제공 내 웹 사이트에 대한 모듈을 쓰고 있어요에게 그룹화하여 배열을 통해 반복, 나는 someout이 유사 넣어 얻을일치하는 값

테스트 제목
평균 마크가 필요합니다 85 %

테스트 제목이
평균 마크 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로 가능합니까?

+0

더 똑똑한 querry로 할 수 있습니다. –

답변

0

이것은 데이터베이스를 통해해야 할 일입니다.

SELECT AVG(mark) FROM test_results GROUP BY tests_test_id 
0

새 배열을 만든 다음 루프를 반복하면서 추가할까요?

$sumArray = array(); 

foreach ($myArray as $key=>$val) { 
    $sumArray[$key]+=$val; 
} 

print_r($sumArray['test_id']); 
관련 문제