how to Fix Pagination using PHP how to Fix Pagination using PHP wordpress wordpress

how to Fix Pagination using PHP


If you are using WordPress there is an inbuilt function paginate_links for pagination.

You can view more info on the same here :http://codex.wordpress.org/Function_Reference/paginate_links .

Rather than creating your own, it will be good to use inbuilt function which does the same functionality what you need.

This will help you to suit your needs. You just need to pass the correct arguments. mid_size is the argument you need to specify how many page numbers should be shown to either side of current page, but not including current page.

UPDATE :

You code can be simplified like below :

global $wpdb;//get the total count$total_count = $wpdb->get_var('SELECT count(*) FROM `inventory_location`');//number of results to be shown per page$results_to_show_per_page   = 100;//specify the number of page numbers to be shown on each side of the current page$mid_size = 3;//check whether the query argument page is set and get the current pageif (isset($_GET['page']))    $page = abs((int)$_GET['page']);else    $page = 1;//generate page links$pagination_links = paginate_links( array(                            'base' => add_query_arg( 'page', '%#%' ),                            'total' => ceil($total_count / $results_to_show_per_page),                            'current' => $page,                            'mid_size'=> $mid_size                     ));echo $pagination_links;

Hope this helps :)