[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>
<?php foreach($fields as $field_name => $field_display): ?>
<th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("admin/teams/$field_name/" .
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
$field_display); ?>
</th>
<?php endforeach; ?>
<th></th>
</thead>
<tbody>
<?php $i = 0; foreach($teams as $team): ?>
<tr>
<?php foreach($fields as $field_name => $field_display): ?>
<td>
<input type="text" name="<?php echo $field_name; ?>[<? echo $i; ?>]" value="<?php echo $team->$field_name; ?>"/>
</td>
<?php endforeach; ?>
<td class="edit-column"><input type="checkbox" name="team_id[<? echo $i; ?>]" value="<? echo $team->team_id; ?>"/></td>
</tr>
<?php ++$i; endforeach; ?>
</tbody>
</table>
<div class="form-footer">
<img src="/resources/images/shared/icons/info.png"/>
<p> There are <?php echo $num_results; ?> teams</p>
<?php if (strlen($pagination)): ?>
<span class="float-right"><p>Pages: <?php echo $pagination; ?></p></span>
<?php endif; ?>
<input name="update" type="submit" class="button default" value="Update">
<input name="delete" type="submit" class="button default" value="Delete">
</div>
</form>
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;
}
}