Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 806
Atif Azad Avatar asked Apr 15 '13 07:04

Atif Azad


People also ask

How can I code pagination in PHP?

The steps to follow to implement pagination with PHP and MySQL are: STEP1: Create a large-scale database. STEP2: Create a table and insert the list of records in it. STEP3: Create a link for pagination to segregate/distribute the data over several different pages and add it to the bottom of the table created in step 2.

How can we manually create pagination in laravel?

Creating A Paginator Manually Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so by creating either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance, depending on your needs.

What is pagination programming?

Pagination is a process that is used to divide a large data into smaller discrete pages, and this process is also known as paging. Pagination is commonly used by web applications and can be seen on Google.


1 Answers

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 :)

like image 82
Sabari Avatar answered Oct 15 '22 01:10

Sabari