如何使用PHP修复分页

时间:2022-10-16 09:41:47

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

以前去第3页(文本框)1 2 3 4 5 ....400下

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到最后一页的所有数字……我想用下面的方式来打破它。

   1 2 3 4 5 ....400

thanx

谢谢

1 个解决方案

#1


3  

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

如果您正在使用WordPress,那么有一个用于分页的内置函数paginate_links。

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

您可以在这里查看更多信息: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.

与其创建自己的函数,不如使用inbuilt function,它的功能与您需要的功能相同。

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.

这将有助于你满足你的需要。你只需要传递正确的参数。mid_size是您需要指定应该向当前页的任意一侧显示多少页号的参数,但不包括当前页。

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

希望这有助于:)

#1


3  

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

如果您正在使用WordPress,那么有一个用于分页的内置函数paginate_links。

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

您可以在这里查看更多信息: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.

与其创建自己的函数,不如使用inbuilt function,它的功能与您需要的功能相同。

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.

这将有助于你满足你的需要。你只需要传递正确的参数。mid_size是您需要指定应该向当前页的任意一侧显示多少页号的参数,但不包括当前页。

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

希望这有助于:)