Hello Friends我想知道如何在搜索引擎中添加分页。我已经尝试了很多次在搜索引擎中添加分页,但每次我都失败了。是否有人修改我的以下代码以添加分页:
的index.php
<?php include 'func.inc.php'; ?>
<html>
<head>
<title>Search</title>
</head>
<body>
<h2> Search </h2>
<form action="" method="POST">
<p>
<input type="text" autocomplete="off" name="keywords" /> <input type="submit" value="Search" />
</p>
</form>
<?php
if (isset($_POST['keywords'])) {
$suffix = "";
$keywords = mysql_real_escape_string (htmlentities (trim($_POST['keywords'])));
$errors = array();
if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search term must be three or more character';
} else if (search_results($keywords) === false) {
$errors[] = 'Your search for ' .$keywords. ' returned no result';
}
if (empty($errors)) {
$results = search_results ($keywords);
$results_num = count ($results);
$suffix = ($results_num !=1) ? 's' : '';
echo '<p> Your search for <strong>', $keywords,'</strong> returned <strong>',$results_num,'</strong> result',$suffix,'</p>';
foreach($results as $result) {
echo '<p><strong><a href="',$result['url'],'">', $result['title'],'</a></strong> <br>', $result['description'],'...<br>', $result['url'],'</p>';
}
} else {
foreach ($errors as $error) {
echo $error, '</br>';
}
}
}
?>
</body>
</html>
func.inc.php
<?php
include 'db.inc.php';
function search_results($keywords) {
$returned_results = array();
$where ="";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach ($keywords as $key=>$keyword) {
$where .="`keywords` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= "AND";
}
}
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0;
if ($results_num === 0) {
return false;
} else {
while ($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
'title' => $results_row['title'],
'description' => $results_row['title'],
'url' => $results_row['url']
);
}
return $returned_results;
}
}
?>
我还有一个问题是在Div的顶部制作一个关闭按钮以关闭自己。
答案 0 :(得分:3)
我修改了函数签名,以包含每页的页码和结果数:
function search_results($keywords,$pagenum,$resultsperpage) {
然后修改SQL以仅提取相关页面的结果:
$startresult=($pagenum-1)*$resultsperpage;
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where LIMIT $startresult,$resultsperpage";
(假设您要为从1
开始的页面编号)
然后您需要的是一个链接列表,让用户选择他们的页面。我通常会使用GET
变量(也许searchpage.php?p=1
),因此您必须像对待搜索字词一样对其进行消毒。
答案 1 :(得分:0)
请不要记得喜欢
<?php
include 'func.php';
include 'config.inc.php';
?>
<form action="" method="POST">
<p>
<input type="text" autocomplete="off" name="keywords" > <input type="submit" value="Search" />
</p>
</form>
<?php
$suffix = "";
$v='';
if(isset($_POST['keywords'])){$v=$_POST['keywords'];}
else{
if(isset($_GET['q'])){$v=$_GET['q'];}
}
$keywords = mysql_real_escape_string (htmlentities (trim($v)));
$errors = array();
if (empty($keywords)) {
$errors[] = 'Please enter a search term';
} else if (strlen($keywords)<3) {
$errors[] = 'Your search term must be three or more character';
} else if (search_results($keywords) === false) {
$errors[] = 'Your search for ' .$keywords. ' returned no result';
}
if (!($errors)) {
$results = search_results($keywords);
$results_num = count ($results);
$suffix = ($results_num !=1) ? 's' : '';
echo '<p> Your search for <strong>', $keywords,'</strong> returned <strong>',$results_num,'</strong> result',$nom,'</p>';
foreach($results as $result) {
echo '<p><strong><a href="',$result['message'],'">', $result['name'],'</a></strong> <br>', $result['id'],'...<br>', $result['message'],'</p>';
}
if($pages>=1 && $page<=$pages){
for($x=1;$x<=$pages;$x++){
echo ($x==$page) ? '<strong><a href="?page='.$x.'&q='.$v.'" >'.$x.'</a></strong> ' : '<a href="?page='.$x.'&q='.$v.'" >'.$x.'</a> ';
}
}
} else {
foreach ($errors as $error) {
echo $error, '</br>';
}
}
?>
func.in.php
<?php
function search_results($keywords) {
global $page,$start,$pages_query,$per_page,$pages,$returned_results,$total_keywords,$nom;
$returned_results = array();
$where ="";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach ($keywords as $key=>$keyword) {
$where .="`message` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= "AND";
}
}
$per_page=6;
$pages_query = mysql_query(' SELECT COUNT(`id`) FROM `paginate` ') or die(mysql_error());
//$pages=ceil(mysql_result($pages_query,0) / $per_page);
$resultt = "SELECT * FROM paginate WHERE $where ";
$queryt =mysql_query($resultt) ;
$nom=mysql_num_rows($queryt);
$pages=ceil($nom / $per_page);
$page= (isset($_GET['page'])) ? (int)$_GET['page'] :1;
$start=($page - 1) * $per_page;
$results = "SELECT * FROM paginate WHERE $where LIMIT $start,$per_page ";
$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0;
if ($results_num === 0) {
return false;
} else {
while ($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
'id' => $results_row['id'],
'name' => $results_row['name'],
'message' => $results_row['message']
);
}
return $returned_results;
}
}
?>