我有一个值通过 $_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?>
任何帮助将不胜感激,谢谢