我需要创建一个网站,允许用户填写搜索字段(例如标题和位置),然后单击搜索。然后,我想通过分页显示结果。
我使用了如下的php代码。当我填写搜索字段并单击搜索按钮时,第一页显示10条结果。可以将下一页定向到正确的链接,但是它们为空白。代码如下所示。
<?php
include 'dbinfo.php';
//set results per page
$limit = 10;
//check if search field has been filled
if ( isset($_POST['submit-search']) && ( ($_POST['discription']) )
{
$discription = mysqli_real_escape_string($db, $_POST['discription']);
//find the number of total pages
if ($discription)
{
$sql1="SELECT * FROM article WHERE a_title LIKE '%$discription%' ORDER BY a_date DESC";
}
$result1 = mysqli_query($db, $sql1);
$queryResult1 = mysqli_num_rows($result1);
//the number of total pages is set as $pages
$pages = ceil($queryResult1 / $limit);
$page = isset($_GET['page'])? $_GET['page']:1;
$start = ($page - 1) * $limit;
//run the query again with LIMIT $start, $limit instructions
if ($discription)
{
$sql="SELECT * FROM article WHERE a_title LIKE '%$discription%' ORDER BY a_date DESC LIMIT $start, $limit";
}
$result = mysqli_query($db, $sql);
$queryResult=mysqli_num_rows($result);
//display the results in each page
if ($queryResult1 > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
echo "
<h3>" . $row['a_title'] . "</h3></a>
";
}
}
if ($queryResult == 0)
{
echo "There are no results matching your search.";
}
if ($queryResult == "")
{
echo "";
}
// display the links to the pages
for ($i = 1; $i <= $pages; $i++)
{
echo '<a href="search.php?page=' . $i . '">' . $i . '</a> ';
}
}
?>