Welcome Guest, Not a member yet? Register   Sign In
Multiple row update
#1

[eluser]PHP Creative[/eluser]
Hi there

I was wondering if anybody could point me in the right direction to update Multiple rows in Codigniter. Just a link or explanation of what to do?

View >>>

Code:
<? echo form_open("".current_url().""); ?>
<table>
    <thead>
        <th>ID</th>
        <th>First Name</th>
        <th>Surname</th>
    </thead>
    &lt;?php foreach ($query->result() as $row){ ?&gt;
    <tr>
        <td>&lt;? echo $row->ID; ?&gt;&lt;? echo form_hidden('ID[]',$row->ID); ?&gt;</td>
        <td>&lt;? echo form_input('FirstName[]',$row->FirstName); ?&gt;</td>
        <td>&lt;? echo form_input('Surname[]',$row->Surname); ?&gt;</td>
    </tr>
    &lt;? } ?&gt;
</table>
&lt;? echo form_submit('Update', 'Edit'); ?&gt;

&lt;? echo form_close() ?&gt;


Bit stuck on what to do next. Got the post array but it doesn't make any sense (keys etc). Any help would be kindly appreciated.

Controller >>


Code:
function Update() {

print_r($_POST);

}

Thanks a million!
#2

[eluser]BrianJM[/eluser]
Please post the output of
Code:
print_r($_POST);
#3

[eluser]cahva[/eluser]
Roughly something like this:
Code:
function Update()
{

    if ($this->input->post('ID'))
    {
        foreach($_POST['ID'] as $k=>$v)
        {
            $this->db->update('my_table',
                array(
                    'FirstName' => $_POST['FirstName'][$k],
                    'Surname' => $_POST['Surname'][$k]
                ),
                array('ID' => (int) $v)
            );
        }
    }
    
    // Load some view or do something else
    
}

This is just an example and you should not use it like that. Theres no input checking and you should pass those values to your model which would update the database. But hopefully you'll get the idea.
#4

[eluser]PHP Creative[/eluser]
Hi there!

Thank a million for that! Gives me a good idea.

I was wondering if that it all possible?


Quote: foreach($_POST['FleetID'] as $k=>$v)
{



$rules['ID['.$k.']'] = "required";
$rules['FirstName['.$k.']'] = "required";



}

Thanks a million
#5

[eluser]cahva[/eluser]
No, you cannot do that if you are defining rules to form validation. You only have to create the rules like this:
Code:
$config = array(
    array(
        'field' => 'FirstName[]',
        'label' => 'First Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'LastName[]',
        'label' => 'Last Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'ID[]',
        'label' => 'ID',
        'rules' => 'required'
    )
);

$this->form_validation->set_rules($config);




Theme © iAndrew 2016 - Forum software by © MyBB