我正在尝试使用分页选项制作PHP搜索脚本,它工作正常,直到我们在第一页上,如果我输入搜索' car',但是当转到下一页搜索查询其余为非,并显示数据库中可用的所有结果
这是我的PHP脚本
$_SESSION['word'] = $_POST['term'];
$term = $_SESSION['word'];
$term = explode(" ",$term);
if (count($term) > 0) {
$Where = '';
foreach($term as $Item) {
$Where .= "Title like '%$Item%' OR ";
}
$Where = substr($Where,0,-4);
$sql = "SELECT COUNT(ID) FROM data where $Where";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 15;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){ $last = 1; }
// Establish the $pagenum variable
$pagenum = 1;
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){ $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); }
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { $pagenum = 1; } else if ($pagenum > $last) { $pagenum = $last; }
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT * FROM data where $Where ORDER BY RAND() $limit";
$query = mysqli_query($con, $sql);
if (!$query) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
// This shows the user what page they are on, and the total number of pages
$textline1 = "<div id='toptext'>Total Videos Found: (<b>$rows</b>)</div>"; $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results if($last != 1){ /* First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. */
if ($pagenum > 1) { $previous = $pagenum - 1; $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){ if($i > 0){ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> '; } } }
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> '; if($i >= $pagenum+4){ break; } }
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {{ $next = $pagenum + 1; $paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> '; } } $list = '';
$width = "200";
$hight = "150";
while($row = mysqli_fetch_array($query, MYSQL_ASSOC))
{
$post_id = $row['ID'];
$title = $row['Link'];
$ti = $row['Title'];
$pic = $row['img'];
$list .= '<div style="text-align:center" id="body"><div id="tablealign"><a href="Video.php?ID='.$post_id.'"><img id="image" src='.$pic.' width='.$width.' height='. $hight.'></a></div><div id="text"><a href="Video.php?ID='.$post_id.'">'.wordwrap($ti, 30, "<br />\n").'</a></div></div>';
}}
// Close your database connection
mysqli_close($con); ?>
<!DOCTYPE html> <html>
<?php echo $textline1; ?> <p><?php echo $textline2; ?><center><div id="pagelayout" >
<div id="pagenum"> <?php echo $paginationCtrls; ?></body></html></div></div></center></p> <p><?php echo $list; ?></p>
这是html代码
<form action="process.php" class="searchform" method="post" id="search-form">
<input type="text" class="searchfield" placeholder="Search..." name="term" />
<input type="submit" name="submit" class="searchbutton" value="GO" >
</form>
查看演示 http://vidupdatez.com/Demo/Search.php 搜索汽车,然后点击第2页,您将看到问题是什么