【问题标题】:How to validate and insert multiple record using codeigniter如何使用 codeigniter 验证和插入多条记录
【发布时间】:2014-01-24 07:51:36
【问题描述】:

我使用 Codeigniter。我有一张桌子。

这是查看源代码

<?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)。 我希望,你给我一个想法,如何解决这个问题。谢谢。

【问题讨论】:

    标签: php mysql arrays codeigniter multiple-records


    【解决方案1】:

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

    如果您想在存在一个错误时进行重定向,请使用重定向其他方式收集错误并稍后显示。

    在模型文件中使用插入批处理插入值。

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

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

    【讨论】:

    • 哦,我明白了,我必须在数组中收集错误和 $prm。但是Bobot Mingguan柱子呢?我怎样才能对列的这个值求和?这个值也必须是 100。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2015-10-04
    • 2015-05-10
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多