Php Flow » Wordpress

Pagination in wordpress admin with wp-mvc

Pagination is very important functionality of any website.The wordpress provides many plugin to integrate beautiful pagination on your website,but some times you need to create your custom pagination based on your requirement.
I am using wp-mvc plugin to create plugin in wordpress based MVC framework. Wp MVC has inbuilt index action to create admin grid but if you need to cutomized grid, then you need to override index action of wp mvc.
So in this tutorial i will described how to create pagination based on mvc architecture.

There are following steps need to create pagination:

Step 1:We will override index action in controller file.

1
2
3
4
5
6
7
8
9
10
11
public function index() {
        $post = isset($_REQUEST) ? $_REQUEST: array();
        $data=$this->Setting->fetchSettings(array(
                    'page' => isset($post['p']) ? intval($post['p']) : 1,
                    'rp' => isset($post['rp']) ? intval($post['rp']) : 2,
                    'id' => isset($this->params['id']) ? $this->params['id'] : 0
        ));
        $this->set('results', $data);
    }

In action we will send each time page number/record_pre-page parameters to model and we will get result set as a response.

Step 2: Fetch data from model based on limit and offset parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function fetchSettings($options){
        global $wpdb;
        $collection = array();
        $data = array();

        $page = ($options['page'] > 1) ? $options['page'] : 1;
        $offset = $options['rp'];
        $limit = ($page-1)*$offset;
        $sql = "SELECT
                    st.constant as setting_key,st.SID
                FROM
                    tbl_settings as st";

        if(!empty($options['id'])) {
            $sql .= " WHERE st.SID = '". $options['id'] ."'";
        }
        $sql .= " LIMIT ". $limit .",".  $offset;

        $count = "SELECT
                    count(SID)
                FROM
                    tbl_settings";
        $collection['results'] = $wpdb->get_results($sql, ARRAY_A);
        $collection['total'] = $wpdb->get_var($count)/$options['rp'];

        return $collection;

    }
?>

Step 3: Finally we will add pagination nav bar into view file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="tablenav">

<div class="tablenav-pages"><?php
$base = admin_url('admin.php?page=mvc_settings')  . remove_query_arg('p', $query_string) . '%_%';

echo paginate_links( array(
    'base' => $base, // the base URL, including query arg
    'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p"
    'prev_text' => __('&laquo; Previous'), // text for previous page
    'next_text' => __('Next &raquo;'), // text for next page
    'total' => $results['total'], // the total number of pages we have
    'current' => isset($_REQUEST['p']) ? intval($_REQUEST['p']) : 1, // the current page
    'end_size' => 1,
    'mid_size' => 5,
));
echo $pagination; ?></div>

</div>
Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>