现在我使用mod-rewrite时需要我的分页器PHP函数的帮助 [英] Need help with my pager PHP function now that I use mod-rewrite

查看:82
本文介绍了现在我使用mod-rewrite时需要我的分页器PHP函数的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在被困住了,下面是我的分页代码的一部分,这是构建分页链接的URL的一部分,到目前为止,它的工作真的非常好,因为现在我要更改整个站点以使用mod-rewrite因此,在页面看起来像这样

I am stuck now, here below is a snip from my paging code, this is the part the builds the URL for the paging links, it worked really great until now because now I am changing my whole site to use mod-rewrite so before a page would look like this

http://localhost/?p = mail.inbox& page = 2

现在我希望它是这样..我已经有正则表达式可以执行此操作,但是我需要更改我的分页正确建立到新URL链接的方式

and now I want it to be like this..I already have the regex to do it but I need to change the way my paging builds links to the new URL's correctly

http://localhost/mail/inbox/page/2

以下是制作OLD链接的代码,有关如何使用新链接的任何帮助或想法?

here is the code that makes the OLD links, any help or ideas on how I can use for new links?

捕获是现在工作的方式,它可以确定URL中是否存在其他变量,如果看到它们,则将确保将其保留在创建新页面链接时所生成的URL中,例如?p. = test& userid = 2& color = green& page = 3,它将确保将所有多余的内容保留在它生成的新url中,而只是增加或减少页数

the catch is the way it works now is it can determine if other variables exist in the URL and if it see's them it will make sure it keeps them in the URL it makes when making new page links, for example is ?p=test&userid=2&color=green&page=3 it would make sure to keep all the extra stuff in the new url it makes and just increase or decrease the page number

$url_string = "?";
foreach ($_GET as $k => $v) {
    if ($k != "page") { // <-- the key you don't want, ie "page"
        if ($url_string != "?") {
            $url_string .= "&"; // Prepend ampersands nicely
        }
        $url_string .= $k . "=" . $v;
    }
}
$selfurl = $url_string . '&page=';
$page = $_GET['page'];
if ($page) {
    $start = ($page - 1) * $items_per_page;
}
else {
    $start = 0;
}
if ($page == 0) {
    $page = 1; //if no page var is given, default to 1.
}

推荐答案

这可以解决问题

$uri = $_SERVER['REQUEST_URI'];
$uri = preg_replace('#page/\d+/*#', '', $uri); // remove page from uri
$uri = preg_replace('#/$#', '', $uri); // remove trailing slash

$selfurl = $uri . '/page/';
$page = $_GET['page'];
if ($page) {
  $start = ($page - 1) * $items_per_page;
} else {
  $start = 0;
}
if ($page == 0) {
  $page = 1;
}

这里的代码正在做的是从uri中删除/page/2部分,然后您可以根据需要修改代码.
您提到,如果/page/1部分不在URI中,则该代码有效,因此删除该部分(如果存在)也应该起作用.

What the code is doing here is removing the /page/2 part from the uri if it exists and then you can modify the code as you want.
You mentioned that the code works if /page/1 part is not in the URI so removing that part if it exists should work too.

这篇关于现在我使用mod-rewrite时需要我的分页器PHP函数的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆