Welcome Guest, Not a member yet? Register   Sign In
Multiple entry update and delete form
#1

[eluser]jpidgeon[/eluser]
Hi guys

I'm pretty new to CodeIgniter and seem to be having trouble getting my multiple update/delete form function to work properly.

I have multiple rows of data pulled from the database which populate my form. I want to use the checkbox to define which rows are updated or deleted when the form gets sent to my controller function 'manage_team'.

I've looked through the forum at some other posts and managed to pull together the basic code - with each row in my view successfully being given unique names for each input field through the for $i loop.

However when I tick a row and select a either Update or Delete, nothing seems to happen and I get returned to the same page. Everything appears to be set correctly in my view, I'm just unsure whether my controller and model are handling everything correctly?

Any help would be much appreciated, Thanks

My View:

Code:
<?php echo form_open('admin/manage_team'); ?>
<table>
    <thead>
        &lt;?php foreach($fields as $field_name => $field_display): ?&gt;
        <th &lt;?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?&gt;>
            &lt;?php echo anchor("admin/teams/$field_name/" .
                (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
                $field_display); ?&gt;
        </th>
        &lt;?php endforeach; ?&gt;
        <th></th>
    </thead>
    
    <tbody>
        &lt;?php $i = 0;  foreach($teams as $team): ?&gt;
        <tr>
            &lt;?php foreach($fields as $field_name => $field_display): ?&gt;
            <td>
                &lt;input type="text" name="&lt;?php echo $field_name; ?&gt;[&lt;? echo $i; ?&gt;]" value="&lt;?php echo $team-&gt;$field_name; ?&gt;"/>
            </td>
            &lt;?php endforeach; ?&gt;
            <td class="edit-column">&lt;input type="checkbox" name="team_id[&lt;? echo $i; ?&gt;]" value="&lt;? echo $team-&gt;team_id; ?&gt;"/></td>
        </tr>
        &lt;?php ++$i; endforeach; ?&gt;            
    </tbody>
    
</table>

<div class="form-footer">
        <img src="/resources/images/shared/icons/info.png"/>
        <p> There are &lt;?php echo $num_results; ?&gt; teams</p>
        
        &lt;?php if (strlen($pagination)): ?&gt;
        <span class="float-right"><p>Pages: &lt;?php echo $pagination; ?&gt;</p></span>
        &lt;?php endif; ?&gt;
        &lt;input name="update" type="submit" class="button default" value="Update"&gt;
        &lt;input name="delete" type="submit" class="button default" value="Delete"&gt;

</div>
&lt;/form&gt;

Controller

Code:
function manage_team()
    {    
        $team_id = $this->input->post('team_id');
          
        if ($this->input->post('delete'))
        {
                $this->admin_model->delete_team($team_id);
         }
        else if ($this->input->post('update'))
        {
                $this->admin_model->update_team($team_id);
        }
        redirect('/admin/teams', 'refresh');

    }

Model

Code:
function delete_team($team_id)
    {
        $this->db->where('team_id', $team_id);
        $this->db->delete('teams');
    }
    
    function update_team($team_id)
    {
        $i = 0;
        $size = count($this->input->post('team_id'));
            while ($i < $size)
            {
                $update_data = array(
                    'team_name' => $this->input->post('team[$i]'),
                    'owner1' => $this->input->post('owner1[$i]'),
                    'owner2' => $this->input->post('owner2[$i]'),
                    'owner3' => $this->input->post('owner3[$i]')
                );
                
                $this->db->where('team_id', $this->input->post('team_id[$i]'));
                $this->db->update('teams', $update_data);
                ++$i;
            }
    }


Messages In This Thread
Multiple entry update and delete form - by El Forum - 11-28-2010, 08:40 AM
Multiple entry update and delete form - by El Forum - 11-28-2010, 12:04 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 06:54 AM
Multiple entry update and delete form - by El Forum - 11-29-2010, 07:53 AM
Multiple entry update and delete form - by El Forum - 11-29-2010, 08:31 AM
Multiple entry update and delete form - by El Forum - 11-29-2010, 12:04 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 01:04 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 03:21 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 04:17 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 04:29 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 04:33 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 04:39 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 04:54 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 05:02 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 05:10 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 05:22 PM
Multiple entry update and delete form - by El Forum - 11-29-2010, 05:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB