我尝试使用$_GET[variable from url]
为我的网站制作高级搜索引擎,以传递用户在我的搜索网页中选择的选项。
我有三个选项供用户搜索:
1. Search By Authors
2. Search By Category
3. Search By Language
4. Search By Keyword
下面的是$query
$k = mysql_real_escape_string($_GET['k']);
$a = mysql_real_escape_string($_GET['a']);
$l = mysql_real_escape_string($_GET['l']);
$c = mysql_real_escape_string($_GET['c']);
$k = $k != NULL ? "AND (native_name LIKE '%".$k."%' OR unique_name LIKE '%".$k."%')" : "";
$a = $a !=0 ? "AND auth_id=".$a : "";
$l = $l !=0 ? "AND lang_id=".$l : "";
if(mysql_real_escape_string($_GET['c']) !=0) {
$query = "
SELECT
books.id,
books.native_name,
books.unique_name
FROM
book_cat_relation
Inner Join books ON
book_cat_relation.book_id = books.id
WHERE
status=1 AND
book_cat_relation.cat_id = ".$c." ".$k." ".$a." ".$l."
";
}else{
$query = "
SELECT
id,
native_name,
unique_name
FROM
books
WHERE
status=1 ".$k." ".$a." ".$l."";
}
下面的分页代码正在应用限制,但没有显示分页,甚至我试图把页面没有手动它不工作...请帮助并纠正我错误的地方。
<?php
$sql = mysql_query($query);
$nr = mysql_num_rows($sql);
if (isset($pn)) {
$pn = preg_replace('#[^0-9]#i', '', $pn);
} else {$pn = 1;}
$itemsPerPage = 12;
$lastPage = ceil($nr / $itemsPerPage);
if ($pn < 1) {$pn = 1;} else if ($pn > $lastPage) {$pn = $lastPage;}
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
$sql2 = mysql_query($query." ".$limit);
?>
<?php
include('db.php'); // DB.
$tbl_name="books"; //table name
$adjacents = 5;
$k = mysql_real_escape_string($_GET['k']);
$a = mysql_real_escape_string($_GET['a']);
$l = mysql_real_escape_string($_GET['l']);
$c = mysql_real_escape_string($_GET['c']);
$total_pages = mysql_fetch_array(mysql_query($sql));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "page.php"; //your file name (the name of this file)
$limit = 12; //how many items to show per page
$pn = mysql_real_escape_string($_GET['pn']);
if($pn)
$start = ($pn - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$result = mysql_query($sql2);
/* Setup page vars for display. */
if ($pn == 0) $pn = 1; //if no page var is given, default to 1.
$prev = $pn - 1; //previous page is page - 1
$next = $pn + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($pn > 1)
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$prev\">Prev</a>";
else
$pagination.= "<span class=\"disabled\">prev</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $pn)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($pn < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $pn)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $pn && $pn > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=1\">1</a>";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=2\">2</a>";
$pagination.= "....";
for ($counter = $pn - $adjacents; $counter <= $pn + $adjacents; $counter++)
{
if ($counter == $pn)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=1\">1</a>";
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $pn)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$counter\">$counter</a>";
}
}
}
//next button
if ($pn < $counter - 1)
$pagination.= "<a href=\"$targetpage?k=$k&c=$c&a=$a&l=$l&pn=$next\">Next</a>";
else
$pagination.= "<span class=\"disabled\">next</span>";
$pagination.= "</div>\n";
}
?>
<?=$pagination?>