【问题标题】:how to Fix Pagination using PHP如何使用 PHP 修复分页
【发布时间】:2013-04-07 06:24:17
【问题描述】:

大家好,我正在尝试以以下格式创建分页

转到第 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 />';

现在的问题是...... 它显示了从 1 到最后一页的所有数字...我想将其分解为以下样式

   1 2 3 4 5 ....400

感谢

【问题讨论】:

  • &amp;&amp;results_to_show 这是一个错字吗?你到处都用过
  • @Atif Azad 你在使用 WordPress
  • 是的,我正在使用 wordpress

标签: php wordpress pagination


【解决方案1】:

如果您使用 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;

希望这会有所帮助:)

【讨论】:

  • thanx sabari 实际上我是 wordpress 的新手......不知道如何使用它。你能告诉我如何在我的代码 paginate_links 代码中使用你提供的链接
猜你喜欢
  • 2017-07-27
  • 2021-12-10
  • 1970-01-01
  • 2021-01-10
  • 2021-06-04
  • 2020-01-12
  • 2012-10-02
  • 2013-04-28
  • 1970-01-01
相关资源
最近更新 更多