0

I am working on an attendance reporting page. Here I am creating a dynamic checkbox with content from db.

I can store the checked student(present) students detail, but I also want to store the absent student. In order to do that I want a hidden field or something to get the unchecked student details please anyone help me to do that.

<form name="myform" action="" method="post">
    <div class="checkbox">
        <table border="1" cellspacing="2" cellpadding="5" summary="">
        <?php while ($row = mysql_fetch_assoc($res)){?>
            <tr>
                <td>
                    <input type="checkbox" class="input" id="input<?php echo $row['st_id']; ?>" name="student[]" value="<?php echo $row['st_id']; ?>" checked="checked"> 
                    <?php echo $row['st_name'] ; ?>
                    <label for="input<?php echo $row['st_id']; ?>"></label>
                    <input type="text" name="absent" value="0"/>
                </td>
            </tr>
        <?php }?>
        </table>
        <input type="submit" name="submit" value="submit"/>
    </div>

PHP code

<?php   
$res = mysql_query("SELECT * FROM `student_info` WHERE `sem`='$selsem'");   
if(isset($_POST["submit"]))
{
    //Here goes array
    for($i=0;$i<count($_POST['student']);$i++)
    {  
        $id=$_POST['student'][$i];
        echo $id;
        $check=1;
       mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$id','$seldate','$selsem','$selperiod','$selsub','$check')");
    }
}
?>
Ekin
  • 1,957
  • 2
  • 31
  • 45
Siby Xavier
  • 136
  • 1
  • 1
  • 18
  • 1
    can't you just list the `checked`(present) students and then remove them from all students and then create the absent student list from it? – EhsanT Feb 20 '16 at 06:22
  • its a good idea but i am using an array for storing present students id....so how can i select the absent student using sql command.... – Siby Xavier Feb 20 '16 at 06:37
  • Do you have any estimation of how many present students can be at max? – EhsanT Feb 20 '16 at 06:40
  • no.may vary according to table in db – Siby Xavier Feb 20 '16 at 06:43
  • 1
    OK, if they are not a huge number then you can create a comma separated list of them and then use the same query as the one which are using to create the checkboxes and add a `where st_id not in (your_comma_separated_list)`(or something like this) to generate a list of all students minus present students which I assume will be absent students. – EhsanT Feb 20 '16 at 06:47
  • now,I am trying exactly like you said... – Siby Xavier Feb 20 '16 at 06:53
  • I tried like this but not working. $ans=array(); $ans=$_POST['student']; print_r($ans); $re=mysql_query("SELECT `st_id` FROM `student_info` WHERE `st_id` NOT IN (' . implode(',',array_map('intval', $ans)) . ')"); while ($ro = mysql_fetch_assoc($re)){ $ro['st_id']; } – Siby Xavier Feb 20 '16 at 07:07
  • @SibyXavier - mysql_* is now deprcated. You shld instead be using mysqli_* or PDO. Please see [this link](http://php.net/manual/en/migration55.deprecated.php). – fmc Feb 20 '16 at 07:13
  • this will use. http://stackoverflow.com/questions/19239536/how-get-value-for-unchecked-checkbox-in-checkbox-elements-when-form-posted – Gopalakrishnan Feb 20 '16 at 07:32
  • I have solved the problem>Thankyou @EhsanT for your idea.... – Siby Xavier Feb 20 '16 at 07:54
  • Good, you are welcome. and also as @Landslyde suggested please consider using either `mysqli` or `PDO` since `mysql` is deprecated and it's beside that, it's really vulnerable. – EhsanT Feb 20 '16 at 07:59
  • okay i will do that.Thanks – Siby Xavier Feb 20 '16 at 08:01

1 Answers1

0

As @EhsanT said i have solved the problem with sql command...Thankyou EhsanT....

if(isset($_POST["submit"]))
{
    $ans=array();
    $ans=$_POST['student'];
    $re=mysql_query("SELECT `st_id` FROM `student_info` 
     WHERE `sem` ='$selsem'
  AND `st_id` NOT IN (".implode(',',array_map('intval',$ans)).")");
    while ($ro = mysql_fetch_row($re)){
     mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$ro[0]','$seldate','$selsem','$selperiod','$selsub','0')");  } 
    //Here goes array
    for($i=0;$i<count($_POST['student']);$i++)
    {  
        $id=$_POST['student'][$i];
        //echo $id;
        $check=1;

       mysql_query("insert into manage_attendance(st_id,date,sem,period,subject,status) values('$id','$seldate','$selsem','$selperiod','$selsub','$check')");
        //print_r ($_POST['student']);

    }
}
Siby Xavier
  • 136
  • 1
  • 1
  • 18