gpt4 book ai didi

mysql - 带分页的搜索结果

转载 作者:行者123 更新时间:2023-11-30 00:17:09 25 4
gpt4 key购买 nike

带分页的搜索结果:我已经为我的数据库搜索结果创建了这个分页

<?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>

我已经发布了这段代码,在选择另一个页面时我没有得到任何结果确实发生了分页,但单击第二页时没有搜索结果如有任何帮助,请

最佳答案

您将页码作为“搜索”参数传递

?search='.($cur_page+1)

但仅在设置 $_POST['search'] 时收集数据

if(isset($_POST['search'])) {

因此,当您单击页面链接时,您只设置了 $_GET['search'],而不是 $_POST['search']

关于mysql - 带分页的搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23563617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com