[英]search engine and pagination
Hello Friends I want to know how to add pagination in search engine. 您好朋友我想知道如何在搜索引擎中添加分页。 I have tried so many times to add pagination in search engine but every time i was fail. 我已经尝试了很多次在搜索引擎中添加分页,但每次我都失败了。 Is there anybody who modify my following code to add pagination: 是否有人修改我的以下代码以添加分页:
index.php 的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 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;
}
}
?>
And i have one more question to make a close button at the top of the Div for closing itself. 我还有一个问题是在Div的顶部做一个关闭按钮以关闭它自己。
I'd amend the function signature to include page number and number of results per page: 我修改了函数签名,以包含每页的页码和结果数:
function search_results($keywords,$pagenum,$resultsperpage) {
and then amend the SQL to pull out only the results for the page in question: 然后修改SQL以仅提取相关页面的结果:
$startresult=($pagenum-1)*$resultsperpage;
$results = "SELECT `title`, LEFT(`description`, 70) as `description`, `url` FROM `articles` WHERE $where LIMIT $startresult,$resultsperpage";
(assuming you'd be numbering you pages starting from 1
) (假设你要从1
开始为你的页面编号)
Then all you'd need is a list of links to let the users select their page. 然后,您只需要一个链接列表,让用户选择他们的页面。 I normally use a GET
variable for this ( searchpage.php?p=1
maybe), so you'd have to sanitize that as you have done with your search terms. 我通常使用GET
变量( searchpage.php?p=1
),所以你必须像对待搜索条件那样清理它。
Please not remember Likes 请不要记得喜欢
<?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 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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.