Codeigniter更新mysql表数据从表单与复选框 [英] Codeigniter update mysql table data from form with checkbox

查看:221
本文介绍了Codeigniter更新mysql表数据从表单与复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的视图生成一个包含MySQL数据的表单,如下所示:

 <?php 
echo form_open('masterdata / update_customers');
?>

< table>
< tr>
td>& nbsp;< / td>< td>客户名称< / td>< td>邮政编码< / 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 =textname =customername_<??php echo $ row-> id?> id =customername_<?php echo $ row-> id?> value =<?php echo $ row-> customer_name;?> >
< / td>
< td>
< input type =textname =postalcode_<?php echo $ row-> id?> id =postalcode_<?php echo $ row-> id?> value =<?php echo $ row-> postalcode;?> >
< / td>
< / tr>
<?php endforeach; >
< / table>
< input type =submitvalue =更新所选项>
<?php else:?>
< h2>找不到记录< / h2>
<?php endif; >
<?php echo form_close(); >

这样的效果非常好,因为我获得了所有输入字段的唯一名称和值。



我的问题现在尝试传递所选的复选框和值到codeigniter,并让它更新每一行。



我会使用 foreach($ _ POST ['editcustomer']作为$ 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'] =主数据首页;
$ 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-> post(?)值?



这里的任何方向或帮助将不胜感激。感谢一百万。

解决方案

对于CodeIgniter,如果你必须获取复选框值,你必须使用$ this-> input-> post('name_of_your_checkbox')。它返回一个数组。



例如,如果你有一个视图:

 code>< input type =checkboxname =editcustomer []value =1/> 
< input type =checkboxname =editcustomer []value =2/>
< input type =checkboxname =editcustomer []value =3/>

从控制器:

  public function update_customers(){

$ editcustomer = $ this-> input-> post('editcustomer');

}

返回:

  Array {
[0] => 1
[1] => 2
[2] => 3
}


New to codeigniter and trying to get my head around updating checked rows from a user form.

My view generates a form with MySQL data as below:

<?php 
    echo form_open('masterdata/update_customers');
?>

<table>
    <tr>
        td>&nbsp;</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(); ?>

This works perfectly well as I get my unique name and values for all input fields.

My issue is now trying to pass the selected checkboxes and there values to codeigniter and get it to update each row.

traditionally I would use foreach($_POST['editcustomer'] as $editcustomer){ but cant get my head around this in codeigniter.

my controller function, update_customers at this stage is very basic:

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

My model function, update_customer_records is:

function update_customer_records($data)
{

        $this->db->where('id',$this->input->post('id'));
    $this->db->update('customers',$data);


}

I know there is quite a bit missing here, like processing only the rows checked. but not sure how to add this in. secondly, my unique name and id's being generated, as per my view is: name="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>"

how do get the update to apply to the unique form input name? I need to append the prefix customername_ and postalcode_ to the input->post(?) value?

Any direction or assistance here will be appreciated. thanks a million in advance.

解决方案

With CodeIgniter, if you have to get checkbox values, you have to use $this->input->post('name_of_your_checkbox'). It's return an array.

For example, if you have that in a view:

<input type="checkbox" name="editcustomer[]" value="1" />
<input type="checkbox" name="editcustomer[]" value="2" />
<input type="checkbox" name="editcustomer[]" value="3" />

From the controller:

public function update_customers() {

    $editcustomer = $this->input->post('editcustomer');

}

Return:

Array {
    [0] => 1
    [1] => 2
    [2] => 3
}

这篇关于Codeigniter更新mysql表数据从表单与复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆