Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP修复分页_Php_Wordpress_Pagination - Fatal编程技术网

如何使用PHP修复分页

如何使用PHP修复分页,php,wordpress,pagination,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[

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

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

thanx

如果您使用WordPress,则有一个用于分页的内置函数
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;

希望这有帮助:)

和&results\u-to\u-show
这是打字错误吗?你用了所有的over@AtifAzad你在用wordpress吗是的我在用wordpress Thanx sabari事实上我是wordpress的新手。。。不知道怎么用。你能告诉我我将如何在我的代码中使用你提供的链接中的paginate_链接代码吗
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;