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

Php 如何使用codeigniter验证和插入多个记录,php,mysql,arrays,codeigniter,multiple-records,Php,Mysql,Arrays,Codeigniter,Multiple Records,我用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

我用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");?>


请在您的表单中添加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");
    }
}
function save\u set\u bobot()
{
$total=$this->input->post('total');
$kode\u mp=$this->input->post('kode\u mp');
$data=array();
$error=array();
对于($i=1;$iinput->post(“bobot\u tp”。$i);
$bobot\u jurnal[$i]=$this->input->post(“bobot\u jurnal”$i);
$bobot_ta[$i]=$this->input->post(“bobot_ta”$i);
$bobot_tubes[$i]=$this->input->post(“bobot_tubes”$i);
$bobot_明管[$i]=$this->input->post(“bobot_明管”$i);
如果($bobot_tp[$i]+$bobot_jurnal[$i]+$bobot_ta[$i]+$bobot_Tube[$i]!=100)
{
//存在错误。请将详细信息收集到阵列中
$error[“bobot_error”。$i”]=“在明谷项目中,总管道数量为$i,共100”
//如果存在一个错误,如果要重定向,请在此处重定向,以其他方式收集错误并稍后显示
//$this->session->set_flashdata(“error_msg”,“Total jumlah bobot TP,Jurnal,TA dan tube pada minggu praktikum$i harus 100”);
//重定向(“admin/set_bobot2?matapraktikum=$kode_mp”);
}
其他的
{
$prm=数组();
$prm['kode\u mp']=$kode\u 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];
//另一种方法是将值移动到$data数组中
$data[]=$prm;
}     
}
//在模型文件中使用“插入批处理”插入值
//$this->db->insert_batch('Table Name',$data);
//如果你想在最后重定向,像这样使用
如果(计数($error)>0)
{
$this->session->set_flashdata(“error_msg”,“Total jumlah bobot TP,Jurnal,TA dan tube pada minggu praktikum$i harus 100”);
重定向(“admin/set_bobot2?matapraktikum=$kode_mp”);
}
}

哦,我明白了,我必须收集数组中的错误和$prm。但是Bobot Mingguan列呢?我如何计算该列值的总和?该值也必须为100。Thx
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");
    }
}