根据表单php上的Check字段在数据库中选择一些字段

时间:2019-06-13 01:30:45

标签: php yii2

我的表名Checkin包含以下字段:id,chief_compain,takeing_history,pass_history,physical_exam和诊断。

我想使用表单上的Checkbox Attribute作为表“ Checkin”中字段的名称,因此在提交表单后,将仅选择从数据库中选择的列名称。

    <?php $form = ActiveForm::begin(); ?>
        <?= $form->field($model, 'id')->checkbox(['value'=>Yii::$app->request->get('id')]) ?>
         <?= $form->field($model, 'chief_compain')->checkbox(['value'=>'chief_compain']) ?>
        <?= $form->field($model, 'taking_history')->checkbox(['value'=>'taking_history']) ?>
        <?= $form->field($model, 'pass_history')->checkbox(['value'=>'pass_history']) ?>
        <?= $form->field($model, 'physical_exam')->checkbox(['value'=>'physical_exam']) ?>
        <?= $form->field($model, 'diagnosis')->checkbox(['value'=>'diagnosis']) ?>

    <div class="form-group text-center">
            <?= Html::submitButton($model->isNewRecord ? 'Submit' : 'Save Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
        </div>
      <?php ActiveForm::end(); ?>




    // === Controler 

    public function actionMedicalReport()
        {

            $model = new CheckIn();

            if ($model->load(Yii::$app->request->post()) ) {
                  $id=Yii::$app->request->post('id');
    $string = '';
            foreach (\Yii::$app->request->post() as $key => $value){
    if($value){
                    $string .= $key . ' ';
                    var_dump($string);
                }
            }

    $report=CheckIn::find()->select($string)->where(['id'=>$id])->all();
    return $this->render('print-report', ['report' => $report,]);

            } else {
                return $this->render('medical-report', [
                    'model' => $model,
                ]);
            }
        } 

2 个答案:

答案 0 :(得分:-1)

好,您不能以Yii2格式提交。您必须使用ActiveForm小部件或使用令牌插入隐藏字段,否则您将获得异常。 第二:如果您在整个POST或GET中传递数据,则可以使用循环轻松检查它。因为您使用复选框,所以我假设您在整个帖子中都通过了,值是true还是false。

$string = '';
foreach (\Yii::$app->request->post() as $key => $value){
    if($key !== '_csrf-backend' && $value){
        $string .= $key . ' ';
    }
}

之后,您可以创建新的查询,也可以在循环之前实现该查询并在循环中传递所需的字段。我强烈建议不要使用原始sql。

答案 1 :(得分:-1)

我已经解决了问题。谢谢大家的回应 现在效果很好

1) Change name of field to array and use only 1 name
<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'id')->checkbox(['value'=>Yii::$app->request->get('id')]) ?>
     <?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'chief_compain']) ?>
    <?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'taking_history']) ?>
    <?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'pass_history']) ?>
    <?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'physical_exam']) ?>
    <?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'diagnosis']) ?>

<div class="form-group text-center">
        <?= Html::submitButton($model->isNewRecord ? 'Submit' : 'Save Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
    </div>
  <?php ActiveForm::end(); ?>

2) in Controller 

 $model = new CheckIn();

        if ($model->load(Yii::$app->request->post()) ) {
              $id=Yii::$app->request->post('id');
$string = '';
$post=$model->chief_compain;
        foreach ($post as $key ){
if($key)
{
               $string .=$key.' ';
   }             

        }
        $myval=str_replace(" ",",",trim($string));

$report=CheckIn::find()->select($string)->where(['id'=>$id])->all();
return $this->render('print-report', ['report' => $report,]);