通过分页传递$ _GET(或$ _POST)变量(SESSION不起作用) [英] Passing $_GET (or $_POST) Variables With Pagination (SESSION not working)
问题描述
我知道这是一个普遍的问题,因为我在决定发布之前已经搜索了答案,但是我似乎无法找出解决方案.
I know this is a common problem as I have searched for answers before deciding to post, but I can't seem to figure out a solution.
问题:我有一个分页脚本(PHP)用于我的搜索结果.很明显,首页结果显示正常,然后进入第2、3等页时失败.
PROBLEM: I have a pagination script (PHP) to use for my search results. As is apparently common, the first page results show fine, then fail when moving onto page 2, 3 etc.
点击第2、3等页时,我在搜索中使用的每个变量均出现未知索引"错误.
I get an 'unknown index' error for each of my variables used in the search when clicking through to page 2, 3 etc.
所以我从表单中$ _GET这些变量:
So I $_GET these variables from my form:
$_SESSION['var1']= $_GET['var1'];
$_SESSION['var2']= $_GET['var2'];
$_SESSION['var3']= $_GET['var3'];
注意事项: 我的标题中已经开始一个会话;我之所以使用$ _GET,是因为如果用户退回",我不希望收到重新提交"警告;变量全部被清除(只是没有足够长的时间未在代码中显示);我必须在WHILE循环中使用$ _GET变量,因为它们计算每个结果的距离,年龄等.
Points to note: A Session has already been started in my header; I'm using $_GET because i prefer not having the 'resubmit' warning if a user goes 'back'; variables are all cleaned (just not shown in code as its long enough already); I have to use the $_GET variables with the WHILE loop as they calculate distance, age etc of each result.
我的分页脚本:
$limit = 4;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
$page = mysql_escape_string(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
if($page) {
$start = ($page - 1) * $limit;
} else {
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName ORDER BY joindate DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1) {
$paginate .= "<div class = 'hp1col'><div class='paginate'>";
$pagetotal = $total_pages.' Results';
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
} else {
$paginate.= "<span class='disabled'>previous</span>";}
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{ $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>Next</a>";
} else {
$paginate.= "<span class='disabled'>Next</span>";
}
$paginate.= "</div></div>";
}
while($row = mysql_fetch_array($result))
{
if (($part1 >= $_SESSION['var1']) AND ($part2 <= $_SESSION['var2']) AND ($part3 <= $_SESSION['var3'])) {
echo
"[Results]
}
}
echo $paginate;
我尝试在此if语句中开始新的会话,但无济于事:
I tried starting a new session in this if statement but it didn't help:
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
} else {
$paginate.= "<span class='disabled'>previous</span>";}
我希望有人可以提供帮助.我为该问题中的大量代码表示歉意,但我认为最好只是放轻松些,希望有人能够提供帮助.
I hope someone can help. I apologise for the slab of code in the question, but I thought it best just to put everything in for ease in, hopefully, someone being able to help.
谢谢
推荐答案
因此,您需要将这些查询参数传递到下一页.如果您的页面期望出现$_GET['var1']
,但您的URL中没有?var1=foo
,则显然无法正常工作.解决此问题的最简单方法是 http_build_query
:
So you need to pass those query parameters through to the next page. If your page expects $_GET['var1']
to be present but you don't have ?var1=foo
in the URL, it obviously won't work. The easiest way to handle this is http_build_query
:
printf('<a href="%s?%s">Next</a>',
$targetpage,
http_build_query(array('page' => 2) + $_GET));
这会将所有当前值保留在$_GET
中,并向其中添加一个page=2
参数.根据您的情况进行修改.
This preserves all current values in $_GET
and adds a page=2
parameter to it. Modify as needed for your case.
这篇关于通过分页传递$ _GET(或$ _POST)变量(SESSION不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!