我正在尝试在我的代码中实现分页。它不能很好地工作:
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;
}