StrangerInBeijing
Jun 11th, 2008, 09:04 PM
I got this page I want to use to edit records in the database. All text fields, and one bool (tinyint(1)) field to indicate whether the record is active or not.
I build up a table of records from the database like this:
<?php
$count = $results->num_rows;
while($row = $results->fetch_object()) {
?>
<tr>
<td>
<input type="hidden" name="_count" value="<?php echo $count ?>"/>
<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
</td>
<td>
<input name="lang_code[]" type="text" id="lang_code" value="<?php echo $row->lang_code; ?>" />
</td>
<td>
<input name="name_en[]" type="text" id="name_en" value="<?php echo $row->name_en; ?>" />
</td>
<td>
<input name="name_native[]" type="text" id="name_native" value="<?php echo $row->name_native; ?>" />
</td>
<td>
<input type="checkbox" <?php if($row->active) echo "checked='checked'"; ?> value="<?php echo $row->active; ?>" name="active[]" id="active" />
</td>
</tr>
<?php } ?>
and at the top of the page run the following if the page was posted:
for($i=0;$i< $_POST['_count'];$i++){
$id=$_POST[id][$i];
$lang_code=$_POST[lang_code][$i];
$name_en=$_POST[name_en][$i];
$name_native=$_POST[name_native][$i];
$active=$_POST[active][$i];
update_language($id,$lang_code,$name_en,$name_native,$active);
}
Everything works fine, it's just I cannot get whether the check box ($active) was checked or not.
I build up a table of records from the database like this:
<?php
$count = $results->num_rows;
while($row = $results->fetch_object()) {
?>
<tr>
<td>
<input type="hidden" name="_count" value="<?php echo $count ?>"/>
<input name="id[]" type="hidden" id="id" value="<?php echo $row->id; ?>" />
</td>
<td>
<input name="lang_code[]" type="text" id="lang_code" value="<?php echo $row->lang_code; ?>" />
</td>
<td>
<input name="name_en[]" type="text" id="name_en" value="<?php echo $row->name_en; ?>" />
</td>
<td>
<input name="name_native[]" type="text" id="name_native" value="<?php echo $row->name_native; ?>" />
</td>
<td>
<input type="checkbox" <?php if($row->active) echo "checked='checked'"; ?> value="<?php echo $row->active; ?>" name="active[]" id="active" />
</td>
</tr>
<?php } ?>
and at the top of the page run the following if the page was posted:
for($i=0;$i< $_POST['_count'];$i++){
$id=$_POST[id][$i];
$lang_code=$_POST[lang_code][$i];
$name_en=$_POST[name_en][$i];
$name_native=$_POST[name_native][$i];
$active=$_POST[active][$i];
update_language($id,$lang_code,$name_en,$name_native,$active);
}
Everything works fine, it's just I cannot get whether the check box ($active) was checked or not.