CodeIgniter Forums
deleting a row using checkbox - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: deleting a row using checkbox (/showthread.php?tid=27006)



deleting a row using checkbox - El Forum - 01-29-2010

[eluser]sasori[/eluser]
hi, can you guys help me develope the logic for this? yes i mean code snippets
here's my controller code
Code:
public function members_area()
  {
    $this->load->model('member_model');
    $data['q'] = $this->member_model->get_urls();
    $data['title'] = "Members Area";
    $data['main_content'] = 'includes/member_view';
    $this->load->view('includes/template',$data);
  }

here's my model code

Code:
public function get_urls()
  {
    $q = $this->db->get('bookmark');
    return $q;
  }

here's my view code

Code:
<table>
&lt;?php form_open('site/delete'); ?&gt;
<tr><td><b>Bookmark</b></td><td><b>Delete</b></td></tr>
&lt;?php foreach($q->result() as $row):?&gt;
        &lt;?php echo "<tr>"; ?&gt;
        &lt;?php echo "<td>".anchor($row->bm_URL,$row->bm_URL)."</td>"; ?&gt;
        &lt;?php echo "<td>".form_checkbox('delete',$row->bm_URL)."</td>"; ?&gt;
        &lt;?php echo "</tr>"; ?&gt;
&lt;?php endforeach; ?&gt;
</table>

here's the initial look
Code:
http://i47.tinypic.com/24vp81v.jpg

if am gonna place a delete link or button at the bottom, how will i make the "checked"
box be deleted ?


deleting a row using checkbox - El Forum - 01-29-2010

[eluser]saidai jagan[/eluser]
name the check box as array ex.
Code:
&lt;input type=checkbox name=delete[] value=row-&gt;id />
submit the form and get the checked check box values in controller by
Code:
print_r($this->input->post(delete));



deleting a row using checkbox - El Forum - 01-29-2010

[eluser]sasori[/eluser]
hi, the delete is working now, but i got a new problem
all the rows gets deleted even if i checked only 1 box LOL
here's my controller code

Code:
public function delete()
  {
    $url = $this->input->post('delete');
    if(isset($url))
    {
      $this->member_model->delete();
      $this->members_area();
    }
    else
    {
      redirect('site/members_area');
    }
  }

here's my new model function

Code:
public function delete()
  {
    $this->db->where('bm_URL',$this->input->post('delete'));
    $q = $this->db->delete('bookmark');
    return $q;
  }

and here's my view code

Code:
&lt;?php form_open('site/delete'); ?&gt;
<tr><td><b>Bookmark</b></td><td><b>Delete</b></td></tr>
&lt;?php foreach($q->result() as $row):?&gt;
        &lt;?php echo "<tr>"; ?&gt;
        &lt;?php echo "<td>".anchor($row->bm_URL,$row->bm_URL)."</td>"; ?&gt;
        &lt;?php echo "<td>".form_checkbox('delete[]',$row->bm_URL)."</td>"; ?&gt;
        &lt;?php echo "</tr>"; ?&gt;
&lt;?php endforeach; ?&gt;
&lt;?php echo form_close(); ?&gt;
</table>
<br />
<hr />
&lt;?php echo anchor('site','Home'); ?&gt;  |  &lt;?php echo anchor('site/addbm','Add BM'); ?&gt;  |
&lt;?php echo anchor('site/delete','Delete BM'); ?&gt;   |   &lt;?php echo anchor('user/changepass','Change Password'); ?&gt;<br />
&lt;?php echo anchor('site/recommend','Recommend URLs to me'); ?&gt;   |   &lt;?php echo anchor('user/logout','Log out'); ?&gt;<br />



deleting a row using checkbox - El Forum - 01-29-2010

[eluser]saidai jagan[/eluser]
show the
Code:
print_r($this->input->post(delete));



deleting a row using checkbox - El Forum - 01-29-2010

[eluser]sasori[/eluser]
I added
Code:
&lt;?php echo print_r($this->input->post('delete')); ?&gt;
at the bottom of the view
and it prints "1"


deleting a row using checkbox - El Forum - 01-29-2010

[eluser]Rob Gordijn[/eluser]
try

Code:
&lt;?php echo print_r($this->input->post('delete'),1); ?&gt;



deleting a row using checkbox - El Forum - 01-29-2010

[eluser]sasori[/eluser]
[quote author="Rob Gordijn" date="1264784921"]try

Code:
&lt;?php echo print_r($this->input->post('delete'),1); ?&gt;
[/quote]

i think you mean
Code:
echo print_r($this->input->post('delete',1));

the output is the same, it prints 1


deleting a row using checkbox - El Forum - 02-20-2010

[eluser]radomir_radojevic[/eluser]
Hi ninja Smile

try this

model function:

Code:
public function delete()
{
                
   $delete = $this->input->post('delete');                
    for($i=0;$i<count($delete);$i++) {
    $del_bm_URL = $delete[$i];
    $this->db->where('bm_URL',$del_bm_URL);
        $this->db->delete('bookmark');        
        }                        
  }



deleting a row using checkbox - El Forum - 02-20-2010

[eluser]sasori[/eluser]
thanks for the reply Mr. radomir, I guess you're a real ninja in PHP haha.


deleting a row using checkbox - El Forum - 02-12-2011

[eluser]zend[/eluser]
This to me does not work ... I use the whole code here but the theme will not work ... Not deleted