load->view("admin/v_top_me-6ren">
gpt4 book ai didi

php - 如何使用 codeigniter 验证和插入多条记录

转载 作者:行者123 更新时间:2023-11-29 13:17:05 25 4
gpt4 key购买 nike

我使用 Codeigniter。我有一张 table 。

这是 View 的源代码

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

这是 Controller 的源代码

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) 中的每个 100。并且 Bobot Mingguan 列的总和必须为 100。我无法验证这条规则。我如何验证这一点?该规则的有效性必须检查 Minggu Praktikum (1,2,3,4,5,6,7,8,9,10) 的每一行,并检查 Bobot Mingguan 列的总和必须为 100。如果此规则为真,并且我必须仅通过一个操作插入数据(使用 insert_batch)。我希望,你给我一个想法,如何解决这个问题。谢谢。

最佳答案

请在您的表单中添加 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");
}
}

关于php - 如何使用 codeigniter 验证和插入多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21327224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com