所以我正在关注this教程。 PHP_SELF
在我的情况下不起作用,因为我在搜索结果页面中使用分页,而url本身具有变量查询字符串。 REQUEST_URI
无效,因为每次附加最后一个网址
<?
if(!isset($_GET['page'])) {
$base_url = $_SERVER['REQUEST_URI'];
}
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"{$_SERVER['REQUEST_URI']}?page = $last\">Last 10 Records</a> |";
echo "<a href = \"{$_SERVER['REQUEST_URI']}?page = $page\">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = \"{$_SERVER['REQUEST_URI']}?page = $page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"{$_SERVER['REQUEST_URI']}?page = $last\">Last 10 Records</a>";
}
?>
我的网址生成为:
search.php?mode=keyword&keyword=&importer_name=&consignor_name=&description=&bl_date=&categories%5B%5D=0
字段是动态的。
答案 0 :(得分:0)
对于搜索结果,我曾经创建过此代码:
<?
$pager = array(
/*here is some params from MySQL and $_GET*/
'current'=0, // current page num from $_GET
'total' = 200, // Total pages counted based on MySQL query results
);
$params = $_GET; // do not forget to cleanup and validate GET before using it!
$page = $_SERVER['REQUEST_URI']; // in my case here is URL generated
$page = trim(str_replace('//', '/', $page), '/'); // just to be shure
// Do we need PREV page button?
if ($pager['current'] != 0) {
if (($pager['current'] - 1) == 0) {
unset($params['page']);
} else {
$params['page'] = $pager['current'] - 1;
}
$url = trim($page . '?' . http_build_query($params), '?');
$pervpage = '<li class="previous"><a href="' . $url . '"><</a></li>';
}
// Do we need NEXT page button?
if ($pager['current'] != ($pager['total'] - 1)) {
$params['page'] = $pager['current'] + 1;
$url = trim($page . '?' . http_build_query($params), '?');
$nextpage = '<li class="next"><a href="' . $url . '">></a></li>';
}
// Lets create 2 buttins fo pages before current and 2 buttons for pages after current
$start = $pager['current'] - 2 >= 0 ? $pager['current'] - 2 : 0;
$end = $pager['current'] + 3 <= $pager['total'] ? $pager['current'] + 3 : $pager['total'];
if ($end - $start <= 4) {
if ($end == $pager['total']) {
$start = $pager['total'] - 5 >= 0 ? $pager['total'] - 5 : 0;
}
if ($start == 0) {
$end = $pager['total'] >= 5 ? 5 : $pager['total'];
}
}
$pager['pages'] = $pervpage;
for ($i = $start; $i < $end; $i++) {
if ($i == $pager['current']) {
$pager['pages'] .= '<li><span>' . ($i + 1) . '</span></li>';
} else {
if ($i == 0) {
unset($params['page']);
} else {
$params['page'] = $i;
}
$url = trim($page . '?' . http_build_query($params), '?');
$pager['pages'] .= '<li><a href="' . $url . '" >' . ($i + 1 == 0 ? "" : $i + 1) . '</a> </li>';
}
}
$pager['pages'] .= $nextpage;
// And we almost ready to output results
$pager['pages'] = str_replace('?&', '?', $pager['pages']);
// Now we can output results
echo $pager['pages'];
?>
我认为可能会进行优化。但是现在它的工作做得很好
答案 1 :(得分:0)
因此,请尝试提取脚本URI (请参阅下面的示例)。
<?php
// Display what we have got on input
echo "<h3>\$_GET parameters received:</h3>";
if (count($_GET)<1) {
echo "NONE<br />";
} else foreach ($_GET as $k=>$v) {
echo "\$_GET[$k]=$v<br />";
}
echo "<h3>Link created:</h3>";
// Get URI elements and create script URI
$prot = ((isset($_SERVER['HTTPS'])) && (strtoupper($_SERVER['HTTPS']) == 'ON')) ? 'https' : 'http';
$script_url = $prot . '://' . getenv('SERVER_NAME') . ':' . getenv('SERVER_PORT') . $_SERVER['PHP_SELF'];
// Collect parameters
$query = '';
$pagenum = (isset($_GET['page'])) ? $_GET['page'] : -1;
foreach ($_GET as $k=>$v) {
if (strtolower($k)=='page') continue; // skip 'page' parameter
$query .= "&$k=$v";
}
// Create link
$pagenum++;
$link = $script_url . '?page=' . $pagenum . $query ;
echo "\$link=$link<br />";
echo "<a href=\"$link\">link</a><br />";
?>
答案 2 :(得分:0)
如果动态生成查询字符串,使用$ _SERVER [&#39; QUERY_STRING&#39;]可能会解决您的问题...以下代码我的帮助您的问题
if(isset($_SERVER['QUERY_STRING'])) {
$query_string = $_SERVER['QUERY_STRING'];
}else{
$query_string = "";
}
echo "<a href = \"{$_SERVER['REQUEST_URI']}?".$query_string."&page = $last\">Last 10 Records</a> |";