Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So, I'm currently doing a small attendance system.

I have a problem structuring the necessary code to work with what I need.

So, to make things short, I have a page named TAKE ATTENDANCE in which the user selects the course, time start, time end and group.

This information enters into a table named attendance which contains:

attID 	<br />
att_courseID 	 (linked with the course table)<br />
att_periodID 	 (linked with the period table)<br />
lesson_start 	<br />
lesson_end 	<br />
att_duration 	 (duration in time)<br />
att_userID 	<br />
att_taken


Then again, the students are assigned to the attendance by being inserted in the table student_attendance:

studentID 	(student id linked to students table)<br />
attendanceID    (attendance id linked to attendance table)<br />
sa_type         (P or A - so that one knows whether the student was Present or Absent)



Now as for the period table, this table contains:

periodID 	(linked with the attendance period id)<br />
periodStart 	(The start date for the period)<br />
periodEnd 	(The end date for the period<br />
period_Att      (When the attendance should be delivered - irrelevant for now)


What I want is that in the VIEW ATTENDANCE section, the user selects the course and period, and the attendance taken is displayed in the following format:
<br />
PERIOD SELECTED: 7 -> 3/3/2012 to 30/3/2012 (PERIODS ARE ALWAYS IN 4 WEEK INTERVALS)<br />
COURSE SELECTED: Degree in 3D Design<br />
<br />
Week 1 - (26 hours of school hours per week)<br />
                                                    8:00-9:00    9:00-10:00    10:00-11:00    11:00-12:00    12:30-13:30    13:30-14:30    14:30-15:30    15:30-16.30    16.30-17.30<br />
FRANCESCO DIMECH  -  MON:           P                P             NA NA                                               P                   P                   A NA NA<br />
FRANCESCO DIMECH  -  TUE:            P                P                   P NA                                      P                   P      NA NA NA            <br />
FRANCESCO DIMECH  -  WED:           P                P NA NA                                                           P                   P                   A                 A NA<br />
FRANCESCO DIMECH  -  THUR:          A                P   NA NA                                                          P                   P                   P     NA         NA<br />
FRANCESCO DIMECH  -  FRI:                NA               P                   P    NA                                   P                   P                   A NA NA<br />
<br />
Total Hours: 21/26 hours<br />
<br />
JOHN DEMICOLI - MON, TUE, etc<br />
<br />
Week 2<br />
bla bla<br />
<br />
Week 3<br />
bla bla<br />
<br />
Week 4<br />
bla bla<br />
<br />
NA - No Lesson<br />
P - Present<br />
A - Absent


Any idea how the SQL would be done? I tried the following though this gets all the data and is a bit confusing:

SQL
SELECT * FROM periods,attendance,student_attendance,students,course,stipend,users
        WHERE attendance.att_periodID = periods.periodID
        AND attendance.att_courseID = course.course_ID
        AND attendance.att_userID = users.userid
        AND student_attendance.attendanceID = attendance.attID
        AND student_attendance.studentID = students.stud_ID
        AND students.stud_StipendID = stipendID
        AND attendance.att_courseID = '$course'
        AND attendance.att_periodID = '$period'



I also retrieved the course hours according to the course selected by using:

SQL
SELECT course_Hours FROM course
        WHERE course_ID = '$course'


What SQL do I need to get such data structure? I'm a bit confused.

Help would be much appreciated!

Thanks,
Francesco
Posted
Updated 4-Mar-12 19:18pm
v4
Comments
graciax8 24-Feb-12 4:21am    
you wanted a view attendance query where after you selected the date, the result would be like:

Required Hours Student Name Attended Hours
30 hours Nancy Ping 15

is that like that?
Necron8 24-Feb-12 15:29pm    
Yes exactly!, I tried to explain the algorithm above, but don't know what query to use. You would be the best & awesomest if you could explain to me what is needed!

The user selects the Course for which he / she wants the attendance while also selecting the period no

So:

Dropdown: SELECT PERIOD
Dropdown: SELECT COURSE

Will return what you said - the stuff needed - where the period is ex say 1 (Period no 1 for example has a START DATE of 1/1/2012 and END DATE of 28/1/2012
) and Course is Engineering

THANKS!
graciax8 29-Feb-12 21:08pm    
Hi Necron!

Im sorry I wasn't able to answer you fast. Im on leave. Anyways, try this query and tell if it is right.

SELECT SUM(a.att_duration as 'Total Class Hours'), min(sa.studentName) as 'studentName', SUM(CASE WHEN sa.sa_type = 'P' THEN att_duration end) AS 'Total Hours Attended'
FROM attendance AS a
LEFT OUTER JOIN student_attendance AS sa ON sa.attendanceID = a.attID
LEFT OUTER JOIN periods AS p ON p.perioID = a.attID
LEFT OUTER JOIN students AS s on s.studentID = sa.studentID
WHERE NOT sa.sa_type = 'N/A' AND a.att_courseID = '' AND a.att_periodID = ''
GROUP BY sa.studentID

If you have questions just comemnt and if this is helpful, mark as answer.

Best Regards,
@iamsupergrasya
Necron8 2-Mar-12 14:41pm    
I can't seem to make it work ;/

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as 'Total Class Hours'), min(sa.studentName) as 'studentName', SUM(CASE WHEN sa.' at line 1

SELECT SUM(a.att_duration as 'TotalClassHours'), min(sa.studentName) as 'studentName', SUM(CASE WHEN sa.sa_type = 'P' THEN att_duration end) AS 'Total Hours Attended' FROM attendance AS a LEFT OUTER JOIN student_attendance AS sa ON sa.attendanceID = a.attID LEFT OUTER JOIN periods AS p ON p.perioID = a.attID LEFT OUTER JOIN students AS s on s.studentID = sa.studentID WHERE NOT sa.sa_type = 'N/A' AND a.att_courseID = '1' AND a.att_periodID = '8' GROUP BY sa.studentID



Do I need to change some of the tables in this query or? everything seems fine. I tried changing a.att_duration to attendance.attendance_duration and all the others but the same error occurs. What should I do? Sry for being such a noob at this ;/

Hi Necron8. I'll have my replies posted here since this is a solution. That's fine but you should learn it also since the error is very basic, i just put the closing parenthesis on the wrong one.
Again, if you have questions just comment and if this is helpful, mark as answer.

SQL
SELECT SUM(a.att_duration) as 'TotalClassHours', min(sa.studentName) as 'studentName', SUM(CASE WHEN sa.sa_type = 'P' THEN att_duration end) AS 'Total Hours Attended' FROM attendance AS a LEFT OUTER JOIN student_attendance AS sa ON sa.attendanceID = a.attID LEFT OUTER JOIN periods AS p ON p.perioID = a.attID LEFT OUTER JOIN students AS s on s.studentID = sa.studentID WHERE NOT sa.sa_type = 'N/A' AND a.att_courseID = '1' AND a.att_periodID = '8' GROUP BY sa.studentID 


God bless!
 
Share this answer
 
Hey,You can use inner join to get all table values....
 
Share this answer
 
Comments
Necron8 28-Feb-12 15:10pm    
Currently, what I did was to make a while loop to get the Period Start Date and Period End date and also the dates between. From the while loop, I search the attendance and get the students associated to that attendance.

If I have 3 attendances on a certain date (ex 26/2/2012), the query gives you 3 sets of students.
What I want is one set of student with the lesson start/end and attendance as I described above.

How could this be done?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900