我正在尝试在我的代码中实现分页。它不能很好地运作:
1.我希望每页显示5个结果,但它显示第一页上的所有内容。
2.当我点击页码时,会出现错误:未定义的索引:搜索。
非常感谢您的帮助!
首先我搜索一个单词,
<form method="post" action="AF12P.php">
<input type="submit" name="submit" value="search">
<input type="text" name="search" />
</form>
这里是AF12P.php
<div id="wrapper">
<div id="comments">
<?php
include('includes/db_AF.php'); //includes the db credentials
include ('paginate.php'); //include paginate page
$per_page = 5; // number of results to show per page
$connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
if ($connection->connect_error) {
die('Connect Error: ' . $connection->connect_error);
}
else {
$search=$_POST["search"];
$query="SELECT *, comment FROM comments AS c
JOIN wordtable2 AS w
ON c.w1 = w.w1
WHERE word like '%$search%'
ORDER BY word DESC"; // Run the query
$resultx = mysqli_query($connection, $query);
$total_results = mysqli_num_rows($resultx);
$total_pages = ceil($total_results / $per_page);//total pages
$result_obj = '';
$result_obj = $connection->query($query);
if (isset($_GET['page'])) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
$show_page=0;
}
// display pagination
if(isset($_GET['page'])){
$page = intval($_GET['page']);
}else{
$page =0;
}
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
while($result = $result_obj->fetch_array(MYSQLI_ASSOC)) {
$items[] = $result;
}
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
//echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
}
foreach ($items as $item) {
echo('<a href="AF11G.php?search='.$item['word'].'">'.$item['word'].'</a>');
echo '<br />';
echo $item['comment'];
echo '<br />';
}
}
?>
</div>
</div>
这是paginate.php {来源:http://www.techumber.com/2012/08/simple-pagination-with-php-mysql.html}
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out.= "<span>" . $prevlabel . "</span>\n";
} elseif ($page == 2) {
$out.= "<li><a href=\"" . $reload . "\">" . $prevlabel . "</a>\n</li>";
} else {
$out.= "<li><a href=\"" . $reload . "&page=" . ($page - 1) . "\">" . $prevlabel . "</a>\n</li>";
}
$pmin = ($page > $adjacents) ? ($page - $adjacents) : 1;
$pmax = ($page < ($tpages - $adjacents)) ? ($page + $adjacents) : $tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li class=\"active\"><a href=''>" . $i . "</a></li>\n";
} elseif ($i == 1) {
$out.= "<li><a href=\"" . $reload . "\">" . $i . "</a>\n</li>";
} else {
$out.= "<li><a href=\"" . $reload . "&page=" . $i . "\">" . $i . "</a>\n</li>";
}
}
if ($page < ($tpages - $adjacents)) {
$out.= "<a style='font-size:11px' href=\"" . $reload . "&page=" . $tpages . "\">" . $tpages . "</a>\n";
}
// next
if ($page < $tpages) {
$out.= "<li><a href=\"" . $reload . "&page=" . ($page + 1) . "\">" . $nextlabel . "</a>\n</li>";
} else {
$out.= "<span style='font-size:11px'>" . $nextlabel . "</span>\n";
}
$out.= "";
return $out;
}
答案 0 :(得分:0)
它不应该是POST
它应该是GET
因为你正在通过url传递search
,这就是你得到未定义索引的原因,尝试使用方法{{1}用于搜索
变化
GET
带
$search=$_POST["search"];
答案 1 :(得分:0)
在您的搜索中,您使用$ _POST发布值。请改用$ _GET,并将所有值与下一个或上一个链接连接起来。这应该解决问题
<form method="get" action="AF12P.php">
<input type="submit" name="submit" value="search">
<input type="text" name="search" />
</form>
并使用 PHP页面上的$ _GET而不是$ _POST
答案 2 :(得分:0)
我建议你在mysql查询中使用 LIMIT 。因此,点击页面后,它将加载没有unessesary行。 我的意思是改变你的查询:
$query="SELECT *, comment FROM comments AS c
JOIN wordtable2 AS w
ON c.w1 = w.w1
WHERE word like '%$search%'
ORDER BY word DESC
LIMIT $start_element,$page_limit";
$ page_limit =每页的商品数量。
$ start_element =查询中的项目数量,您的查询将受到限制。