how to Fix Pagination using PHP

hi everyone i am trying to create my pagination in below format

Go to Page 3 [text-box] Previous 1 2 3 4 5 ….400 Next

Read More

and below is my code

             if(isset($_REQUEST['limit_start']) && $_REQUEST['limit_start'] > 0) $limit_start = $_REQUEST['limit_start'];
        else  $limit_start = 0 ;
        if(isset($_REQUEST['results_to_show']) )  $results_to_show= $_REQUEST['results_to_show'];
        else $results_to_show = 100;

        if(($limit_start-$results_to_show)>=0)
        $pagination.='<a href="details-inventory.php?limit_start='.($limit_start-$results_to_show).'&&results_to_show='.$results_to_show.'" >Previous</a> | ';

          if (isset($_REQUEST['submit']) && $_REQUEST['submit'] != "")$search_limited = 1;
          else $search_limited = 0;

        global $wpdb;
        $sql='SELECT count(*) 
        FROM `inventory_location` ';

        $data= $wpdb->get_results($sql,ARRAY_A);
         $row_count= $data[0]['count(*)']; 

        for($number=(($limit_start/$results_to_show)+1);$number<($row_count/$results_to_show);$number++)//($row_count/$results_to_show);$number++)($limit_start/$results_to_show)+
        $pagination.= '<a href="details-inventory.php?limit_start='.($number*$results_to_show).'&&results_to_show='.$results_to_show.'" >'.$number.'</a> | ';

        $pagination.= ' <a href="details-inventory.php?limit_start='.($limit_start+$results_to_show).'&&results_to_show='.$results_to_show.'" >Next </a> <br />';

Now the problem is this …..
it show all the numbers from 1 to last page … i want to break it in below style

   1 2 3 4 5 ....400

thanx

Related posts

Leave a Reply

1 comment

  1. 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 page
    if (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 🙂