-2
ALTERNATIVE CODE

<?php
// Update present values
if (isset($_POST['submit'])) 
{
    // Get a list of student ids to check
    $idsResult = mysql_query("SELECT student_id from students");

    $presentIds = array();
    $absentIds = array();
    while($idRow = mysql_fetch_array($idsResult))
    {
       // If the student's checkbox is checked, add it to the presentIds array.
       if(isset($_POST['present'.$idRow['student_id']]))
       {
         $presentIds[] = $idRow['student_id'];
       }
       else
       {
         $absentIds[] = $idRow['student_id'];
       }
    }

      // Convert array to string for query
      $idsAsString = implode(",", $presentIds);

      // You can set present to whatever you want. I used 1. 
      $sql = "UPDATE course_attendance SET present='1' WHERE course_id='101' AND week_id='2' AND student_id IN (".$idsAsString.")";
      $result = mysql_query($sql);

      if($result){
        echo "Successfully logged the attendance for IDs ".$idsAsString;
      }
      else {
        echo "ERROR updating on IDs ".$idsAsString;
      }


      // OPTIONAL: Mark absent students as '0' or whatever other value you want
      $absentIdsAsString = implode(",", $absentIds);
      // You can set present to whatever you want. I used 1. 
      $absentQuery = "UPDATE course_attendance SET present='0' WHERE course_id='101' AND week_id='2' AND student_id IN (".$absentIdsAsString.")";
      $absentResult = mysql_query($absentQuery);

      if($absentResult){
        echo "Successfully logged absence for IDs ".$absentIdsAsString;
      }
      else {
        echo "ERROR updating absence on IDs ".$absentIdsAsString;
      }

}

$test3= "SELECT * FROM course_attendance, students, courses, attendance WHERE course_attendance.course_id=courses.course_id AND course_attendance.week_id=attendance.week_number_id AND course_attendance.student_id= students.student_id AND courses.course_id='101' AND attendance.week_number_id='2' ";
$result = mysql_query($test3)  or die(mysql_error());
?>
<form name='Biology_lecture11.php' method='post'>
</br>
<table border='1' align='center'>
  <tr>
    <th><strong>Student ID</strong></th>
    <th><strong>First Name </strong></th>
    <th><strong>Last Name</strong></th>
    <th><strong>Present</strong></th>
  </tr>
<?php
while($rows=mysql_fetch_array($result)){
  echo "<tr><td width='100' align='center'>" .$rows['student_id'].
  "</td><td width='120' align='center'>" .$rows['fname'].
  "</td><td width='120' align='center'>" .$rows['lname'].
  "</td><td><input type='checkbox' name='present".$rows['student_id']."' ";

  // NOTE: REPLACE 1 with whatever value you store in the database for being present. 
  // I used 1 since the update at the top of the code uses 0 and 1.
  if($rows['present']=='1')
  {
    echo "checked='checked' ";
  }
  // With a checkbox, you don't need to assign it a value.
  echo "value=" .$rows['present'];

  echo ">";
}
?>
</table>
<input type='submit' name='Submit' value='Submit'>
</form>

I need to create a database for the code above, That is my task.. and how to connect it...
If anyone can get a database created in mysql and then export it to me then it would be good or just give me points on what tables and type of database to create and then i could do it... please respond fast as possible as i have to get it completed by tomorrrow morning....

3
Contributors
2
Replies
10
Views
2 Years
Discussion Span
Last Post by lps
0

We are not going to provide code for you to get your task done. What we do, we can only give guidance
Tables you should create and the fields needed:

course_attendance
    present
    course_id
    week_id
    student_id
students
    student_id
courses
    course_id
attendance
    week_number_id

I am sure you can googling for 'php mysql database connect' or 'create mysql table' and found plenty of guidance out there.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.