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;
#2

[eluser]worchyld[/eluser]
Okay, seeing as there's been no replies on this, I guess I have try a different idea;

1) Are examples of a paginated form that works with posted variables that doesn't have that annoying cached POSTed variables message?

2) How did you make it remember the success message and the offset, and yet still allow you to get back to page 1?

Examples of combining pagination with a form would be great!
#3

[eluser]worchyld[/eluser]
Still no replies? I guess I need to supply the original code in full to get a reply?

1. You can download the controller here;
http://www.filepanda.com/get/mztkwbbs/

Installation instructions;
rename to .php file and get rid of the phrase "controller_" and place file in controller folder

2.1 You can download the model here; (Rename as .php file, and place in model folder)
http://www.filepanda.com/get/wh2fa8pb/

3. You can download the view file here; (Remove view_ from title, rename as .php and put in view folder)
http://www.filepanda.com/get/nhjluml8/


Just to summarize.

1. There is a paginated list of customers. Using GET works, there is no issue with pagniation.
2. There is a form which appears on the page which will allow you to add new customers (not implemented yet).
3. Pressing submit on the form produces a success message, after a redirect (to prevent posted variables message)
4. But it won't remember the offset. Using a session is the only way to solve this, except that it won't let you go back to page 1.
5. Getting rid of the redirect and using a normal view page means that the session message is never displayed until the next time you go to the page.

I don't know what else to do to make it clear what I'm trying to do?

I guess I can always upload a demo.
#4

[eluser]FarCoder[/eluser]
Heya,

use sessions, you can always store data there and then check what you need.




Theme © iAndrew 2016 - Forum software by © MyBB