我使用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的总和必须为每个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。
答案 0 :(得分:1)
请在表单中添加 javascript 验证,这可能会对您有所帮助。
如果您想重定向,如果存在一个错误,请使用redirect other wise收集错误并稍后显示。
使用插入批处理在模型文件中插入值。
注意:请进一步阅读内联评论。
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");
}
}