[英]how to get all rows in column to insert db in codeigniter
我试图将所有显示视图的行插入表中,但它仅插入一个值为'1'的行,而不是所有行,并且一列中不包含该ID:
视图:
echo"<table class='table table-hover type-list2'id='traineeList'>
<tr class='success'>
<th>Trainee ID</th>
<th>Trainee Name</th>
<th>Present</th>
</tr>";
foreach($list['trainee'] as $row){
echo "
<tr><td>".str_pad($row->TraineeID,7,"0", STR_PAD_LEFT)."</td>
<td>".$row->Name."</td>
<td><input type='checkbox' name='.$row->TraineeID[]' value='".$row->TraineeID."' checked></td>
</tr>";
}
echo "</table>";
控制器:
public function insertAttendance(){
$data = array('TraineeID'>=$this->input->post('TraineeID'));
$attnDate=$this->input->post('attnDate');
$classHour= $this->input->post('classHour');
foreach ($data as $id){
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
redirect('attendance/index/');
}
您的重定向功能可能应该放在foreach
循环之外。 查看您的代码,您的foreach
循环将只运行一次,然后将您重定向到另一页。
请尝试这个
foreach($_POST['TraineeID'] as $key => $id) {
$attnDate=$_POST['attnDate'][$key];
$classHour= $_POST['classHour'][$key];
$query="INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('".$id."','".$attnDate."','".$classHour."')";
$this->db->query($query);
}
redirect('attendance/index/');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.