...

View Full Version : Dynamic Checkboxes



havish
10-14-2012, 03:48 PM
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..


$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


$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??

sunfighter
10-14-2012, 06:29 PM
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.

havish
10-14-2012, 06:55 PM
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?

Fou-Lu
10-14-2012, 07:10 PM
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:


$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:


$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>

havish
10-15-2012, 05:26 AM
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?

havish
10-15-2012, 09:34 AM
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..

havish
10-15-2012, 09:37 AM
Here is how I did with help of Fou-Lu. Hope this is correct because I got what result i want..

$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..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum