如何使用PHP修复分页

Ati*_*zad 6 php wordpress pagination

大家好,我想以下面的格式创建我的分页

转到第3页[文本框]上一页1 2 3 4 5 .... 400下一页

以下是我的代码

             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 />';
Run Code Online (Sandbox Code Playgroud)

现在问题是这.....它显示从1到最后一页的所有数字...我想在下面的风格打破它

   1 2 3 4 5 ....400
Run Code Online (Sandbox Code Playgroud)

感谢名单

Sab*_*ari 3

如果您使用 WordPress,则有一个内置的paginate_links分页功能。

您可以在此处查看更多信息:http://codex.wordpress.org/Function_Reference/paginate_links

与其创建自己的函数,不如使用内置函数来完成您所需的相同功能。

这将帮助您满足您的需求。您只需要传递正确的参数即可。mid_size是您需要指定当前页面两侧应显示多少页码的参数,但不包括当前页面。

更新 :

您的代码可以简化如下:

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;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)