Welcome Guest, Not a member yet? Register   Sign In
Getting pagination to work with POSTED variables
#1

[eluser]worchyld[/eluser]
I have a paginated list of customers, and on the same page I also have a form where you can add a customer (not implmented yet).

The form sends back to the same page, and a redirect is actioned so that
it doesn't give that crappy cached POSTed variables message.

In order for it to remember the offset I force the session variable to remember the offset, except that when I click page 1 it never lets me get back to page 1 at all.

How do I fix this?

This is what I want the page to handle:

1) Paging with GET variables
2) Paging with POST variables
3) Handle incoming POSTed form data which sends an offset
4) Post a "Successfully saved message"
5) Stop the page from displaying the cached POSTed variables message when you press F5
6) Remember what page (offset) you are on, yet still allow you to get back to page 1.

Here is the code.

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
Show_all_customers.php controller
*/

class Show_all_customers extends Controller {

    // Constructor
    function Show_all_customers() {
        parent::Controller();
    } // end function

    // ==========================================

    // Main handler
    function index() {

        // Load libraries
        $this->load->library('pagination');
        $this->load->library('table');
        $this->load->library('validation');

        // Get the page offset
        $offset = $this->get_page_offset();

        // Handle form validation
        $rules['name'] = "trim|required|xss_clean";

        $this->validation->set_rules($rules);

        /*
        If there are the validation has not run list all pages to offset, else redirect to same page with a success message.
        */
        if ($this->validation->run() == FALSE) {

            // This prevents me from going back to page 1 -- why?
            $this->session->set_flashdata('offset', $offset);

            // List all
            $this->list_all($offset);

        } else {

            // Do something here (add, edit, delete?)
            // Redirect to same page with success msg
            $this->session->set_flashdata('msg', 'Save succeeded');
            $this->session->set_flashdata('offset', $offset);

            // stops silly "cached" post message
            redirect('show_all_customers');
            // $this->list_all($offset);

        } // end if
    } // end function

    // ==========================================

    // List all customers in a paged format
    function list_all($offset=0) {

        // Load customer model
        $this->load->model('Customer_list_model');

        // Pagination config
        $config['base_url']     = site_url('show_all_customers/index/');
        $config['total_rows']   = $this->db->count_all('ci_customers');
        $config['per_page']     = 10;
        $num                    = $config['per_page'];
        $config['cur_page']        = $offset;
        $data['offset']            = $offset;

        $this->pagination->initialize($config);        // Run paging

        // Get paging results
        $data['results']        = $this->Customer_list_model->get_customers('ci_customers', $num,    $data['offset']);

        // =======================================================

        $data['message'] = $this->session->flashdata('msg');

        // Set page variables
        $this->view->set("data", $data);
        $this->table->set_heading('ID', 'Name');

        // =======================================================

        // Load the actual page
        $this->view->load('show_all_customers');

    } // end function

    // ==========================================

    // Get the paging offset
    function get_page_offset() {
        $offset=0;

        if (is_numeric($this->uri->segment(3))) {
            // Handle GET variable
            $offset    = $this->uri->segment(3);
        } else {
            // Handle POST variable
            if (is_numeric($this->input->post("offset"))) {
                $offset = $this->input->post("offset");
            } else {
                // Handle SESSION variable
                if (is_numeric($this->session->flashdata('offset'))) {
                    $offset = $this->session->flashdata('offset');
                } else {
                    // Use default
                    $offset = 0;
                } // end if
            }
        } // end if

        return ($offset);
    } // end function


} // end class
?>
Code:
<?php
/*
Customer_list_model.php
*/
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
?>

Code:
<!--
VIEW FILE | Show_all_customers.php
-->

<?=$this->table->generate($data['results']); ?>
<?=$this->pagination->create_links(); ?>

<hr />

&lt;?=$this->validation->error_string; ?&gt;
<br />Msg:&lt;?//$this->session->flashdata('msg');?&gt;
&lt;?=$data['message'];?&gt;
&lt;!-- Form --&gt;
&lt;?=form_open('show_all_customers/index'); ?&gt;

<fieldset>
    <legend>Add Customer</legend>

    <p>
        <label for="name">Name:</label><br />
        &lt;input type="text" name="name" id="name" value="" /&gt;
    </p>

    <p>
        &lt;input type="submit" value="Submit" /&gt;
        &lt;input type="text" id="action" name="action" value="add" /&gt;
        &lt;input type="text" id="offset" name="offset" value="&lt;?=$data['offset'];?&gt;" /&gt;
    </p>
</fieldset>

&lt;?=form_close();?&gt;
&lt;!-- /Form --&gt;


Messages In This Thread
Getting pagination to work with POSTED variables - by El Forum - 10-25-2007, 08:55 AM
Getting pagination to work with POSTED variables - by El Forum - 10-26-2007, 02:38 AM
Getting pagination to work with POSTED variables - by El Forum - 10-27-2007, 11:41 AM
Getting pagination to work with POSTED variables - by El Forum - 11-05-2007, 08:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB