[eluser]worchyld[/eluser]
I have a paginated list of customers (with 2 fields: 'ID' and 'name'), which I've got working fine, even the GET links work fine.
I have a form underneath my listing so I can add/edit a customer to/from the list, and I want CI to remember what page it was on.
So, it sorta looks like this;
Quote:{{list of customers}}
{{paging links}}
{{form}}
The problem is, that CI doesn't remember what page it was on when the form is submitted as a POST (its fine as a GET), and consequently is always going back to the start of the list/
I don't know what' going wrong. It should remember what page it was on, regardless if it were a POST or a GET.
I've posted the code below;
Code:
<?php
/*
Controller for Form2
- A marjority of the helpers/libraries are already pre-loaded
*/
class Form2 extends Controller {
function Form2() {
parent::Controller();
$this->output->enable_profiler(TRUE);
no_cache();
} // end function
// =======================================================
function index() {
$this->load->library('pagination');
$this->load->library('table');
$this->load->library('validation');
// OFFSET ===============================================
$offset=0;
$offset = $this->uri->segment(3);
// At this point I want to check the post,
// but it never passes it, or remembers it, or NULLs it -- why?
$g_offset = $this->input->post("offset");
if (isset($g_offset)) {
$offset = $g_offset;
} // end if
// FORM VALIDATION =====================================
$rules['name'] = "trim|required|xss_clean";
$this->validation->set_rules($rules);
if ($this->validation->run() == FALSE) {
// List all records in a paginated fashion
$this->list_all($offset);
} else {
// Redirect to same page with success msg
$this->session->set_flashdata('msg', 'Save succeeded');
redirect('form2');
} // end if
// END FORM VALIDATION ================================
} // end function
// =======================================================
function list_all($offset=0) {
// Load model
$this->load->model('Customer_list_model');
// =======================================================
// Pagination config
$config['base_url'] = site_url('form2/index/');
$config['total_rows'] = $this->db->count_all('ci_customers');
$config['per_page'] = 10;
$num = $config['per_page'];
$data['offset'] = $offset;
// Run pagination config
$this->pagination->initialize($config);
// Get paginated results
$data['results'] = $this->Customer_list_model->get_customers('ci_customers', $num, $data['offset']);
// =======================================================
// Set page variables
$this->view->set("pageTitle", "Test pagination");
$this->view->set("data", $data);
$this->table->set_heading('ID', 'Name');
// =======================================================
// Load partial (view within a view)
$this->view->part("header", "layout/header.php");
// Load partial (view within a view)
$this->view->part("footer", "layout/footer.php");
// Load the actual page
$this->view->load('showform');
} // end function
} // end class
?>
Code:
<?php
/*
View: showform
- header and footer are just very basic semantic XHTML header/footer.
*/
?>
<?=$header;?>
<?php // List of records, paginated ?>
<?=$this->table->generate($data['results']); ?>
<?=$this->pagination->create_links(); ?>
<hr />
<?=$this->validation->error_string; ?>
<?=$this->session->flashdata('msg');?>
<?=form_open('form2/index'); ?>
<h5>Form test</h5>
<p>
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" value="" />
</p>
<p>
<input type="submit" value="Submit" />
<input type="text" id="action" name="action" value="add" />
<input type="text" id="offset" name="offset" value="<?=$data['offset'];?>" />
</p>
<?=form_close();?>
<?=$footer;?>
Code:
<?php
/*
Customer List Model
- A very basic model, doesn't do much yet except get customers.
*/
class Customer_list_model extends Model {
// Constructor model
function Customer_List_Model() {
parent::Model();
} // end function
function get_customers($table='ci_customers', $num=0, $offset=0) {
$query = $this->db->get($table, $num, $offset);
return $query;
} // end function
} // end model
?>