Question

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.

Was it helpful?

Solution

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top