分页搜索结果: 我已经为我的数据库搜索结果创建了这个分页
<?php
$con = mysql_connect("server","some_user","password"); // Enter hostname,user,password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// select database
mysql_select_db("some_db", $con);
$output = '';
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
//pagination code
$query = mysql_query("SELECT * FROM some_table WHERE Title Like '%$searchq%'"); //Counting total number of rows in the table 'data',
$total_rows = mysql_num_rows($query);
// setup configuration//
//step:3
$base_url = 'http://localhost/php_search/New/index.php'; //Provide location of you index file
$per_page = 1; //number of results to shown per page
$num_links = 8; // how many links you want to show
$total_rows = $total_rows;
$cur_page = 1; // set default current page to 1
//now we will extract information from url//
//step:4
if(isset($_GET['search']))
{
$cur_page = $_GET['search'];
$cur_page = ($cur_page < 1)? 1 : $cur_page; //if page no. in url is less then 1 or -ve
}
// calculate limit and offset, it'll will be used for Sql Query//
//step:5
$offset = ($cur_page-1)*$per_page; //setting offset
$pages = ceil($total_rows/$per_page); // no of page to be created
//Calculate the start and end page numbers for pagination links//
//step:6
$start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
$end = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;
//query the database with calculated OFFSET //
//step:7
$res = mysql_query("SELECT * FROM some_table WHERE Title Like '%$searchq%' LIMIT ".$per_page." OFFSET ".$offset);
mysql_close($con);
//pagination code
if(isset($res))
{
while($result = mysql_fetch_array($res))
{
$title = $result['Title'];
$name = $result['name'];
$description = $result['Description'];
$output .='<div><h1><a href="http://server.com/soome_page.php?name='.$name.'">'.$title.'</a><br>'.$name.'</h1><br>'.$description.'</div>';
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" placeholder="Search here" />
<input type="submit" value="search" />
</form>
<?php print("$output"); ?>
<div id="pagination">
<div id="pagiCount">
<?php
if(isset($pages))
{
if($pages > 1)
{ if($cur_page > $num_links) // for taking to page 1 //
{ $dir = "first";
echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?search='.(1).'">'.$dir.'</a> </span>';
}
if($cur_page > 1)
{
$dir = "prev";
echo '<span id="prev"> <a href="'.$_SERVER['PHP_SELF'].'?search='.($cur_page-1).'">'.$dir.'</a> </span>';
}
for($x=$start ; $x<=$end ;$x++)
{
echo ($x == $cur_page) ? '<strong>'.$x.'</strong> ':'<a href="'.$_SERVER['PHP_SELF'].'?search='.$x.'">'.$x.'</a> ';
}
if($cur_page < $pages )
{ $dir = "next";
echo '<span id="next"> <a href="'.$_SERVER['PHP_SELF'].'?search='.($cur_page+1).'">'.$dir.'</a> </span>';
}
if($cur_page < ($pages-$num_links) )
{ $dir = "last";
echo '<a href="'.$_SERVER['PHP_SELF'].'?search='.$pages.'">'.$dir.'</a> ';
}
}
}
?>
</div>
</div>
</body>
</html>
我已发布此代码,我在选择其他页面时未获得任何结果 分页确实发生,但点击第二页时没有搜索结果 请帮忙
答案 0 :(得分:0)
您将页码传递为&#34;搜索&#34;参数
?search='.($cur_page+1)
但仅在设置$ _POST [&#39;搜索&#39;]
时收集数据if(isset($_POST['search'])) {
因此,当您点击页面链接时,您只需设置$ _GET [&#39;搜索&#39;],而不是$ _POST [&#39;搜索&#39;]