求个搜索引擎与分页代码 求高手给我个写好的搜索引擎分页, 我想要的分页类例如: 共15/300页 第一页 前一页 18 19 20 21 22 后一页 最后一页或是其他类也可以, 谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php // 默认一页显示十条记录function getAll( $mysql_result) // 取得共有多少页结果{ return ceil( mysql_num_rows( $mysql_result )/10 );}function getNow(){ if( isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>0 ) return $_GET['page']; else return 1; // 参数缺省则默认1}// 调用方法// $all = getAll( $mysqlResult ); // 查询返回的结果集$now = getNow();// $all 总共多少页// $now 当前所在的第几页// 查询数据库// 连接查询操作,我后面直接写sql// $sql = 你的原先的sql语句,再加上 $sqlForPage$sqlForPage = "LIMIT ".( ($now-1)*10 ) ." , 10" ;/* test$now = 20;$all = 100;*/// 下面分页的代码echo "共".$now."/".$all; // 共15/300页 echo "<a href=\"http://yourdomain.com/search.php?page=1\">第一页</a>";echo "<a href=\"http://yourdomain.com/search.php?page=".($now-1)."\">前一页</a>";for( $n=0; $n<5; $n++ ){ if( ($x = $now+$n) > 0 ) { echo "<a href=\"http://yourdomain.com/search.php?page=".$x."\">$x</a> "; }}echo "<a href=\"http://yourdomain.com/search.php?page=".($now+1)."\">后一页</a>";echo "<a href=\"http://yourdomain.com/search.php?page=".$all."\">第一页</a>";?> 还没好吗?至少你要把出现的错误给贴出来才能帮你分析,mysql_error(); 还有php的错误,总得有个为什么这样吧这样回避问题不是办法 我试回你之前给我的分页代码这是引擎<?phpinclude 'database.php';function search_results($search) { $returned_results = array(); $where = ""; $search = preg_split('/[\s]+/', $search); $total_keywords = count($search); foreach($search as $key=>$keyword) { $where .= " `keywords` LIKE '%$keyword%' ";if ($key != ($total_keywords - 1)) {$where .= " AND ";} } //唯一的改变就是下面// $pageNum 这是分页,比如第 1,2,3页,这个值由url传递的GET参数确定,默认是1; if( is_numeric( $_GET['pageNum'] ) && $_GET['pageNum'] >0 ) $pageNum = $_GET['pageNum']; // $fooNum 表示截取数据从哪一行开始,这个是从0开始,就像数组一样所以还要减1;// $listNum 表示一页显示多少数据,默认10行,你自己改$listNum = 10;$fooNum = ( ( $pageNum-1) * $listNum ) - 1; $results = "SELECT `tittle`, `description`, `link` FROM `search` WHERE $where LIMIT $fooNum ,$listNum";// 结束,再要输出分页HTML的话 $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( 'tittle' => $results_row['tittle'], 'description' => $results_row['description'], 'link' => $results_row['link'] );} return $returned_results; } }mysql_error();?>这是显示页<?php include 'function.php'; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body><form id="form1" name="form1" method="post" action=""> <label for="textfield"></label> <input type="text" name="keywords" id="keywords" value="<?php echo $_POST['keywords']; ?>" /> <label for="Submit"></label> <input type="submit" name="Submit" value="Search" id="Search" /></form><?phpif (isset($_POST['keywords'])) { $suffix = ""; $search = mysql_real_escape_string(htmlentities(trim($_POST['keywords']))); $errors = array(); if (empty($search)){ $results[] = 'Please Type Your Search Words!'; } else if (strlen($search)<0) { $errors[] = 'Your Word Minimun 1 Characters!'; } else if (search_results($search) === false) { $errors[] = 'Your search for <b>'.$search.'</b> no results'; } if (empty($errors)) { $results = search_results($search); $results_num = count($results); $suffix = ($results_num !=1) ? 's' : ''; echo '<p>Your search for <strong>', $search, '</strong> returned <strong>', $results_num, '</strong> result', $suffix, '</p>';?> <?php$prew = $pageNum>1 ? $pageNum-1 : 1;echo <<<html <a href="search.php?pageNum={$prew}">next</a>html; for ($i = 1; $i>10; $i++ ) { echo <<<html <a href="search.php?pageNum={$i}">$i</a>html; }$next = $pageNum + 1;echo <<<html <a href="search.php?pageNum={$next}">next</a>html;?><?php foreach($results as $result) { echo '<p> <strong>', $result['tittle'], '</strong> <br /> ', $result['description'], '... <br /> <a href="', $result['link'], '">', $result['link'], '</a> </p>'; } } else { foreach($errors as $error) { echo $error, '</br>'; } } }?><div id="page_results"><?php$prew = $pageNum>1 ? $pageNum-1 : 1;echo <<<html <a href="search.php?pageNum={$prew}">next</a>html; for ($i = 1; $i>10; $i++ ) { echo <<<html <a href="search.php?pageNum={$i}">$i</a>html; }$next = $pageNum + 1;echo <<<html <a href="search.php?pageNum={$next}">next</a>html;?></body></html>当我搜索是就是这样 简化了代码,在我的机器上测试已通过<?phpmysql_connect("localhost", 'root', ''); // 我测试用的mysql_select_db('hello');function search_results($search){global $pageNum; // 这个还要函数外要用/* $returned_results = array(); $where = ""; $search = preg_split('/[\s]+/', $search); $total_keywords = count($search); foreach($search as $key=>$keyword) { $where .= " `keywords` LIKE '%$keyword%' ";if ($key != ($total_keywords - 1)) $where .= " AND "; */ $where = "id = 1 "; // 这是我测试用的,所以把上面的过程注释了 // $pageNum 这是分页,比如第 1,2,3页,这个值由url传递的GET参数确定,默认是1; if( isset($_GET['pageNum']) && is_numeric( $_GET['pageNum'] ) && $_GET['pageNum'] >0 ) $pageNum = $_GET['pageNum']; else $pageNum = 1;// $fooNum 表示截取数据从哪一行开始,这个是从0开始,就像数组一样所以还要减1;// $listNum 表示一页显示多少数据,默认10行,你自己改$listNum = 10;$fooNum = ($pageNum-1) * $listNum; $sql = "SELECT title, description, link FROM search WHERE $where LIMIT $fooNum ,$listNum";// 结束,再要输出分页HTML的话$results = mysql_query($sql);$results_num = ($results) ? mysql_num_rows($results) : 0;if($results_num == 0) return "没有找到结果";while ($results_row = mysql_fetch_assoc($results)) { $returned_results[] = array( 'title' => $results_row['title'], 'description' => $results_row['description'], 'link' => $results_row['link'] ); } // 最后返回数组 return $returned_results ;} ?>include 'other/test.php'; // 只含 search_results 方法// 过滤 $suffix = " "; $keyM = trim($_GET['keywords']); // post我给改成get了 $keyN = htmlentities($keyM); $search = mysql_real_escape_string($keyN); // 参数是否合法 if (empty($search)) $errors = 'Please Type Your Search Words!'; else if (strlen($search)<=0) $errors = 'Your Word Minimun 1 Characters!'; else if (search_results($search) === false) $errors = 'Your search for <b>'.$search.'</b> no results'; // 不合法终止 if ( !empty($errors)) exit( $errors ); $results = search_results( $search ); $results_num = count($results); $suffix = ($results_num !=1) ? 's' : ''; echo '<p>Your search for <strong>'. $search. '</strong> returned <strong>'. $results_num. '</strong> result'. $suffix. '</p>';foreach( $results as $result) { echo '<p> <strong>'. $result['title']. '</strong> <br /> '. $result['description']. ' <br /> <a href="'. $result['link']. '">'. $result['link']. '</a> </p>'; }// 开始输出分页div echo "<div id=\"page_results\">";$prew = $pageNum>1 ? $pageNum-1 : 1;echo "<a href=\"search.php?pageNum=$prew\">last</a>"; for ($i = 1; $i<10; $i++ ) echo "<a href=\"search.php?pageNum=$i\">$i</a> ";$next = $pageNum + 1;echo "<a href=\"search.php?pageNum=$next\">next</a>";?> 帮看这个.htaccess文件啥意思啊 php生成json,我一个下午未解决,向各位好汉求助,此难不除,今晚无心吃饭。 apache在配置php时无法启动了 急! 高手指点,PHP上传的问题。 广州招聘PHP 和 MySQL 高手兼职 字段间的结果怎么显示名次 php执行shell脚本问题 如何隐藏免费Php空间的顶部广告?? 讨论:Win32下的PHP用不用把.dll复制到相应的目录下 关于cakephp抽奖转盘问题 一个数据库操作问题。 smarty模板中html_checkbox怎么用?
<?php // 默认一页显示十条记录
function getAll( $mysql_result) // 取得共有多少页结果
{
return ceil( mysql_num_rows( $mysql_result )/10 );
}
function getNow()
{
if( isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>0 )
return $_GET['page'];
else
return 1; // 参数缺省则默认1
}// 调用方法
// $all = getAll( $mysqlResult ); // 查询返回的结果集
$now = getNow();
// $all 总共多少页
// $now 当前所在的第几页
// 查询数据库
// 连接查询操作,我后面直接写sql
// $sql = 你的原先的sql语句,再加上 $sqlForPage
$sqlForPage = "LIMIT ".( ($now-1)*10 ) ." , 10" ;/* test
$now = 20;
$all = 100;
*/
// 下面分页的代码
echo "共".$now."/".$all; // 共15/300页
echo "<a href=\"http://yourdomain.com/search.php?page=1\">第一页</a>";
echo "<a href=\"http://yourdomain.com/search.php?page=".($now-1)."\">前一页</a>";
for( $n=0; $n<5; $n++ )
{
if( ($x = $now+$n) > 0 )
{
echo "<a href=\"http://yourdomain.com/search.php?page=".$x."\">$x</a> ";
}
}
echo "<a href=\"http://yourdomain.com/search.php?page=".($now+1)."\">后一页</a>";
echo "<a href=\"http://yourdomain.com/search.php?page=".$all."\">第一页</a>";
?>
至少你要把出现的错误给贴出来才能帮你分析,mysql_error(); 还有php的错误,总得有个为什么这样吧这样回避问题不是办法
这是引擎
<?php
include 'database.php';function search_results($search) {
$returned_results = array();
$where = "";
$search = preg_split('/[\s]+/', $search);
$total_keywords = count($search);
foreach($search as $key=>$keyword) {
$where .= " `keywords` LIKE '%$keyword%' ";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";
}
}
//唯一的改变就是下面
// $pageNum 这是分页,比如第 1,2,3页,这个值由url传递的GET参数确定,默认是1;
if( is_numeric( $_GET['pageNum'] ) && $_GET['pageNum'] >0 )
$pageNum = $_GET['pageNum'];
// $fooNum 表示截取数据从哪一行开始,这个是从0开始,就像数组一样所以还要减1;
// $listNum 表示一页显示多少数据,默认10行,你自己改
$listNum = 10;
$fooNum = ( ( $pageNum-1) * $listNum ) - 1;
$results = "SELECT `tittle`, `description`, `link` FROM `search` WHERE $where LIMIT $fooNum ,$listNum";
// 结束,再要输出分页HTML的话 $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(
'tittle' => $results_row['tittle'],
'description' => $results_row['description'],
'link' => $results_row['link']
);
}
return $returned_results;
}
}
mysql_error();
?>
这是显示页
<?php include 'function.php'; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head><body>
<form id="form1" name="form1" method="post" action="">
<label for="textfield"></label>
<input type="text" name="keywords" id="keywords" value="<?php echo $_POST['keywords']; ?>" />
<label for="Submit"></label>
<input type="submit" name="Submit" value="Search" id="Search" />
</form><?php
if (isset($_POST['keywords'])) {
$suffix = "";
$search = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$errors = array();
if (empty($search)){
$results[] = 'Please Type Your Search Words!';
} else if (strlen($search)<0) {
$errors[] = 'Your Word Minimun 1 Characters!';
} else if (search_results($search) === false) {
$errors[] = 'Your search for <b>'.$search.'</b> no results';
} if (empty($errors)) {
$results = search_results($search);
$results_num = count($results);
$suffix = ($results_num !=1) ? 's' : '';
echo '<p>Your search for <strong>', $search, '</strong> returned <strong>', $results_num, '</strong> result', $suffix, '</p>';
?>
<?php
$prew = $pageNum>1 ? $pageNum-1 : 1;echo <<<html
<a href="search.php?pageNum={$prew}">next</a>
html; for ($i = 1; $i>10; $i++ )
{
echo <<<html
<a href="search.php?pageNum={$i}">$i</a>
html;
}
$next = $pageNum + 1;echo <<<html
<a href="search.php?pageNum={$next}">next</a>
html;
?>
<?php
foreach($results as $result) {
echo '<p> <strong>', $result['tittle'], '</strong> <br /> ', $result['description'], '... <br /> <a href="', $result['link'], '">', $result['link'], '</a> </p>';
} } else {
foreach($errors as $error) {
echo $error, '</br>';
}
}
}
?>
<div id="page_results">
<?php
$prew = $pageNum>1 ? $pageNum-1 : 1;echo <<<html
<a href="search.php?pageNum={$prew}">next</a>
html; for ($i = 1; $i>10; $i++ )
{
echo <<<html
<a href="search.php?pageNum={$i}">$i</a>
html;
}
$next = $pageNum + 1;echo <<<html
<a href="search.php?pageNum={$next}">next</a>
html;
?>
</body>
</html>当我搜索是就是这样
<?phpmysql_connect("localhost", 'root', ''); // 我测试用的
mysql_select_db('hello');function search_results($search)
{
global $pageNum; // 这个还要函数外要用/*
$returned_results = array();
$where = "";
$search = preg_split('/[\s]+/', $search);
$total_keywords = count($search);
foreach($search as $key=>$keyword) {
$where .= " `keywords` LIKE '%$keyword%' ";
if ($key != ($total_keywords - 1))
$where .= " AND ";
*/
$where = "id = 1 "; // 这是我测试用的,所以把上面的过程注释了
// $pageNum 这是分页,比如第 1,2,3页,这个值由url传递的GET参数确定,默认是1;
if( isset($_GET['pageNum']) && is_numeric( $_GET['pageNum'] ) && $_GET['pageNum'] >0 )
$pageNum = $_GET['pageNum'];
else
$pageNum = 1;// $fooNum 表示截取数据从哪一行开始,这个是从0开始,就像数组一样所以还要减1;
// $listNum 表示一页显示多少数据,默认10行,你自己改
$listNum = 10;
$fooNum = ($pageNum-1) * $listNum;
$sql = "SELECT title, description, link FROM search WHERE $where LIMIT $fooNum ,$listNum";
// 结束,再要输出分页HTML的话$results = mysql_query($sql);$results_num = ($results) ? mysql_num_rows($results) : 0;if($results_num == 0)
return "没有找到结果";
while ($results_row = mysql_fetch_assoc($results))
{
$returned_results[] = array(
'title' => $results_row['title'],
'description' => $results_row['description'],
'link' => $results_row['link']
);
}
// 最后返回数组
return $returned_results ;
}
?>include 'other/test.php'; // 只含 search_results 方法// 过滤
$suffix = " ";
$keyM = trim($_GET['keywords']); // post我给改成get了
$keyN = htmlentities($keyM);
$search = mysql_real_escape_string($keyN); // 参数是否合法
if (empty($search))
$errors = 'Please Type Your Search Words!';
else if (strlen($search)<=0)
$errors = 'Your Word Minimun 1 Characters!';
else if (search_results($search) === false)
$errors = 'Your search for <b>'.$search.'</b> no results'; // 不合法终止
if ( !empty($errors))
exit( $errors ); $results = search_results( $search );
$results_num = count($results);
$suffix = ($results_num !=1) ? 's' : '';
echo '<p>Your search for <strong>'. $search. '</strong>
returned <strong>'. $results_num. '</strong> result'. $suffix. '</p>';
foreach( $results as $result) {
echo '<p> <strong>'. $result['title']. '</strong> <br /> '.
$result['description']. ' <br /> <a href="'. $result['link'].
'">'. $result['link']. '</a> </p>';
}// 开始输出分页div
echo "<div id=\"page_results\">";$prew = $pageNum>1 ? $pageNum-1 : 1;
echo "<a href=\"search.php?pageNum=$prew\">last</a>"; for ($i = 1; $i<10; $i++ )
echo "<a href=\"search.php?pageNum=$i\">$i</a> ";$next = $pageNum + 1;
echo "<a href=\"search.php?pageNum=$next\">next</a>";?>