简体   繁体   English

现在我使用mod-rewrite时需要我的分页器PHP函数的帮助

[英]Need help with my pager PHP function now that I use 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 我现在被困住了,下面是我的分页代码的一部分,这是为分页链接构建URL的部分,到目前为止,它的工作真的非常好,因为现在我要更改整个站点以使用mod-rewrite,因此在页面看起来像这样

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

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

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

here is the code that makes the OLD links, any help or ideas on how I can use for new links? 这是构成OLD链接的代码,关于如何用于新链接的任何帮助或想法?

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中是否存在其他变量,如果看到它们,则将确保将其保留在建立新页面链接时所生成的URL中,例如?p = test&userid = 2&color = green&page = 3,它将确保将所有多余的内容保留在其生成的新url中,并仅增加或减少页码

$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.
}

This will do the trick 这将解决问题

$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;
}

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. 此处代码的作用是从uri中删除/ page / 2部分(如果存在),然后可以根据需要修改代码。
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. 您提到,如果/ page / 1部分不在URI中,则代码可以正常工作,因此删除该部分(如果存在)也应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM