• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete a column from a database

#1
[eluser]zend[/eluser]
Hello everyone.
I have a problem with a button to delete.
How to make a call ID that is not in foreac loop?

I want that button to be outside table where we sort the results from the database.
The problem is because the button where I want to be outside the loop foreac and shows me an error ...

I set the screenshot and see how I imagined, I want that button to be above and not in each column.

my VIEW is HERE

Code:
/// I whant this use delete button
         <a >id; ?&gt;">Delite</a></span>

    <tbody>
    &lt;?php foreach ($query as $row):?&gt;    
    <tr>
      <td>&lt;?php echo $row->id;?&gt;</td>
          <td>&lt;?php echo $row->title;?&gt;</td>
          <td>&lt;?php echo $row->author;?&gt;</td>
          <td>&lt;?php echo $row->parent;?&gt;</td>
          <td>&lt;?php echo $row->date_created;?&gt;</td>

          /// NOT THIS IN TABLE
      <td><a >id; ?&gt;">Delite</a></td>
    </tr>
    &lt;?php endforeach?&gt;
  </tbody>

#2
[eluser]Victor Michnowicz[/eluser]
I think you will need to use JavaScript to grab the ID of the rows that are checked. Then you can harness the amazing power of AJAX to POST all those values to a controller method that will delete those items.

View:
Code:
// In your foreach loop...
&lt;input type="checkbox" name="item[]" value="&lt;?php echo $row-&gt;id; ?&gt;" />

JavaScript:
Code:
$('#delete').click(function() {

    var rows_to_delete = $('#my_table_id').serilize();

    $.post('my_controller/delete', rows_to_delete, function(data) {
        // Do some cool stuff like remove the rows
    });

});

Controller:
Code:
public function delete()
{
    $rows_to_delete = $this->input->post('item');

    // Delete them
}

I'm probably forgetting a lot of stuff, but that should get you started in a (possibly right) direction.

#3
[eluser]zend[/eluser]
I dont understand how to use this?? Is dont work with me..

My Controoller
Code:
public function delite_page($id)
{
   $this->Page_Model->delitePage($id);  
   $rows_to_delete = $this->input->post('item');      
   redirect('page/', 'refresh');      
}

Model
Code:
public function delitePage($id)
{
  $this->db->where('id', $id);
  $this->db->delete('pages');
}

View
Code:
SCRIPT LANGUAGE=JavaScript  
$('#delete').click(function() {

    var rows_to_delete = $('107').serilize();

    $.post('page/delite_page/', rows_to_delete, function(data) {
        // Do some cool stuff like remove the rows
    });

});  
/SCRIPT

<a href="page/delite_page/"> Delete </a>

&lt;?php foreach ($query as $row):?&gt;
<tr>
   <td>&lt;input type="checkbox" name="item[]" value="&lt;?php echo $row-&gt;id; ?&gt;" /></td>
   <td>&lt;?php echo $row->id;?&gt;</td>
   <td>&lt;?php echo $row->title;?&gt;</td>
   <td>&lt;?php echo $row->author;?&gt;</td>
   <td>&lt;?php echo $row->parent;?&gt;</td>
   <td>&lt;?php echo $row->date_created;?&gt;</td>
</tr>
&lt;?php endforeach?&gt;

Dont work i dont know whay? Please help me i dont udnerstand.
Sorry my English is bad.

#4
[eluser]Victor Michnowicz[/eluser]
My code was meant as more of a guide - it was not meant to work right away. You will have to do some more work to get it functional inside your application.

I was also using jQuery for my JavaScript code. I probably should have mentioned that.

You should check out jQuery serilize(). This will help you with what you are trying to do.

#5
[eluser]zend[/eluser]
I tried the examples from the site and I get the output parameter
page = 1

My link should be
my_site.com/page/delete/1

How do I send the parameter ID to my controller and model

#6
[eluser]zend[/eluser]
I succeeded in their own way with a simple JavaScript
Code:
function validate(form) {
var namelist = "";
with(document.form1) {
for(var i = 0; i < names.length; i++){
if(names[i].checked) {
namelist += names[i].value + "\n";
}
}
}
if(namelist == "") {
alert("select names");
} else {
[removed] = "page/delite_page/"+namelist;
}
return false;
}

I grab id and put in url
+namelist is grabed id
Code:
[removed] = "page/delite_page/"+namelist;

<a href=" ">Delete</a>

Controller
public function delite_page($id)
    {
       $this->Page_Model->delitePage($id);
       redirect('page/','refresh');
    }

Model
public function delitePage($id)
{
$this->db->where('id', $id);
$this->db->delete('pages');
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.