CodeIgniter Forums
Multiple entry update and delete form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Multiple entry update and delete form (/showthread.php?tid=36320)

Pages: 1 2


Multiple entry update and delete form - El Forum - 11-28-2010

[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;
            }
    }



Multiple entry update and delete form - El Forum - 11-28-2010

[eluser]vitoco[/eluser]
the problem i guess it's in the controller :
Code:
...
        // 'team_id' index on $_POST doesn't exist
        /* in the view it's defined as
        &lt;input type="checkbox" name="team_id[&lt;? echo $i; ?&gt;]" value="&lt;? echo $team-&gt;team_id; ?&gt;"/>
        so you must get it like : $this->input->post('team_id'.$i);
        */
        $team_id = $this->input->post('team_id');

        // SHOW THE INDEXES IN POST THAT YOUR GETTING IN THE CONTROLLER LIKE THIS
        print_r( $_POST );
        // AND THEN ADJUST THE NAME TO WHAT YOU'RE RECEIVING
    ...
    }

Saludos


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]jpidgeon[/eluser]
Thanks for you're reply.

I've changed my controller but now get an error explaining that $i is undefined, and that a database error has occured.

New Controller:

Code:
function manage_team()
    {    
        $team_id = $this->input->post('team_id'.$i);
        print_r( $_POST );
          
        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');

    }

Error Message:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: i

Filename: controllers/admin.php

Line Number: 285
Array ( [team_name] => Array ( [0] => Barbara Streisand [1] => Ben Urazewski [2] => Brad Pantling [3] => Clare Mitchell [4] => Donna McGrellis [5] => Liam Swift [6] => Mandy Ryan [7] => Margaret Freeman [8] => Mike Horlock [9] => Stacey Miller ) [dept_name] => Array ( [0] => Inbound [1] => Outbound Bournemouth [2] => Outbound Plymouth [3] => Outbound Plymouth [4] => Outbound Bournemouth [5] => Outbound Bournemouth [6] => Operational Support [7] => Outbound Plymouth [8] => Outbound Bournemouth [9] => Business Services ) [owner1] => Array ( [0] => F8584993 [1] => F12341234 [2] => F7771737 [3] => F8584993 [4] => F12341234 [5] => F12341234 [6] => F38850504 [7] => F4937372 [8] => F12341234 [9] => F1578452 ) [owner2] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) [owner3] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => ) [team_id] => Array ( [0] => 28 [1] => 1 ) [delete] => Delete )
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

DELETE FROM `teams` WHERE `team_id` = Array



I understand that $i should be defined, but am not sure where or how to get this from the view?


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]Bart Mebane[/eluser]
Try this:

Controller
Code:
function manage_team()
{
    if ($this->input->post('team_id'))
    {
        foreach ($this->input->post('team_id') as $index => $id)
        {  
            if ($this->input->post('delete'))
            {
                $this->admin_model->delete_team($id);
            }
            else if ($this->input->post('update'))
            {
                $team = array();
                $team['team_name'] = $this->input->post("team_name[$index]");
                $team['owner1'] = $this->input->post("owner1[$index]");
                $team['owner2'] = $this->input->post("owner2[$index]");
                $team['owner3'] = $this->input->post("owner3[$index]");
                $this->admin_model->update_team($id, $team);
            }
        }
    }
    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, $team_data)
{
    $this->db->where('team_id', $team_id);
    $this->db->update('teams', $team_data);
}
This leaves out some steps, like validating input.


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]jpidgeon[/eluser]
Thanks for your reply.

The delete function work perfectly now, but the update sets all columns being updated to "0" on the specified row(s) when I submit the form


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]Bart Mebane[/eluser]
Can you post the code where the $fields array is being set for the form? Also, do a view source of the form page and be sure the text boxes have the correct names (team_name[0], etc.).


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]jpidgeon[/eluser]
This is the controller for my form 'team' view:

Code:
function teams($sort_by = 'team_name', $sort_order = 'asc', $offset = 0)
    {
        $limit = 10;
        $data['fields'] = array(
            'team_name' => 'Team Name',
            'dept_name' => 'Department',
            'owner1' => 'Primary Owner',
            'owner2' => 'Secondary Owner',
            'owner3' => 'Reserve Owner'
        );
        
        $data['dept_list'] = $this->admin_model->get_departments();
        $data['team_list'] = $this->admin_model->get_teams();
        
        $results = $this->admin_model->view_teams($limit, $offset, $sort_by, $sort_order);
        
        $data['teams'] = $results['rows'];
        $data['num_results'] = $results['num_rows'];
        
            // pagination
        $this->load->library('pagination');
        $config = array();
        $config['base_url'] = site_url("admin/teams/$sort_by/$sort_order");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        
        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;
            
            // field name, error message, validation rules
        $this->form_validation->set_rules('team', 'Team Name', 'trim|required');
        $this->form_validation->set_rules('department', 'Department', 'required');
        $this->form_validation->set_rules('owner1', 'Primary Owner', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('header');
            $this->load->view('admin/teams', $data);
            $this->load->view('footer');
        }
        
        else
        {            
            if($query = $this->admin_model->create_team())
            {    
                $status = array('status' => "Team Added Successfully");    
                redirect('/admin/teams', 'refresh');
                // $this->load->view('header', $status);
                // $this->load->view('admin/teams', $data);
                // $this->load->view('footer');
            }
            else
            {
                $this->load->view('header');
                $this->load->view('admin/teams', $data);
                $this->load->view('footer');
            }
        }
    
    }

When I look at my source code for my form view once output, all the input field names appear correct aswell

Code:
<tbody>
  <tr>
    <td>&lt;input type="text" name="team_name[0]" value="Ben Urazewski"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[0]" value="Inbound"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[0]" value="F5845124"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[0]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[0]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[0]" value="35"/&gt;&lt;/td>
  </tr>
  <tr>
    <td>&lt;input type="text" name="team_name[1]" value="Donna McGrellis"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[1]" value="Outbound Bournemouth"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[1]" value="F12341234"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[1]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[1]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[1]" value="3"/&gt;&lt;/td>
  </tr>
  <tr>
    <td>&lt;input type="text" name="team_name[2]" value="Liam Swift"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[2]" value="Outbound Bournemouth"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[2]" value="F12341234"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[2]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[2]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[2]" value="5"/&gt;&lt;/td>
  </tr>
  <tr>
    <td>&lt;input type="text" name="team_name[3]" value="Mandy Ryan"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[3]" value="Operational Support"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[3]" value="F38850504"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[3]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[3]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[3]" value="29"/&gt;&lt;/td>
  </tr>
  <tr>
    <td>&lt;input type="text" name="team_name[4]" value="Margaret Freeman"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[4]" value="Outbound Plymouth"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[4]" value="F4937372"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[4]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[4]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[4]" value="30"/&gt;&lt;/td>
  </tr>
  <tr>
    <td>&lt;input type="text" name="team_name[5]" value="Stacey Miller"/&gt;&lt;/td>
    <td>&lt;input type="text" name="dept_name[5]" value="Business Services"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner1[5]" value="F5845215"/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner2[5]" value=""/&gt;&lt;/td>
    <td>&lt;input type="text" name="owner3[5]" value=""/&gt;&lt;/td>
    <td class="edit-column">&lt;input type="checkbox" name="team_id[5]" value="34"/&gt;&lt;/td>
  </tr>
</tbody>



Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]Bart Mebane[/eluser]
Ok. One more thing to check: In your original post, part of your model code was
Code:
$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]')
                 );
That won't work, since php won't parse the variable name $i unless it's in a double-quoted string, like this:
Code:
$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]")
                 );
That part of the code, if you used what I posted earlier, would now be in the controller. Please check to be sure that you have double quotes around all the strings where you want the variables to be evaluated.


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]jpidgeon[/eluser]
I've done a check and double quotes only appear to be needed there in the controller.

It just doesn't look like the $team variable is being set. I've used print_r to check:

Code:
function manage_team()
    {
        if ($this->input->post('team_id'))
        {
            foreach ($this->input->post('team_id') as $index => $id)
            {  
                if ($this->input->post('delete'))
                {
                    $this->admin_model->delete_team($id);
                }
                else if ($this->input->post('update'))
                {
                    $team = array();
                    $team['team_name'] = $this->input->post("team_name[$index]");
                    $team['owner1'] = $this->input->post("owner1[$index]");
                    $team['owner2'] = $this->input->post("owner2[$index]");
                    $team['owner3'] = $this->input->post("owner3[$index]");
                    
                    print_r($team);
                    //$this->admin_model->update_team($id, $team);
                }
            }
        }
        //redirect('/admin/teams', 'refresh');
    }

and just get:

Quote:Array ( [team_name] => [owner1] => [owner2] => [owner3] => )

Both $id and $index are being set fine but the post field values are not being grabbed. I've checked again on my view and all the field names are correct.


Multiple entry update and delete form - El Forum - 11-29-2010

[eluser]Bart Mebane[/eluser]
Hmmm ... try
Code:
print_r ($this->input->post('team_name'));
outside of the foreach loop and see what you get.