php 如何删除codeigniter中的特定行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19700582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:53:27  来源:igfitidea点击:

how to delete a specific row in codeigniter?

phpcodeigniter

提问by Abhijit

I am new in codeigniter.In my view page I am showing the data from database in a table where I have two anchor tags for update and delete. I want to delete a specific row from database through id.

我是 codeigniter 的新手。在我的视图页面中,我在表中显示数据库中的数据,其中有两个用于更新和删除的锚标记。我想通过 id 从数据库中删除特定行。

my view page is

我的查看页面是

 <?php foreach($query as $row){ ?>
 <tr>
 <td><?php echo $row->name  ?></td>
  <td><?php echo $row->testi  ?></td>
  <td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
  <td><a href="#ajax-modal2" data-id="delete[]" role="button" class="Delete" data-toggle="modal" onClick="confirm_delete()"><i class="icon-trash"></i></a></td>
  </tr>

<?php } ?>
</table>

my controller page is

我的控制器页面是

function delete_row()
{

   $this->load->model('mod1');
   $this->mod1->row_delete();
   redirect($_SERVER['HTTP_REFERER']);  
}

and my model page is

我的模型页面是

 function row_delete()
  {

      $this->db->where('id', $id);
      $this->db->delete('testimonials'); 
  }

I want to delete the row by catching the respective id. Please dont be harsh. thank you

我想通过捕获相应的 id 来删除该行。请不要苛刻。谢谢你

回答by Jeemusu

You are using an $idvariable in your model, but your are plucking it from nowhere. You need to pass the $idvariable from your controller to your model.

$id在模型中使用了一个变量,但您是从无处获取它的。您需要将$id变量从控制器传递给模型。

Controller

控制器

Lets pass the $id to the model via a parameter of the row_delete()method.

让我们通过row_delete()方法的参数将 $id 传递给模型。

function delete_row()
{
   $this->load->model('mod1');

   // Pass the $id to the row_delete() method
   $this->mod1->row_delete($id);


   redirect($_SERVER['HTTP_REFERER']);  
}

Model

模型

Add the $id to the Model methods parameters.

将 $id 添加到模型方法参数中。

function row_delete($id)
{
   $this->db->where('id', $id);
   $this->db->delete('testimonials'); 
}

The problem now is that your passing the $idvariable from your controller, but it's not declared anywhere in your controller.

现在的问题是您$id从控制器传递变量,但它没有在控制器中的任何地方声明。

回答by Levin

a simple way:

一个简单的方法:

in view(pass the id value):

在视图中(传递 id 值):

<td><?php echo anchor('textarea/delete_row?id='.$row->id, 'DELETE', 'id="$row->id"'); ?></td>

in controller(receive the id):

在控制器中(接收 ID):

$id = $this->input->get('id');
$this->load->model('mod1');
$this->mod1->row_delete($id);

in model(get the passed args):

在模型中(获取传递的参数):

function row_delete($id){}

Actually, you should use the ajax to POST the id value to controller and delete the row, not the GET.

实际上,您应该使用 ajax 将 id 值发布到控制器并删除行,而不是 GET。

回答by Hemant Randive

**multiple delete not working**

function delete_selection() 
{
        $id_array = array();
        $selection = $this->input->post("selection", TRUE);
        $id_array = explode("|", $selection);

        foreach ($id_array as $item):
            if ($item != ''):
                //DELETE ROW
                $this->db->where('entry_id', $item);
                $this->db->delete('helpline_entry');
            endif;
        endforeach;
    }

回答by Waseem shah

It will come in the url so you can get it by two ways. Fist one

它将出现在 url 中,因此您可以通过两种方式获取它。 拳头一

<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
$id = $this->input->get('id');

2nd one.

第二个。

$id = $this->uri->segment(3);

But in the second method you have to count the no. of segments in the url that on which no. your id come. 2,3,4 etc. then you have to pass. then in the ();

但是在第二种方法中,您必须计算否。网址中没有的段数。你的id来了 2,3,4 等,那么你必须通过。然后在 ();

回答by madhu kant tiwari

My controller

我的控制器

public function delete_category()   //Created a controller class //
    {      
         $this->load->model('Managecat'); //Load model Managecat here 
         $id=$this->input->get('id');     //  get the requested in a variable
         $sql_del=$this->Managecat->deleteRecord($id); //send the parameter $id in Managecat  there I have created a function name deleteRecord

         if($sql_del){

               $data['success'] = "Category Have been deleted Successfully!!";  //success message goes here 

         }

    }

My Model

我的模特

public function deleteRecord($id) {

    $this->db->where('cat_id', $id);
    $del=$this->db->delete('category');   
    return $del;

}