我正在使用codeigniter分页库并通过表单进行更新和删除。问题是,当删除或更新后删除或更新任何记录时,即使我在第4页,我也会重定向回第1页。如何重定向到我更新或删除的同一页面。 这是我的更新和删除功能的控制器代码:
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" );
}
目前我正在重定向到第一页,任何建议如何重定向到我已完成删除或更新的页面。
答案 0 :(得分:0)
将<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']?>' />