codeigniter的新手,并尝试从用户表单中更新已检查的行。
我的视图生成一个包含MySQL数据的表单,如下所示:
<?php
echo form_open('masterdata/update_customers');
?>
<table>
<tr>
td> </td><td>Customer Name</td><td>postalcode</td>
<tr>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">
</td>
<td>
<input type="text" name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >
</td>
<td>
<input type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
</td>
</tr>
<?php endforeach ; ?>
</table>
<input type="submit" value="Update Selected">
<?php else : ?>
<h2> No Records Found</h2>
<?php endif; ?>
<?php echo form_close(); ?>
这非常有效,因为我获得了所有输入字段的唯一名称和值。
我的问题是现在尝试将选中的复选框和值发送到codeigniter并让它更新每一行。
传统上我会使用foreach($_POST['editcustomer'] as $editcustomer){
但是无法在codeigniter中解决这个问题。
我的控制器功能,update_customers
在这个阶段是非常基本的:
function update_customers()
{
$this->form_validation->set_rules("customer_name","`Customer Name`","required|min_length[6]|xss_clean");
$this->form_validation->set_rules("postalcode","`Postal Code`","required|xss_clean|min_length[6]");
if ($this->form_validation->run() == FALSE){
$data["message"]="";
$data['title']="Master Data Home Page";
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_manage_customers");
$this->load->view("master_data/view_master_data_footer");
} else {
$data = array(
'customer_name' => $this->input->post('customer_name'),
'postalcode' => $this->input->post('postalcode'),
);
$this->model_master_data->update_customer_records($data);
$this->customers_updated();
}
}
我的模型函数update_customer_records
是:
function update_customer_records($data)
{
$this->db->where('id',$this->input->post('id'));
$this->db->update('customers',$data);
}
我知道这里有相当多的缺失,比如只处理检查的行。但不知道如何添加它。其次,根据我的观点生成我的唯一名称和ID:
name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"
如何让更新应用于唯一表单输入名称?我需要将前缀customername_和postalcode_附加到input-&gt; post(?)值?
此处的任何指示或协助将不胜感激。提前感谢一百万。
答案 0 :(得分:1)
使用CodeIgniter,如果必须获取复选框值,则必须使用$ this-&gt; input-&gt; post('name_of_your_checkbox')。它返回一个数组。
例如,如果你在视图中有这个:
<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />
来自控制器:
public function update_customers() {
$editcustomer = $this->input->post('editcustomer');
}
返回:
Array {
[0] => 1
[1] => 2
[2] => 3
}