我有一个表单,填充了数据库中的数据,每个记录中都有一个单选按钮列表按钮。勾选并提交表单后,只有最后一条记录保存到数据库中,但我希望保存所有记录。 这是我的代码: //获取/查询数据库的表单代码
0,0-1440,315
//控制器代码获取并将结果发送到表单
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'class')->label('Class')->dropDownList(
Arrayhelper::map(Classes::find()->all(), 'class_name',
'class_name'),
[
'maxlength' => true,
'style'=>'width: 300px; height: 50px;',
'prompt'=>'select class',
'onchange'=>'
$.post( "index.php?
r=attendance/list&id='.'"+$(this).val(), function(data){
$("section#Attend").html(data);
});'
]); ?>
//我的actionCreate来自同一个控制器
public function actionList($id){
$model = new Attendance;
$form = new ActiveForm;
$countclass = Registrationinfo::find()
->where(['current_class' => $id])
->count();
$lga = Registrationinfo::find()
->where(['current_class' => $id])
->all();
if($countclass > 0){
foreach($lga as $data){ ?>
<div >
<div class="col-md-1"><?= ('1')?></div>
<div style="display: inline;"><?= $data['first_name'] ?></div>
<div style="display: inline; margin-left: 20px;"><?=
$data['surname'] ?></div>
<div style="display: inline; margin-left: 79px;"><?=
$data['gender'] ?></div>
<div style="display: inline; margin-left: 20px;"><?= $form-
>field($model, 'student_id')->textInput(['value'=>$data->reg_no,
'disabled'=>''])->label(false) ?></div>
<div style="display: inline; margin-left: 150px;"> <?= $form-
>field($model, 'status')
->radioList(array('PR' => 'Present', 'PM' =>'Permission', 'LT'
=>'Late', 'AB' =>'Absent'), array('class' => 'i-checks')); ?>
<hr style="color: blue;">
<?php
}
}
else{
echo "<div class=>'info'>Invalide Class name </div>";
}
}
答案 0 :(得分:0)
将表单的name属性声明为array,for update / insert遍历表单的每个元素(用于每个元素)并为每个元素运行insert / update语句。