我有一个搜索,我正在尝试进行分页。我在 PHPEasyStep找到了一个很酷的 php 脚本(是的,我是 PHP 和编程新手。)我实现了它,它可以显示分页和前进一页(从第 1 页到第 2 页),但是当您从第 2 页返回到第 1 页,搜索查询消失了,然后显示每个数据库条目(但分页)。搜索查询一定在某处迷路了?
代码(当我粘贴时,它已被压缩,所以我已尽力缩进,但如果仍然难以阅读,我感到非常抱歉。如果它太难阅读,主要是在网站上,我从更好的缩进...上面有一个链接!)
<?php
$q = mysql_real_escape_string(ucfirst(trim($_GET['searchquery'])));
?>
<?php
REQUIRE('config.php');
$tbl_name="companies";
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE company_name LIKE '%$q%' OR company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$targetpage = "search.php";
$limit = 10;
$page = $_GET['page'];
if($page){
$start = ($page - 1) * $limit;
}
else{
$start = 0;
}
$sql = "SELECT * FROM $tbl_name WHERE company_name LIKE '%$q%' OR company_description LIKE '%$q%' OR cat1 LIKE '%$q' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' OR company_phone LIKE '%$q' ORDER by company_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\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev+searchquery=$q\">� previous</a>";
else
$pagination.= "<span class=\"disabled\">� previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter+searchquery=$q\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
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+searchquery=$q\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">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+searchquery=$q\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1+searchquery=$q\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage+searchquery=$q\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1+searchquery=$q\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2+searchquery=$q\">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+searchquery=$q\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next+searchquery=$q\">next �</a>";
else
$pagination.= "<span class=\"disabled\">next �</span>";
$pagination.= "</div>\n";
}
?>
如果您需要更多代码,我可以快速提供!感谢您的任何帮助!
哦,我忘了说我试图通过在所有分页 URL 的末尾添加 +searchquery=$q 来解决这个问题,所以这就是为什么存在。