Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Dynamic Checkboxes

    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 Code:
    $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>
       </
    tr>
      while(list(
    $id$studname$studroll) = mysql_fetch_row($report))
      {
      <
    tr>
        <
    td>echo $id</td>
        <
    td>echo $studname</td>
        <
    td>echo $studroll</td>
        <
    td align="center">echo '<input type="hidden" name="att[]" value="0"/>';echo '<input type="checkbox" name="att[]" value="1"  />';</td>
       </
    tr>
       }
       
      echo 
    "</table>"
      
    <input type="submit" name ="submit2"  id="submit2" value ="submit"></input>
    </
    form
    and my attendance.php file
    PHP Code:
    $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??
    Last edited by havish; 10-14-2012 at 03:06 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,455
    Thanks
    23
    Thanked 631 Times in 630 Posts
    In your first code post your bouncing back and forth between html code and php code. It's not gonna work. If the file is php then use echos for the html part. All of them.

    All I see you trying to send to attendance.php is the status of the checkbox. You also need to know who it is. You way of getting "$report = mysql_query("SELECT id FROM student")" does not tie the two things together. And, I would think, what day it was would be nice.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    In your first code post your bouncing back and forth between html code and php code. It's not gonna work. If the file is php then use echos for the html part. All of them.

    All I see you trying to send to attendance.php is the status of the checkbox. You also need to know who it is. You way of getting "$report = mysql_query("SELECT id FROM student")" does not tie the two things together. And, I would think, what day it was would be nice.
    thanks for the suggestion. could you provide any alternate method for what I am trying?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Do you need to actually track a P and A? My suggestion would be that of providing the date of attendance, and if there is no associated studentid then you would consider them absent.
    First correct the HTML. To tie the studentid in properly, you have to add it to the checkbox offsets: att[$id] in the HTML code, otherwise it will always use an incrementing offset and fields that are not checked are not considered successful. So, if you check the first and fourth students, the second and third are not present. But, because they are not present, the form will submit att[0] and att[1] without explicitly telling it what they are.

    For an insert, assuming you can get away with just the date, you can use a datetime datatype and a studentid, then use a simple insert:
    PHP Code:
    $aKeys array_keys($att);
    $aInsert = array();
    foreach (
    $aKeys AS $student)
    {
        
    $aInsert[] = sprintf('(%d, NOW())', (int)$student);
    }

    if (
    count($aInsert) > 0)
    {
        
    $sInsert 'INSERT INTO `attendance`(`stud_id`,`att`) VALUES ' implode(', '$aInsert);

    Where $sInsert can now be used in a Mysql query (or you can use binding with a statement and PDO/mySQLi). I assume stud_id is an integer.

    HTML would be created as such:
    PHP Code:
    $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>
       </tr>
    <?php
      
    while(list($id$studname$studroll) = mysql_fetch_row($report))
      {
        print(
    '<tr>' PHP_EOL);
        
    printf('<td>%d</td>' PHP_EOL$id);
        
    printf('<td>%s</td>' PHP_EOL$studname);
        
    printf('<td>%s</td>' PHP_EOL$studroll);
        
    printf('<td><input type="checkbox" name="att[%d]" value="1"/></td>' PHP_EOL$id);
        print(
    '</tr>');
       }
    ?>
      </table>
      <input type="submit" name ="submit2"  id="submit2" value ="submit"></input>
    </form>

  • #5
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you Fou-Lu.. I am new to PHP.. So I will first understand your code and then implement. Well is my idea correct for implementing the attendance for student or is there any better solution for this?

  • #6
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Fou-Lu.. I got my problem solved.. Had to include id in array as u told.. It worked.. Ill post the final code in the next post..

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is how I did with help of Fou-Lu. Hope this is correct because I got what result i want..
    PHP Code:
    $report = mysql_query("SELECT id, studname, studroll FROM student ") or die(mysql_error());
    echo '<form action="" method="post">
          <table width="600" border="2">
          <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>
       </tr>';
       while(list($id, $studname, $studroll) = mysql_fetch_row($report))
       {
         echo '<tr>
                    <td>'.$id.'</td>
                    <td>'.$studname.'</td>
                    <td>'.$studroll.'</td>
                    <td><input type="hidden" name="att['.$id.']" value="0"/><input type="checkbox" name="att['.$id.']" value="1"/></td>
               </tr>';
       }
       echo '</table><input type="submit" name ="submit2"  id="submit2" value ="submit"></input>
    </form>';
    ?>
    <?php
    $att 
    $_POST['att'];
    foreach(
    $att AS $key => $value)
    {
     
    $attendance  =$value 'P' 'N';
     
    $query "INSERT INTO `samp`(`stud_id`,`attendance`) VALUES ('".$key."','".$attendance."')";
     
    mysql_query($query);
         
    }
    Please let me know if there any mistakes..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •