please i have problem at hand,
I have the following tables in my database MySQL
students
studentID | fname | mname | lname |
1 | Wilson | Dale | Micheal|
examtype
examtypeID | name |
1 | ca 1
2 | ca 2
3 | ca 3
4 | exam
first_term_result
student_id | examtype_id | subject_id | mark
1 | 1 | 7 | 12
1 | 2 | 7 | 10
1 | 3 | 7 | 9
1 | 4 | 7 | 45
currently i have this SQL statement
SELECT CONCAT(students.fname,' ',students.mname,' ',students.lname)
AS student_name, students.studentID, exam_type.name, first_term_result.mark
FROM students, exam_type, first_term_result
WHERE students.studentID=first_term_result.student_id
AND exam_type.exam_typeID=first_term_result.exam_type_id
AND first_term_result.subject_id=7
it works as i get a table where i can use to get an array like this
How i got it to that array using codeigniter framework :
$sheet = $this->teacher_model->spreadsheet();
$data = array();
foreach($sheet as $row)
{
$data[$row['student_name']]['studentID']=$row['studentID'];
$data[$row['student_name']][$row['name']]=$row['mark'];
}
Array
(
[Wilson Dale Micheal] => Array
(
[studentID] => 1
[CA 1] => 12
[CA 2] => 10
[CA 3] => 9
[Exam] => 45
)
)
another SQL statement as follows:
SELECT CONCAT(students.fname,' ',students.mname,' ',students.lname)
AS students_name, students.studentID, SUM(first_term_result.mark) as Total
FROM students, first_term_result
WHERE students.studentID=first_term_result.student_id
AND first_term_result.subject_id=7 group by students_name
gives something like this:
Array
(
[Wilson Dale Micheal] => Array
(
[studentID] => 1
[Total] => 76
)
)
Merging the two array i get my result, which is:
this is how i merged merged the two, the function array_merge() didnt work so i used:
$data = array();
foreach($sheet as $row)
{
$data[$row['student_name']]['studentID']=$row['studentID'];
$data[$row['student_name']][$row['name']]=$row['mark'];
}
foreach($total as $tol)
{
$data[$tol['student_name']]['Total']=$tol["Total"];
}
Array
(
[Wilson Dale Micheal] => Array
(
[studentID] => 1
[CA 1] => 12
[CA 2] => 10
[CA 3] => 9
[Exam] => 45
[Total] => 76
)
)
But i am wondering if i can achieve all these with a single SQL statement so that i will be able to make ORDER BY Total DESC, so the student with the highest score will come first and so on. The current statement doesnt sort the Total as DESC though i have the array just like i want to.
Thanks