有人可以给我个搜索引擎可以配合这分页引擎吗? (急要)!!!
<?php
include('database.php'); $tableName="search";
$targetpage = "index.php";
$limit = 5;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo $total_pages.' Results';
// pagination
echo $paginate;
?>
<?php
include('database.php'); $tableName="search";
$targetpage = "index.php";
$limit = 5;
$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo $total_pages.' Results';
// pagination
echo $paginate;
?>
多的是,然后根据类所需要的数据自己查出来,然后调用类方法就能实现了。
我要好像这样的分页 例如:共1251页 prev 1 2 ... 8 9 10 11 12 ... 29 30 next 求!!
这是我的引擎
<?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 ";
}
}
$results = "SELECT `tittle`, LEFT(`description`, 80) as `description`, `link` FROM `search` 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(
'tittle' => $results_row['tittle'],
'description' => $results_row['description'],
'link' => $results_row['link']
);
}
return $returned_results;
}
}
?>
这是我的主页
<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
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>';
}
}
}
?>
<?php
class Pager
{
var $_perPage; //每一页记录数 var $_totalItems; //总记录数 var $_totalPages; //总共页数 var $_currentPage; // 当前页序号 var $_listStart; //当前头条记录ID var $_listEnd; //当前最后条记录ID var $_pageStart; //第一页 var $_pageEnd; //最后页 var $_listPage; //循环显示当前分页列表 var $_pageData; //当前要显示的记录 var $_linkData; //页面传递的变量 var $_startId; //从数据库读出的启始id function pager($_totalItems, $_perPage, $_linkData)
{
$this->_currentPage = max((int)@$_GET['pageId'], 1); //当前的页序号 $this->_perPage = $_perPage; //每一页记录数 $this->_totalItems = $_totalItems; //总记录数 $this->_totalPages = ceil($this->_totalItems / $this->_perPage); //总页数 $this->_linkData = $_linkData; //传递的变量 $this->_listStart = ($this->_currentPage - 1) * $this->_perPage + 1; //当前头条记录ID $this->_listEnd = ($this->_currentPage != $this->_totalPages) ? $this->_currentPage * $this->_perPage : $this->_totalItems; //当前最后条记录ID $this->_startId = $this->_listStart -1; //从数据库读出的启始id }
function startId() // 从数据库读出的启始id {
return $this->_startId;
}
function totalItems() // 总记录数 {
return $this->_totalItems;
}
function totalPages() // 总页数 {
return $this->_totalPages;
}
function listStart() // 当前头条记录ID {
return $this->_listStart;
}
function listEnd() // 当前最后条记录ID {
return $this->_listEnd;
}
function currentPage() // 当前页 {
return $this->_currentPage;
}
function pageStart() // 第一页 {
return $this->_pageStart = "<a href=" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId=1><FONT title=首页 face=webdings>9</FONT></a>";
}
function pageEnd() // 最后页 {
return $this->_pageEnd = "<a href=" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId=" . $this->_totalPages . "><FONT title=尾页 face=webdings>:</FONT></a>";
}
function upPage() // 上一页 {
if ($this->_currentPage > 1 and $this->_currentPage <= $this->_totalPages)
{
$up = $this->_currentPage-1;
return $this->_upPage = "<a href=" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId=" . $up . "><FONT title=上页 face=webdings>7</FONT></a>";
}
}
function downPage() // 下一页 {
$down = $this->_currentPage + 1;
if ($this->_currentPage < $this->_totalPages)
return $this->_downPage = "<a href=" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId=" . $down . "><FONT title=下页 face=webdings>8</FONT></a>";
}
function listPage() // 循环显示当前分页列表 {
if ($this->_currentPage <= 5 and $this->_currentPage != "")
{
$start = 1;
if ($this->_totalPages < 10) $end = $this->_totalPages;
else $end = 10;
} elseif (($this->_currentPage + 5) > $this->_totalPages)
{
$start = $this->_currentPage - 5;
$end = $this->_totalPages;
}
else
{
$start = $this->_currentPage - 4;
$end = $this->_currentPage + 5;
}
for($i = $start; $i <= $end; $i++)
{
$this->_listPage .= "<a href=" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId=" . $i . ">" . $i . "</a> ";
}
return $this->_listPage;
}
function jumpPage() // 跳转 {
$this->_jumpPage = "<table border=0 width=100% class='fonts'><tr><td>跳转 <select name='pageId' style='height=14px' onchange=\"location='" . $_SERVER['PHP_SELF'] . ($this->_linkData) . "&pageId='+this.value;\">"; //页面跳转 for($i = 1; $i <= $this->_totalPages; $i++)
{
if ($i == $this->_currentPage) $selected = "selected";
$this->_jumpPage .= "<option value='$i' $selected>$i</option>";
unset($selected);
}
return $this->_jumpPage .= "</select> 页</td></tr></table>";
}
}
// /范例$totalItems = 1100; //总记录$page = new Pager($totalItems, 20, "?a=b&c=6");
for($i = $page->startId(); $i <= $page->startId() + 20; $i++)
{
$data[] = array($i, $i + 1);
}
echo "当前开始ID:" . $listStart = $page->listStart();
echo "<br>";
echo "当前结束ID:" . $listEnd = $page->listEnd();
echo "<br>";
echo "第一页:" . $pageStart = $page->pageStart();
echo "<br>";
echo "最后页:" . $pageEnd = $page->pageEnd();
echo "<br>";
echo "上一页:" . $upPage = $page->upPage();
echo "<br>";
echo "下一页:" . $downPage = $page->downPage();
echo "<br>";
echo "列表显示:" . $listPage = $page->listPage();
echo "<br>";
echo "跳转页:" . $jumpPage = $page->jumpPage();
echo "<br>";
// print_r($data);?>
/**
PS:网上多的是。这是复制过来的。
估计这个更适合你:http://www.oschina.net/code/snippet_162279_5852
*/