如何使用CodeIgniter验证和插入多个记录



我使用codeigniter.i有一个表。

这是源码的视图

<?php $this->load->view("admin/v_header");?>
<?php $this->load->view("admin/v_top_menu");?>
<?php $this->load->view("admin/v_sidebar");?>
<div class="content">
    <div class="header">
        <h1 class="page-title"><?php echo "Form Set Bobot Matapraktikum $kode_mp";?></h1>
    </div>
    <?php $this->load->view("admin/v_alert_msg");?>
    <form class="form-horizontal" name="set_bobot" action="<?php echo base_url();?>admin/save_set_bobot" method="POST">
        <fieldset>
            <table class="table table-bordered table-striped">
                <thead>
                    <tr>
                        <th>Minggu Praktikum</th>
                        <th>Bobot TP</th>
                        <th>Bobot Jurnal</th>
                        <th>Bobot TA</th>
                        <th>Bobot Tubes</th>
                        <th>Bobot Mingguan</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    foreach($total_minggu_praktikum->result() as $row):
                        $total=$row->jumlah_minggu_praktikum;
                    for($i=1;$i<=$total;$i++)
                    {
                        echo "<tr>";
                        echo "<td>".$i."</td>";
                        echo "<td><input type='number' min='0' max='100' name='bobot_tp".$i."' value='0' required='true' class='input-mini'/></td>";
                        echo "<td><input type='number' min='0' max='100' name='bobot_jurnal".$i."' value='0' required='true' class='input-mini'/></td>";
                        echo "<td><input type='number' min='0' max='100' name='bobot_ta".$i."' value ='0'required='true' class='input-mini'/></td>";
                        echo "<td><input type='number' min='0' max='100' name='bobot_tubes".$i."' value='0' required='true' class='input-mini'/></td>";
                        echo "<td><input type='number' min='0' name='bobot_mingguan".$i."' value='0' required='true' class='input-mini'/></td>";
                        echo "</tr>";
                    }
                    ?>
                <input type="hidden" name="total" value="<?php echo $i-1;?>"/>
                <input type="hidden" name="kode_mp" value="<?php echo $kode_mp;?>"/>
                    <?php
                    endforeach;
                    ?>
                   </tbody>
               </table>
            <div class="control-group">
                <div class="controls">
                    <button type="submit" class="btn btn-primary" >
                        <i class="icon-ok icon-white"></i>Save
                    </button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<?php $this->load->view("admin/v_footer");?>

这是控制器的源代码

function save_set_bobot()
{
    $total=  $this->input->post('total');
    $kode_mp= $this->input->post('kode_mp');
    for($i=1;$i<=$total;$i++)
    {
        $bobot_tp[$i] =  $this->input->post("bobot_tp".$i);
        $bobot_jurnal[$i]=  $this->input->post("bobot_jurnal".$i);
        $bobot_ta[$i]=  $this->input->post("bobot_ta".$i);
        $bobot_tubes[$i]=  $this->input->post("bobot_tubes".$i);
        $bobot_mingguan[$i]=  $this->input->post("bobot_mingguan".$i);

        if($bobot_tp[$i]+$bobot_jurnal[$i]+$bobot_ta[$i]+$bobot_tubes[$i]!=100)
        {
            $this->session->set_flashdata("error_msg","Total jumlah bobot TP, Jurnal, TA dan Tubes pada minggu praktikum $i harus 100");
            redirect("admin/set_bobot2?matapraktikum=$kode_mp");
        }
        else
        {
            $prm['kode_mp']=$kode_mp;
            $prm['minggu_praktikum']=$i;
            $prm['bobot_tp']=$bobot_tp[$i];
            $prm['bobot_jurnal']=$bobot_jurnal[$i];
            $prm['bobot_ta']=$bobot_ta[$i];
            $prm['bobot_tubes']=$bobot_tubes[$i];
            $prm['bobot_mingguan']=$bobot_mingguan[$i];
            if($this->mbbt->save($prm))
            {
                $this->session->set_flashdata("success_msg","Data bobot berhasil disimpan");
                redirect("admin/act_list_bobot?matapraktikum=$kode_mp");
            }
            else
            {
                $this->session->set_flashdata("error_msg","Data bobot gagal disimpan");
            }
        }     
    }
}

在我想将这些数据插入数据库之前,我必须检查此数据的验证,在Bobot TP,Bobot Jurnal,Bobot TA和Bobot Tubes的总和必须100个Minggu praktium(1,2,3,4,5,6,7,8,9,10)。圆柱Bobot Mingguan的总和必须100。我无法验证这个规则。我该如何验证?并且该规则的验证必须检查Minggu Praktikum(1,2,3,4,5,6,7,8,9,10)的每一行,并检查Bobot Mingguan列的总和必须为100。如果此规则为真,我必须仅插入数据(使用insert_batch)。我希望,您给我一个想法,如何解决这个问题。thx。

请添加 javascript 在您的表单中验证可能会帮助您进一步。

如果要重定向,如果存在一个错误,请使用重定向其他明智的定位收集错误并稍后显示。

使用模型文件中的插入批次插入值。

注意:请进一步阅读内联评论。

function save_set_bobot()
{
    $total=  $this->input->post('total');
    $kode_mp= $this->input->post('kode_mp');
   $data = array();
   $error = array();
    for($i=1;$i<=$total;$i++)
    {
        $bobot_tp[$i] =  $this->input->post("bobot_tp".$i);
        $bobot_jurnal[$i]=  $this->input->post("bobot_jurnal".$i);
        $bobot_ta[$i]=  $this->input->post("bobot_ta".$i);
        $bobot_tubes[$i]=  $this->input->post("bobot_tubes".$i);
        $bobot_mingguan[$i]=  $this->input->post("bobot_mingguan".$i);

        if($bobot_tp[$i]+$bobot_jurnal[$i]+$bobot_ta[$i]+$bobot_tubes[$i]!=100)
        {
            //error exist please collect the details into the array           
            $error["bobot_error".$i"] = "Total jumlah bobot TP, Jurnal, TA dan Tubes pada minggu praktikum $i harus 100"
            //If you want to redirect if one error exist please redirect here other wise collect the errors and display it later
            //$this->session->set_flashdata("error_msg","Total jumlah bobot TP, Jurnal, TA dan Tubes pada minggu praktikum $i harus 100");
            //redirect("admin/set_bobot2?matapraktikum=$kode_mp");
        }
        else
        {
            $prm  = array();
            $prm['kode_mp']=$kode_mp;
            $prm['minggu_praktikum']=$i;
            $prm['bobot_tp']=$bobot_tp[$i];
            $prm['bobot_jurnal']=$bobot_jurnal[$i];
            $prm['bobot_ta']=$bobot_ta[$i];
            $prm['bobot_tubes']=$bobot_tubes[$i];
            $prm['bobot_mingguan']=$bobot_mingguan[$i];
            //other wise move the values into the $data array
            $data[] = $prm;
        }     
    }
    //insert the values using insert batch in your model file
    //$this->db->insert_batch('Table Name', $data);
    //if you want to redirect at the end use like this
    if(count($error)>0) 
    {
      $this->session->set_flashdata("error_msg","Total jumlah bobot TP, Jurnal, TA dan Tubes pada minggu praktikum $i harus 100");
      redirect("admin/set_bobot2?matapraktikum=$kode_mp");
    }
}

相关内容

  • 没有找到相关文章

最新更新