我有一个值通过$ _POST传递到我的搜索结果页面并存储在名为$ term的变量中。然后变量在MySQL查询中,然后运行以显示搜索结果,如果有超过15个结果,我有一个分页php脚本,将结果分成几页。
第一页很棒,找到了正确的结果数量,并且可以使用正确的页数。如果我回显变量,则将正确的值存储在其中。但是,如果我选择查看下一批结果并且页面刷新,则会列出表的所有行(就好像查询只是SELECT *而没有参数)并且变量现在为空
这是我的代码(有点长,对不起,我打算把它放在另一个页面中,并在它正常工作后加入它)
<?php
$tbl_name="accounts";
$adjacents = 3;
if (isset($_POST['basic_search_submit'])){
$term = $_POST['item'];}
$query = "SELECT COUNT(*) as num FROM accounts LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c WHERE accounts.name LIKE '%$term%' ORDER BY accounts.name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$targetpage = "search_results_play.php";
$limit = 15;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$sql = "SELECT * FROM accounts LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c WHERE accounts.name LIKE '%$term%' ORDER BY accounts.name LIMIT $start, $limit";
$result = mysql_query($sql);
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">< < previous</a>";
else
$pagination.= "<span class=\"disabled\">< < previous</span>";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next > ></a>";
else
$pagination.= "<span class=\"disabled\">next > ></span>";
$pagination.= "</div>\n";
}
?>
<? while ($row = mysql_fetch_array($result)){
//table displaying results
}
?>
<?=$pagination?>
非常感谢任何帮助,谢谢
答案 0 :(得分:3)
PHP中的变量不会跨页面持久存在。当然,除了其中一些,但常规$page
或$whatever
将不会持续存在。您将加载一个页面,其中包含一些变量,然后在页面完全加载到浏览器后,这些变量将消失。
建议在所有页面的脚本顶部使用session_start()
,并使用$_SESSION['variable']=...
存储变量。这样,它们将持续跨页。
最简单的解决方法:
$page = $_GET['page'];
用这个替换该行:
$page = isset($_SESSION['page']) && is_numeric($_SESSION['page']) ? $_SESSION['page'] : 0;
$_SESSION['page'] = $page;
并在每个脚本的顶部添加session_start()
。如果您有一个公共头包含,或者带有mysql身份验证数据的config.php,请将它放在该文件的顶部。
答案 1 :(得分:0)
另一种解决方案是使用GET
请求进行搜索,并按$_GET['term']
获取该字词,并附加到生成的链接'&term='. $_GET['term']
。这很简单。使用$_SESSION['term']
的主要缺点是,如果您将页面留给主页,例如,此变量将保持设置,直到会话过期,并可能导致未来的麻烦。