我不知道如何更好地表达它,但我认为我的编码展示揭示了我的问题。。我试图从foreach循环中获得一个值的select标记列表,并将其转换为数组,然后使用表单post操作传递它,我在从循环中检索select标记值数组时遇到了困难。。这是我的代码
<form method="post" action="<?php echo base_url();?>index.php?admin/manage_attendance/
<?php
//STUDENTS ATTENDANCE
foreach($students as $row)
{
?>
<select name="status" class="form-control" style="width:100px; float:left;">
<option value="0" <?php if($status == 0)echo 'selected="selected"';?>></option>
<option value="1" <?php if($status == 1)echo 'selected="selected"';?>>Present</option>
<option value="2" <?php if($status == 2)echo 'selected="selected"';?>>Absent</option>
</select>
<?php
}
?>
<input type="submit" class="btn btn-default" value="save" style="float:left; margin:0px 10px;">
</form>
的代码
<select name="status" class="form-control" style="width:100px; float:left;">...
</select>
在foreach循环内部,当然它将为每个循环执行。也许你可以把它放在循环之外。
我得到了一个简单的解决方案。。我只写了一些代码,我的解决方案是添加一个数组作为select标记的name属性,使foreach语句在循环数组时填充数组,这是完整的代码
index.php?admin/manage_attendance/">
<?php
//STUDENTS ATTENDANCE
foreach($students as $row)
{
?>
<tr class="gradeA">
<td><?php echo $row['roll'];?></td>
<td><?php echo $row['name'];?></td>
<td align="center">
<?php
//inserting blank data for students attendance if unavailable
$verify_data = array( 'student_id' => $row['student_id'],
'date' => $full_date);
$query = $this->db->get_where('attendance' , $verify_data);
if($query->num_rows() < 1)
$this->db->insert('attendance' , $verify_data);
//showing the attendance status editing option
$attendance = $this->db->get_where('attendance' , $verify_data)->row();
$status = $attendance->status;
?>
<select name="status[]" class="form-control" style="width:100px; float:left;">
<option value="0" <?php if($status == 0)echo 'selected="selected"';?>></option>
<option value="1" <?php if($status == 1)echo 'selected="selected"';?>>Present</option>
<option value="2" <?php if($status == 2)echo 'selected="selected"';?>>Absent</option>
</select>
<input type="hidden" name="student_id" value="<?php echo $row['student_id'];?>" />
<input type="hidden" name="date" value="<?php echo $full_date;?>" />
<?php
}
?>
<input type="text" name="test" value="<?php echo $string?>" />
<input type="submit" class="btn btn-default" value="save" style="float:left; margin:0px 10px;">
</form>