Welcome Guest, Not a member yet? Register   Sign In
don't really understand how pagination works
#1

[eluser]Flying Fish[/eluser]
This is my first crack at paginating something, having some trouble...

Here is the Controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*

    The Admin class is where we can view and manage orders.

*/


class Admin extends MY_Controller {

    function Admin()
    {
        parent::MY_Controller();
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        // Restrict access to Admins only
        if($this->authentication->check_admin() !== TRUE)
        {
            show_error('404');
        }
        
        // Load Libraries
        $this->load->library('pagination');
        
        // Load Models
        $this->load->model('Orders');
        $this->load->model('Users');

    }
    
    function index()
    {
            
        $data['title'] = "Admin :: NBT Supplies";
        $data['heading'] = "Admin Home";
        
        $data['query_new_orders'] = $this->Orders->new_orders();
        $data['query_old_orders'] = $this->Orders->old_orders();
        
        // Load this view by default
        $this->load->view('admin_index', $data);
    }
    
}


/* End of file admin.php */
/* Location: ./system/application/controllers/admin.php */

Here is the View
Code:
<div id="content">
            <h1>&lt;?=$heading?&gt;</h1>
            <h2>New Orders</h2>
            &lt;?
                if($query_new_orders !== FALSE):
            ?&gt;
                <table class="admin-orders">
                    <tr>
                        <th>Order Id</th>
                        <th>Date Ordered</th>
                        <th>Rush</th>
                        <th>User</th>
                    </tr>
            
                &lt;?
                    foreach ($query_new_orders as $row):
                    
                    $user = $this->Users->user_detail($row->user_id);
                ?&gt;
                <tr class="rush-&lt;?=$row->rush?&gt;">
                    <td class="order-id"><a >order_id?&gt;">&lt;?=$row->order_id?&gt;</a></td>
                    <td class="date-ordered">&lt;?=$row->date_ordered?&gt;</td>
                    <td class="rush">&lt;?=$row->rush?&gt;</td>
                    <td>&lt;? echo $user->user_first_name.' '.$user->user_last_name.' ('.$user->user_email.')';?&gt;</td>
                </tr>
                &lt;?
                    endforeach;
                ?&gt;
                </table>
            &lt;?
                else:
                    echo '<p>There are no new orders at this time</p>';
                endif;
            ?&gt;
                
                <h2>Old Orders</h2>
                &lt;?
                    if($query_old_orders !== FALSE):
                ?&gt;
                <table class="admin-orders">
                    <tr>
                        <th>Order Id</th>
                        <th>Date Ordered</th>
                        <th>Status</th>
                    </tr>
                    &lt;?    
                        foreach ($query_old_orders as $row):
                    ?&gt;
                    <tr class="&lt;?=$row->status?&gt;">
                        <td class="order-id"><a >order_id?&gt;">&lt;?=$row->order_id?&gt;</a></td>
                        <td class="date-ordered">&lt;?=$row->date_ordered?&gt;</td>
                        <td class="status">&lt;?=$row->status?&gt;</td>
                    </tr>
                    &lt;?
                        endforeach;
                    ?&gt;
                    
                </table>
                
                &lt;?
                    else:
                        echo '<p>There are no completed orders at this time</p>';
                    endif;
                    
                    $pagination_config['base_url'] = $this->uri->uri_string();
                    $pagination_config['total_rows'] = '3';
                    $pagination_config['per_page'] = '1';
        
                    $this->pagination->initialize($pagination_config);
                    echo $this->pagination->create_links();        
                    
                ?&gt;
        </div>&lt;!-- #content --&gt;

The page displays new orders at the top, and old orders at the bottom. Since there are going to be a lot more old orders than new, I'd like to display paginate below old orders and cycle through pages of those. (I'd like the new orders to display on every page regardless of what 'old order page' the user is on.)

I Read the documentation under pagination, but didn't find anything that helped me. Must be missing something major.

Thanks
#2

[eluser]jedd[/eluser]
Pagination doesn't ever seem to do what people think it will do. Which is odd.

Think of pagination as a way of making those little << 1 2 3 >> page-jump links at the top and bottom of pages - like a forum page on here, for example.

If you want to always have new orders at the top, separate to the old order view-table, then this is easy. If you want to paginate them separately to paginating the old orders, that's easy (but would be ugly I think) too. If you want to have old and new orders in the same view-table, but just have the new orders always on the first page .. that's easy too (and you would dictate that in your Model .. seriously)

The model is the key here.

You are going to want to change your Controller code from this:

Code:
$data['query_new_orders'] = $this->Orders->new_orders();
$data['query_old_orders'] = $this->Orders->old_orders();

.. to this:
Code:
$data['query_new_orders'] = $this->Orders->new_orders ( $offset , $items_per page );
$data['query_old_orders'] = $this->Orders->old_orders( $offset , $items_per_page );

That should give you a hint ... ?

In your model of course you'll need to have your new_orders() and old_orders() methods respect those two variables ($offset and $items_per_page), and modify your SQL queries to respect them. The hint there is the LIMIT parameter of SELECT .. it takes two parameters - offset, and count.

That should keep you busy for a little while Smile
#3

[eluser]xwero[/eluser]
I think the pagination library gives people the false impression it's tied in with a model because you have to set the items per page and the pagination link is the offset.

try my pagination plugin. i stripped the functionality of the CI library to the core which makes it easier to use. And the page segments is the actual page number which makes the url more hackable.
#4

[eluser]Thorpe Obazee[/eluser]
or you can view this file for a template for pagination

Pagination in CI
#5

[eluser]Flying Fish[/eluser]
Thanks guys. Let me take a whack at it again, and see how it goes.
#6

[eluser]Flying Fish[/eluser]
Thanks to all who replied to this.

I got pagination working. I did need to add a route, as per this post

apparently because I'm paginating the index method function thingy




Theme © iAndrew 2016 - Forum software by © MyBB