Forum Moderators: coopster
[edited by: coopster at 4:27 am (utc) on Dec. 20, 2007]
[edit reason] removed email per TOS [webmasterworld.com] [/edit]
I see three basic approaches, and which way you do it depends on your overall motivation for taking the attendance. The first is 'student-centric', where your primary concern is that the student is attending classes. The second is 'teacher-centric' where you're concerned with how many people are attending the class, perhaps to determine payment. The third is a hybrid wherein you want to be able to easily retrieve the information from either perspective.
All three will require a students table with the student's name and hopefully a unique ID number.
A teacher-centric approach would require a table for the classes. Depending on how you prefer to retrieve the data, you could either have a field for the date and a text field to hold a comma (or other character, doesn't matter) delimited list of student IDs, or you could put each student id on a separate table row. If you have more than one class/teacher on any given day, you'd also want a field to denote the class/teacher and probably another table to hold a registry of teachers or classes. I'd use separate rows.
For a student-centric implementation you could either set up a text field in the student table for a comma-delimited list of attendance dates or you could set up a separate table with fields for student id and date. I recommend the latter.
The hybrid approach would use a student table, a teacher/class table, and attendance table. The attendance table would have fields for student id, teacher/class id, and attendance date.
Personally I'd do the hybrid method since you never know what sort of report someone will want you to produce next week. You wind up with table data comprised of lots of couples or triplets of numbers, but computers get to sort it out and they love numbers so who cares.
I would lay out the form so that all of the checkboxes had the same name as an array, e.g. <input name="attendee[]" and the student's id as the value. I would store the dates as unix timestamps.
Your existing students table is fine, unchanged.
Make a new table (we'll call it ledger) with columns for classdate (big int) and student id. If you're dealing with more than one teacher/class, add a third column for class id. The primary key for the table is all of the columns.
You'll build the form by doing a SELECT query on your students table, like with
$qry = mysql_query("SELECT recID,lastname,firstname FROM students ORDER BY lastname, firstname");
while($dat = mysql_fetch_assoc($qry))
echo '<input type="checkbox" name="students[]" value="' . $dat['recID'] . '">' . $dat['lastname'] . ', ' . $dat['firstname'];
When the form is POSTed, do:
$tm = strtotime($_POST['date']);
foreach($_POST['students'] as $id)
mysql_query("INSERT INTO ledger (classdate,id) VALUES ($tm,$id)");
Doing it this way allows you to retrieve all records matching a particular student id, attendance date or particular teacher/class.
You can learn more about using php to store and retrieve data from mysql by looking in this forum's library. A good thread to look at is:
Basics of extracting data from MySQL using PHP [webmasterworld.com]