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

Codeigniter pagination URLs to be start with ?page=1 and so on

我已经在 Codeigniter 框架中创建了我的网站,并且想为特定页面而不是整个网站更新我的分页 URL。低于当前 URLs 有效的模式

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 将是一个整数

在使用 page_query_string 时不应设置 uri_segment。

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

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

您的 url 上的额外 / 可能是因为您设置了基础 url。尝试将其删除。

您也可以尝试将每次出现的“&page=/”替换为“?page=”。

我创建了一个自始至终都在使用的分页助手。需要做一些数学运算才能计算出您的总页数,但这应该可行。

<?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)); ?>

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