簡體   English   中英

使用Codeigniter同時更新從foreach輸入字段派生其值的多個數據庫記錄

[英]Simultaneously update multiple database records deriving their values from foreach input field using codeigniter

我是一名底層代碼愛好者,嘗試開發學生成績應用程序。 輸入所有學科的分數后,它將捕獲每個學科的學生分數,並生成全面的報告表格。

如果可以為班級中的所有學生輸入學生標記並一次發布所有標記(全部更新按鈕),而不是單獨發布每個學生標記(更新按鈕),這將簡化教師的工作。 那就是我的困境。

學生分數表

在此處輸入圖片說明

在我的控制器中,更新代碼為:

if ($this->input->post('operation') == 'update') {
$data['mark_obtained'] = $this->input->post('mark_obtained');
$data['mark_total'] = $this->input->post('mark_total');

if($data['mark_obtained']<=$data['mark_total'] && $data['mark_obtained']>=0){
$this->db->where('mark_id', $this->input->post('mark_id'));
$this->db->update('mark', $data);
}

更新按鈕可以正常工作。 我只是似乎沒有概念化如何使“全部更新”工作。 請幫忙。

<?php 
$my_tabindex=1;

foreach($marks as $student_mark):
<tr>
    <td  class="span3">
        <?php 
            //this is derived from another query that returns information      over students of a selected class
            echo $row['student_name'];  
        ?>
    </td>
    <td>
          <input type="number" step = "0.01" value="<?php echo $student_mark['mark_obtained'];?>" name="mark_obtained" tabindex="<?php echo $my_tabindex+1; ?>" />
    </td>
<td>
    <input type="hidden" name="operation" value="update" />
    <button type="submit" class="btn btn-normal btn-gray"> Update</button>
</td>
</tr>
?>
<?php 
endforeach;
?>  

請注意,這是我有史以來的第一個答案,因此對於格式/規則破壞,我深表歉意。

擴展對Hassan ALi的評論。 您應該發布一個值數組而不是一個值。

Codeigniter具有此功能: “要返回包含多個POST參數的數組,請將所有必需的鍵作為數組傳遞。$ this-> input-> post(array('field1','field2'));“ https://www.codeigniter.com/user_guide/libraries/input.html

創建數組時,我會考慮使用mark_id作為鍵。 例如,如果您有一個名為$ marks_obtained_array的數組,則可以插入鍵值對'id'->'mark'。

然后,如果您還有mark_id的數組,則可以輕松地在控制器中循環遍歷它們以進行更新。

if ($this->input->post('operation') == 'update_all') {
     foreach($mark_ids_array as $mark_id){
         $data['mark_obtained'] = $marks_obtained_array[$mark_id];
         $data['mark_total'] = $marks_total_arry[$mark_id];

         if($data['mark_obtained']<=$data['mark_total'] && $data['mark_obtained']>=0){
              $this->db->where('mark_id', $mark_id);
              $this->db->update('mark', $data);
          }
....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM