Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何删除codeigniter中的特定行?_Php_Codeigniter - Fatal编程技术网

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

Php 如何删除codeigniter中的特定行?,php,codeigniter,Php,Codeigniter,我是codeigniter的新手。在我的视图页面中,我将数据库中的数据显示在一个表中,在该表中我有两个用于更新和删除的锚标记。我想通过id从数据库中删除特定行 我的查看页面是 <?php foreach($query as $row){ ?> <tr> <td><?php echo $row->name ?></td> <td><?php echo $row->testi ?></

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

我的查看页面是

 <?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>
我的模型页面是

 function row_delete()
  {

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

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

您在模型中使用了一个
$id
变量,但您却不知从何处获取了它。您需要将
$id
变量从控制器传递到模型

控制器 让我们通过
row\u 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']);  
}
模型 将$id添加到模型方法参数中

function row_delete($id)
{
   $this->db->where('id', $id);
   $this->db->delete('testimonials'); 
}
现在的问题是,您从控制器中传递
$id
变量,但它不会在控制器中的任何位置声明。

一种简单的方法:

在视图中(传递id值):

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


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

它将出现在url中,因此您可以通过两种方式获取它。
**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;
    }
第一个

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

但是,在第二种方法中,您必须计算url中的段数,您的id的编号来自该段。2、3、4等等,然后你必须通过。然后在()中

我的控制器

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 

         }

    }
我的型号

public function deleteRecord($id) {

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

}

您必须传递$id才能删除模型中的行。首先,谢谢您,先生。还有一件事我需要在模型函数行中添加nything额外的代码,还是应该在模型函数行中保留相同的代码?非常感谢。它的工作就像一个魅力。。。非常感谢,先生,不客气,把我的代码放在你的代码里。这是一种快速满足需求的方法,但不是最好的方法。GET是幂等方法,POST不是。因此,我们使用GET获取数据,并对数据进行后期修改。
**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;
    }
<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
$id = $this->input->get('id');
$id = $this->uri->segment(3);
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 

         }

    }
public function deleteRecord($id) {

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

}