Codeigniter分页URL以?page = 1开头,依此类推

时间:2018-11-27 11:49:21

标签: codeigniter url pagination query-string

我已经在Codeigniter框架中创建了我的网站,并希望针对特定页面而不是整个网站更新我的分页URL。在当前有效的网址格式下方

https://www.example.com/city/2
https://www.example.com/city/hotel-with-air-conditioning.html/2
https://www.example.com/locality/hotels-in-dwarka.html/2
https://www.example.com/locality/5-star-hotel-in-dwarka.html/2
https://www.example.com/city/5-star-hotels-in-newdelhi.html/2
https://www.example.com/locality/hotel-with-air-conditioning-in-dwarka.html/2

Now I want to change them all above to as shown below:

https://www.example.com/city?page=2
https://www.example.com/city/hotel-with-air-conditioning.html?page=2
https://www.example.com/locality/hotels-in-dwarka.html?page=2
https://www.example.com/locality/5-star-hotel-in-dwarka.html?page=2
https://www.example.com/city/5-star-hotels-in-newdelhi.html?page=2
https://www.example.com/locality/hotel-with-air-conditioning-in-dwarka.html?page=2

我已经使用$ config ['page_query_string'] = TRUE;在初始化分页方法之前,

进行更改后,我得到了“&per_page = / 2”,但不是我所需要的。还建议我应该在$ config ['uri_segment']中传递什么,因为我不想从段中而是从查询参数中选择页号?page = X,其中X是整数

2 个答案:

答案 0 :(得分:1)

我创建了一个遍历的分页助手。可以做一些数学运算来弄清楚您总共有多少页,但这应该可行。

<?php
function paginate($pagecount, $currentpage, $baseurl, $params = array()) {

    //var_dump($params);
    $param_parts = array();
    foreach ($params as $key => $param) {
        $param_parts[] = $key . '=' . $param;
    }
    ?>
    <div class="text-center">
    <?php
    $left_ellipsis = false;
    $right_ellipsis = false;
    for ($i = 1; $i <= $pagecount; $i++) {

        if ( ($i < 4) || ($i > $pagecount - 3) || ($i > $currentpage - 3 && $i < $currentpage + 3) ) {

            if ($i == $currentpage) {
                ?>
                <span class="btn btn-sm disabled"><?php echo $i; ?></span>
                <?php
            } else {
                $urlout = array(
                    'currentpage=' . $i
                );
                $urlout = implode('&',$param_parts);
                if ($urlout != '') {$urlout = '&' . $urlout;}
                ?>
                <a class="btn btn-default btn-sm" href="<?php echo $baseurl; ?>?page=<?php echo $i; ?><?php echo $urlout; ?>"><?php echo $i; ?></a>
                <?php
            }
        } else {
            if ($currentpage > 6 && !$left_ellipsis) {
                $left_ellipsis = true;
                echo '<span class="ellip-block">...</span>';
            }
            if ($currentpage < $pagecount - 5 && $i > $currentpage && !$right_ellipsis) {
                $right_ellipsis = true;
                echo '<span class="ellip-block">...</span>';
            }
        }
        //echo $pagecount; 
    }
    ?></div><?php
}
?>

我将助手文件命名为paginate_helper.php,要使用它,只需将其添加到任何视图中即可。

$params变量只是获取通过“ get”传递的参数数组。

<?php paginate($page, $cur, $url, array('q' => $q)); ?>

可能会有所帮助。随时更新和分发。

答案 1 :(得分:0)

使用page_query_string时,您不应设置uri_segment。

要获取page = 1而不是per_page,应使用query_string_segment配置,如下所示:

$config['query_string_segment'] = 'page';

URL上的多余/可能是因为您设置了基本URL。尝试将其删除。

您也可以尝试将所有出现的'&page = /'替换为'?page ='。