首页 > 解决方案 > 如何在单个提交中逐行插入值

问题描述

这是我的观点页面

在此处输入图像描述

这是动态字段测试名称和单位来自表我需要插入测试名称和结果以及正常值也单位。

我的控制器

 $patient_id = $this->input->post('patient_id');
                        $doctor_id = $this->input->post('doctor_id');
                        $prescription_id = $this->input->post('prescription_id'); 
                        $lab_result =$this->input->post('lab_result');
                        $lab_test =$this->input->post('lab_test');
                        $units =$this->input->post('units');
                       $normal_value =$this->input->post('normal_value');
                       $cat_id = $this->input->post('cat_id');

     for($i=0; $i<count($prescription_id); $i++)
           {
                    $labreport[] = array(
                       'patient_id' => $patient_id[$i],
                       'doctor_id' => $doctor_id[$i],
                       'prescription_id' =>$prescription_id[$i],
                       'lab_result'   => $lab_result[$i],
                       'lab_test'     => $lab_test[$i],
                       'units'        =>  $units[$i],
                       'normal_value' => $normal_value[$i],
                       'cat_id' => $cat_id, );
                      //echo '<pre>'; print_r($labreport); '</pre>'; exit; 
                         }
                       $stringlabreport= json_encode($labreport);
                         $this->db->insert('patient_lab_report',$stringlabreport); 
                          if($this->db->affected_rows()){
                        return true;
                    } else {
                        return false;
                    }
        $this->session->set_flashdata('message','Lab Report  Added Successfully');
                          redirect('laboratory_report/all');

 =========================

这是我的查看代码

 <tr>   
                            <td><input type="hidden" value="<?php echo $data->name; ?>"name="lab_test[]"><?php echo $data->name; ?></td>                        
                            <td><input type="text" value=""        name="lab_result[]" class="form-control"></td>
                            <td><input type="hidden" value="<?php echo $data->units; ?>"    name="units[] "> <?php echo $data->units; ?></td>
                            <td><input type="hidden" value="<?php echo $data->n_value; ?>" name="normal_value[] "> <?php echo $data->n_value; ?></td>
                        </tr>

标签: phpcodeigniter

解决方案


使用数组名称作为输入。IE:

<form action="" method="post" name="someform">
    <input name="row[0][test_name]"/>
    <input name="row[0][result]"/>
    <input name="row[1][test_name]"/>
    <input name="row[1][result]"/>
    <input type="submit" name="submitted" value="send">
</form>

因此,在提交时,在 php 中解析,使用filter_inputwithFILTER_REQUIRE_ARRAY作为选项:

<?php

$submitted = filter_input(INPUT_POST, 'submitted');
if(!empty($submitted)){

    $rows = filter_input(INPUT_POST,'row',  FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
    $parsed_rows = [];
    foreach($rows as $row){
        if(!empty($row['test_name']) && !empty($row['result'])){
            $parsed_rows[] = $row;
        }
    }

}

?>

推荐阅读