Reputation:
I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I am displaying classes accordingly to their majors. I have two majors: Computer Engineer and Information System. Their values are stored in a table named major. Their respective ID is 1 and 2. I am having difficulties totaling the hours of all course. How can I add each course hours and show the total hours for the major at the bottom of the page?
<?
try {
$pdo = new PDO ("mysql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$query = $pdo->prepare("SELECT course.name, course.code, course.description, course.hours,
CASE semester.semester
WHEN 0 THEN 'Spring'
WHEN 1 THEN 'Fall'
WHEN 2 THEN 'All-year'
END semester, semester.year
FROM course
LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
LEFT JOIN major ON major.id = major_course_xref.major_id
LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
LEFT JOIN semester ON course_semester_xref.semester_id = semester.id
Where major.id = '1'
");
$query->execute();
if ($query->execute()){
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
print "<b>" . $row['name'] . "</b><br>";
print $row['code'] . "<br>";
print $row ['description'] . "<br>";
print $row['hours'] . " hrs. <br>";
print $row['semester'] . "<br>";
print $row['year'] . "<br>";
print "------------------------------<br />";
}
}
else
echo 'Could not fetch results.';
unset($pdo);
unset($query);
?>
Current Output
Computer Programming I
CPSC1400
Introduction to disciplined, object-oriented program development.
4 hrs.
Spring
2013
------------------------------
Computer Programming II
CPSC1500
This course builds upon the topics covered in Computer Science I and provides experience developing complex applications.
4 hrs.
Fall
2013
------------------------------
Database Programming
CPSC2100
Study of relational database management systems and information storage and retrieval techniques.
4 hrs.
Spring
2014
Upvotes: 0
Views: 108
Reputation: 3850
You could add a variable eg $tothours
then in the while
clause add $row['hours']
to it.
Might be better than another query/join in the sql statement.
Example
$tothours = 0;
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
print "<b>" . $row['name'] . "</b><br>";
print $row['code'] . "<br>";
print $row ['description'] . "<br>";
print $row['hours'] . " hrs. <br>";
print $row['semester'] . "<br>";
print $row['year'] . "<br>";
print "------------------------------<br />";
$tothours += $row['hours']; <-- this line
}
print "<p>Total hours for Major: " . $tothours . ".</p>"
Sorry if my syntax is incorrect.
Upvotes: 0
Reputation: 74018
This query shows the sum of your course hours grouped by major:
select major.id, sum(course.hours)
from course, major, major_course_xref
where course.id = major_course_xref.course_id
and major.id = major_course_xref.major_id
group by major.id
Upvotes: 1