【问题标题】:Codeigniter - update table from form with checkboxCodeigniter - 使用复选框从表单更新表格
【发布时间】:2013-03-02 21:09:22
【问题描述】:

我正在尝试使用Codeigniter 更新MySQL 表。

我的型号代码是:

function update_customer_records($updatedrow)
{
    $this->db->where('id',$this->input->post('id'));
    $this->db->update('customers',$updatedrow);
}

我的看法是:

    $attributes=array(
        'name'=>'updatecustomer',
        'id'=>'updatecustomer',
        );
    echo form_open('masterdata/manage_customers',$attributes);
?>

<table>
    <tr>
        <td>&nbsp;</td><td>&nbsp;</td><td>Customer Name</td><td>postalcode</td>
    <tr>

<?php if(isset($records)) : foreach ($records as $row) : ?>
    <tr>
        <td>
<?php echo anchor('masterdata/customers_updated/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>
        </td>
        <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="customername_<?php echo $row->id ?>" id="customername_<?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(); ?>

我的控制器是:

function manage_customers()
    {

        $data['title']="Manage Customers";

            //query model to get data results for form
            $data=array();

            if($query=$this->model_master_data->get_records()){
                $data['records']=$query;


            $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",$data);
            $this->load->view("master_data/view_master_data_footer");


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

            if(isset($editcustomer)){

                //begin outputting id of selected checkbox
                foreach ($editcustomer as $row) :
                    echo $row;

                $updatedrow=array(
                    'id'=>$row,
                    'postalcode'=>'44444'
                    );


                $this->model_master_data->update_customer_records($updatedrow);

                endforeach;
            }

我有两个问题:

  1. 如果未选中复选框,如何停止 foreach 运行。
  2. 如何正确地将数组传递给模型以便更新运行?

一如既往地提前致谢。

【问题讨论】:

    标签: forms codeigniter checkbox updates multiple-records


    【解决方案1】:

    首先,我在表单中找到了两个具有相同名称和 ID(如下所示)的字段,在一个字段中设置其值 customer_name,在另一个字段中设置 postalcode

    <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="customername_<?php echo $row->id ?>" id="customername_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >
                                    --^^--
    </td>
    

    所以我认为(可能)第二个字段的名称和 id 应该是 postalcode 根据它的值。

    您也不必担心foreach 循环,因为循环内的代码只有在表单上有选中的复选框时才会运行,因为不会提交未选中的复选框,但您可以选中并运行使用以下代码循环

    if( $this->input->post('editcustomer') != false )
    {
        foreach ($editcustomer as $row)
        {
            // code here
        }
    }
    

    如果editcustomer 未找到或未与表单一起提交,if 条件将返回 false。您的表单中也没有id 字段,在这种情况下您不能使用$this-&gt;input-&gt;post('id'),因此如果您需要检查模型的where clause 中的复选框ID,那么您可以使用

    在控制器中:

    if( $this->input->post('editcustomer') != false )
    {
        $this->load->model('model_master_data');
        foreach ($editcustomer as $row_id)
        {
            $data = array( 'postalcode' => '44444' );
            $this->model_master_data->update_customer_records( $row_id, $data );
        }
    }
    

    我认为您不需要传递'id'=&gt;$row,,因为您可能不想更新此字段。另外,在更新记录之前,您应该使用form validation检查表单输入(您可以设置绑定用户输入邮政编码所需的邮政编码字段)。

    在模型中:

    function update_customer_records( $id, $data )
    {
        $this->db->where( 'id', $id );
        $this->db->update( 'customers', $data );
    }
    

    所以它会做这样的事情(伪代码)

    update the customers table set `postalcode` = '44444' where `id` = $id
    

    更新: 我想你也可以使用update_batch

    在控制器中:

    if( $this->input->post('editcustomer') != false )
    {
        $data = array();
        foreach ($editcustomer as $row_id)
        {
            $data[] = array( 'id' => $row_id, 'postalcode' => '44444';
        }
        $this->load->model('model_master_data');
        $this->model_master_data->update_customer_records( $data );
    }
    

    在模型中:

    function update_customer_records( $data )
    {
        $this->db->update_batch('customers', $data, 'id');
    }
    

    【讨论】:

    • 嗨,Sheikh,非常感谢您的解释。
    • @Smudger,很高兴知道 :-)
    【解决方案2】:

    "Codeigniter update mysql table from form with CI" 重复的主题?

    如果未选中复选框,我如何阻止 foreach 运行。

    如果未选中复选框,则不必停止 foreach 运行。 因为$editcustomer 变量只包含选中的复选框。

    如何正确地将数组传递给模型以便更新运行?

    你的模型是错误的。应该是:

    public function update_customer_records($updatedrow)
    {
        $this->db->update('customers', $updatedrow);
    }
    

    你不需要写$this-&gt;db-&gt;where('id',$this-&gt;input-&gt;post('id'));(这是错误的,因为你不能在你的模型中使用$this->input->post())因为你已经在$updaterow中传递了id

    编辑:对不起,我读你的代码太快了!

    function update_customer_records($updatedrow)
    {
        $this->db->where('id',$this->input->post('id'));
        $this->db->update('customers',$updatedrow);
    }
    

    ...是正确的。或者,您可以用更短的方式编写它:

    function update_customer_records($updatedrow)
    {
        $this->db->update('customers', $updatedrow, array('id' => $this->input->post('id')));
    }
    

    【讨论】:

    • 你能告诉我为什么OP不能在模型中使用$this-&gt;input-&gt;post('id')吗?
    • 这(技术上)是可能的,但这不是最佳实践。从控制器访问 $this->input->post() 更加灵活,并且更符合 MVC 设计模式(在我看来)。
    • 我问是因为你写了you can't use :-)
    • 我的错!我应该解释为什么最好不要在模型中直接使用 $this->input->post() ,而不是仅仅写它是不可能的。 PS:对不起我的英语不好!
    猜你喜欢
    • 2013-02-17
    • 2013-11-15
    • 1970-01-01
    • 2023-03-18
    • 2017-01-02
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多