2017-05-24 16 views
-2

我有一個表格,填充了數據庫中的數據,每個記錄中都有一個無線電列表按鈕。在勾選並提交表單後,只有最後一條記錄被保存到數據庫,但我希望保存所有記錄。 這是我的代碼: //形式代碼讀取/查詢數據庫無法使用一個表格將多個記錄提交到同一個數據庫中

<?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); 
        });' 
       ]); ?> 

//控制器代碼的讀取和結果從發送到窗體

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>"; 
     } 
    } 

//我actionCreate同一控制器

public function actionCreate() 
    { 
     $model = [new Attendance()]; 
     //select date from datesetup where status is open 
     $date = DateSetup::findbysql('SELECT date from date_setup WHERE 
    status = "open"')->all(); 
     if(count($date) > 0){ 
      foreach ($date as $dates); 
     }else{ 
      echo "No date As been opened yet"; 
     } 
     //select Term from Termsetup where status is open 
     $terms = Termsetup::findbysql('SELECT term from termsetup WHERE 
    status = "open"')->all(); 
     if(count($terms) > 0){ 
      foreach ($terms as $term); 
     }else{ 
      echo "No Term As been opened yet"; 
     } 

     if ($model->load(Yii::$app->request->post()) && $model->validate()) 
    { 
    //adding the database value retrive to the attandance form field  
      $model->term = $term->term; 
      $model->day = date('d', strtotime('$dates')); 
      $model->month = date('m', strtotime('$dates')); 
      $model->year = date('Y', strtotime('$dates')); 
      $model->date = date('Y-m-d h:m:s'); 
      $model->user_id = "admin"; 
      if (isset($_POST['student_id'])) { 
    $model->student_id = $_POST['student_id']; 
    // validate, save or more.. 
    } 
    else { 
     echo "reg_no not posted"; //return array 
    } 
      //$model->student_id = $data->student_id; 
      //$model->user_id = Yii::$app->user->identity->user_id; 
      if($model->save()) 

     return $this->redirect(['view', 'id' => $model->id]); 
     } 
     return $this->render('create', [ 
      'model' => $model, 
      ]); 
    } 
+0

你需要開始自己做一些調試,我們不會通讀所有這些代碼來找出什麼不適合你。打開錯誤報告並檢查您的日誌。 – Epodax

+0

問題是,在滴答第一個單選按鈕之後,當我勾選第二個單選按鈕(在下一個記錄中)時,第一個按鈕會自動取消選中它本身@Epodax –

回答

0

聲明的形式陣列的名稱屬性,用於更新/插入去的形式的每一個元素(用於每個)和過爲每個元素運行插入/更新語句。

+0

對於yii框架工作來說是一種新穎的方式,並且可能會有點困難在哪裏/如何申報@ S.Kartik –

+0

示例代碼: 」style =「text-align:center 「onkeypress ='return event.charCode> = 48 && event.charCode <= 57'> foreach($ hour as $ key => $ value){ #code ... // echo $ value。」 - 「。$ date [$關鍵] 「\ n」。 $ enter_date = $ date [$ key]; // $ sql_statement = –

相關問題