CodeIgniter分页 - 删除行后如何在同一页面上保存



我正在使用CodeIgniter分页库,并通过表单进行更新和删除。问题是,当我在删除或更新后删除或更新任何记录时,即使我在第四页上,我也将其重定向回到第一页。我如何将其重定向到完成更新或删除的同一页面。这是我用于更新和删除功能的控制器代码:

  public function update_att(){
      if(isset($_POST['update'])){
        $u_id = $_POST['at_id'];
        if($_POST['status']=="Present"){
            $data = array(
                  'status'=>"Absent"
            );
            $this->db->where('at_id', $u_id);
            $this->db->update('attendence', $data);
        }elseif ($_POST['status']=="Absent") {
            $data = array(
                  'status'=>"Present"
            );
            $this->db->where('at_id', $u_id);
            $this->db->update('attendence', $data);
        }
    redirect("usr/att_list", "refresh");
   }
}
 public function delete_att(){
     if (isset($_POST['delete'])) {
        $at_id = $_POST['at_id'];
        $this->db->where('at_id', $at_id);
        $this->db->delete('attendence');
      }
      redirect("usr/att_list" );
 }

目前,我正在重定向到第一页,任何建议如何将我重定向到我完成的删除或更新的页面。

<input type='hidden' name='current_page' value='<?php echo $current_page?>' />添加到您的表格中。

请记住,您需要用

之类的东西填充$current_page
    $current_page = ''; // empty means first page
    if( isset($_REQUEST['current_page'] ){
       $current_page = $_REQUEST['current_page'];
    }

然后,根据您的样本,您只需修改redirect路径,如下所示(对于您的两个功能):

public function delete_att(){
     if (isset($_POST['delete'])) {
        $at_id = $_POST['at_id'];
        $this->db->where('at_id', $at_id);
        $this->db->delete('attendence');
      }
      redirect("usr/att_list".(strlen($current) ? "?".$current : "" );
 }

注意:

您的页面路径应该看起来像这样的 domain.com/controller/your_page_with_pagination/?current_page=A_NUMBER,然后在controller/your_page_with_pagination中您根据$_GET['current_page']

调整要输出的数据

基于您的链接编辑:

    public function att_list($id = ""){
         /**
          * Keep your data in array
          */
         $this->data = array();
         /**
          * Add the page number ($id) to the array 
          * Check for url /:id first , or else check for a $_GET/$_POST current_page (this will be triggered when you submit your form)
          */
         $this->data['current_page'] = is_numeric($id) ? $id : (isset($_REQUEST['current_page'] ? $_REQUEST['current_page']: 0);
         /**
          * This is your 'keep on the same page' line
          * $this->data['current_page'] will act as an offset in your query
          * You can read about 'offset' here https://www.w3schools.com/php/php_mysql_select_limit.asp
          * $this->data['my_desired_records'] must be generated into your <form> ... </form> , and the same form must contain the input with name='current_page'
          */
         $this->data['my_desired_records'] = $this->your_model->get_data($this->data['current_page']);
         /**
          * Pass the whole array into your view
          */
         $this->load->view('path/to/view', $this->data);
      }

然后在您的视图中添加您的表格<input type='hidden' name='current_page' value='<?php echo $this->data['current_page']?>' />

最新更新