I have generated checkboxes dynamically and want to insert into att field in attendance table.. ie when user checks the checkbox P would be inserted and when he does not check A would be inserted.. I am trying out attendance system for students.. here is what i have tried..

<?php
$report = mysql_query("SELECT id, studname, studroll FROM student") or die(mysql_error());
?>
<form action="attendance.php" method="post">
<table id = "attendance" width="567" border="1">
  <tr>
    <th width="83" scope="col">ID</th>
    <th width="83" scope="col">Student Name</th>
    <th width="55" scope="col">Student Roll.No</th>
    <th width="51" scope="col">Attendance</th>

    <th width="276" scope="col">Remarks of Absentees</th>
  </tr>
  <?php while(list($id, $studname, $studroll) = mysql_fetch_row($report))
  {

  ?>
  <tr>
    <td><?php echo $id ?></td>
    <td><?php echo $studname ?></td>
    <td><?php echo $studroll ?></td>


    <td align="center"><?php echo '<input type="hidden" name="att[]" value="0"/>';?><?php echo '<input type="checkbox" checked="checked"  name="att[]" value="1"  />'; ?></td>

    <td align="center"><label for="remarks"></label>
    <input type="text" name="remarks" id="remarks" /></td>
  </tr>
<?php
  }

  echo '</table>';
  ?>


  <input type="submit" name ="submit2"  id="submit2" value ="submit"></input>
</form>



and my attendance.php file



<?php
$att = $_POST['att'];

            foreach($att as $key => $attendance) {
                          $at = $attendance ? 'P' : 'N'; 


            }
            $report = mysql_query("SELECT id FROM student") or die(mysql_error());
            while(list($id) = mysql_fetch_row($report))
            {
            $query = "INSERT INTO `attendance`(`stud_id`,`att`) VALUES ('".$id."','".$at."') ";
            $result = mysql_query($query);}


?>

I know i am doing some mistake in the loop but not able to figure out.. Any help??

Member Avatar for diafol

Don't know about your attendance logic, but my school uses codes for various absences, e.g. N (unspecified absence), L (late), B (authorized absences), etc. The 'present' in most apps is represented by '/'.

As we take a register every for every period, I am able to see the pupil's attendance for the previous lessons for that day and it can flag up a possible 'AWOL' if a pupil fails to arrive at my lesson within a few minutes. I can then contact admin to ensure the pupil is safe / accounted for / contact home if missing.

With this in mind, a text input may be more approapriate than a checkbox. However, no essential for a simple solution.

For a good working solution, you could use the following tables:

staff, pupils, groups, timetable, weeks (for cyclic timetables, e.g. fortnightlies)

registers (sent registers - reg_id | timetable_id | schooldate | staff_id | sent_datetime), attendance, staff_groups, pupil_groups

The attendance table could just be:

att_id (optional) | reg_id | pupild_id | status

Just a thought. Anyway, you say there's a problem with the loop - you've got three - to which one are you referring? Just a tip - you're running individual queries for each student by the looks of it. That's gotta hurt - have a look at the VALUES syntax which allows multiple records to be inserted, thereby negating the need for a loop.

thanks diafol. actually i have one more field in my table which is remarks. So the absentees remarks will be inserted here.. So you mean to say your attendance system, the staff has to type the codes for individual students if he late, unspecified absence and so on..?

Member Avatar for diafol

This is usually completed by the admin staff (M = medical, etc) if the absence scheduled or the school has been contacted (e.g. late, illness, medical, school trips, etc).
This then shows up automatically on the teacher's register - so if a kid is out for the morning with a dental appt, I see an 'M' pre-filled in my register.

If a kid turns up late, I give him/her an 'L' instead of a '/'. Our system also allows the entering of minutes late.

AN useful addition to the online registration page is the 'mark all present' button - it saves you going through everybody. It doesn't overwrite prefilled marks - that would be bad!

Anyway, hope it gives you an idea of how existing solutions may be put together.
Truth be told, although this system allows tracking of attendance throughout the day, flagging unexpected absences and automating stats collection - give me a pen and paper anyday :) Something very comforting about seeing a snapshot of attendance for a whole class over the year. Depending on to whom you talk, this may very well be the worst idea ever.

Turn the PC on ... standby ... login ... standby ... wait for Windows to load everything ... standby ... open IE ('yuk!') ... standby ... wait for url to load and then login ... standby ... navigate to the registration page ... standby ... AT LAST fill in the register ... SEND ... standby - OK, get on with the lesson.

Oh darn. Little Johnny's just turned up, OK go back to PC ... Oh no! Session has timed out ... login ...(etc)...

I reckon one guy in an office could tally and calculate every stat for the whole school in a few hours. That vs. all the phaphing about and the teaching time lost. Some sicko in some council office somewhere must have thought that teachers have nothing better to do than to click a mouse for a living. OK, rant over.

commented: Thanks for the info.. +0

Pass students id's ,in your form

with

    <input type="checkbox" checked="checked"  name="att[]" value="1"  />
    <input type="hidden" name="stdId[]" value="<?php echo $id; ?>"  />

in attendance.php

<?php
$att = $_POST['att'];
$stdId = $_POST['stdId'];

            foreach($att as $key => $attendance) {
                          $at = $attendance ? 'P' : 'N';

                $query = "INSERT INTO `attendance`(`stud_id`,`att`) VALUES ('".$stdId[$key]."','".$at."') ";
                $result = mysql_query($query);
            }           
?>
commented: well i tried a different way.. trying with it now.. i will try tour solution also.. but i din't get the idea behind your logic +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.