从我的角度来看,我有两个数组。我需要插入它们彼此对应的数据。
以下是我的观点
<?php foreach ($seats as $seat):?>
<tr>
<td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
<td><?php echo $seat->seatLabel;?></td>
<td><input type="hidden" name="seat_label[]" value="<?php echo $seat->seatLabel;?>"></td>
</tr>
<?php endforeach;?>
从上面的视图中,seat_id 和 seat_label 携带必须存储在数据库中的值
这是我的控制器
if($this->form_validation->run()){
$seat_ids = $this->input->post("seat_id[]");
$seat_labels = $this->input->post("seat_label[]");
$busNumber = $this->input->post("busNumber");
$bookingDate = $this->input->post("bookingDate");
$reportingTime = $this->input->post("reportingTime");
$departureTime = $this->input->post("departureTime");
$this->load->model('Queries');
$insert = $this->Queries->saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime);
这是我的模型
public function saveMessage($seat_ids, $seat_labels, $busNumber, $bookingDate, $reportingTime, $departureTime){
foreach($seat_ids as $seat_id )
foreach($seat_labels as $seat_label ){
{
$record = array(
'seatNumber' => $seat_id,
'seatLabel' => $seat_label,
'bookingDate' => $bookingDate,
'reportingTime' => $reportingTime,
'departureTime' => $departureTime,
'busNumber' => $busNumber,
'seatUse' => 'Enabled',
'seatStatus' => 'Available',);
$this->db->insert('schedule', $record);
}