(search.php)
<?php
header("content-type:text/html;charset=utf-8");//设置当前页
$page=isset($_GET['page'])?$_GET['page']:1;
$memcache=new Memcache();
$memcache->connect("127.0.0.1",11211);
// $memcache->delete("$page");die;
//判断数据是否存在memcache中
if($memcache->get("$page")){
// echo "已经存在第 $page 页的数据";
$arr=$memcache->get("$page");
}else{
// echo "数据不存在,正在读取第 $page 页的数据";
$pdo=new PDO("mysql:host=127.0.0.1;dbname=seven_month","root","root");
$pdo->exec("set names utf8");
$num=$pdo->query("select count(*) as num from exam2")->fetchAll(PDO::FETCH_ASSOC);
//总数
$num=$num[0]['num'];
//每页显示
$pagenum=10;
//多少页
$pagetotal=ceil($num/$pagenum);
//偏移量
$limit=($page-1)*$pagenum;
$arr=$pdo->query("select * from exam2 limit $limit,$pagenum")->fetchAll(PDO::FETCH_ASSOC);
$memcache->set("$page",$arr);
}
?>
<center>
<form action="search1.php" method="post">
<tr>
<td><input type="text" name="title" ></td>
<td><input type="submit" value="查询"></td>
</tr>
</form>
<table border="1">
<tr>
<th>标题</th>
<th>地址</th>
<th>详情</th>
</tr>
<?php foreach ($arr as $key => $val): ?>
<tr>
<td><?php echo $val['title'] ?></td>
<td><?php echo $val['address'] ?></td>
<td><?php echo $val['content'] ?></td>
</tr>
<?php endforeach ?>
</table>
<?php
$up=$page-1;
$down=$page+1;
?>
<a href="search.php?page=1">首页</a>
<a href="search.php?page=<?php echo $up ?>">上一页</a>
<a href="search.php?page=<?php echo $down ?>">下一页</a>
<a href="search.php?page=<?php echo $pagetotal ?>">尾页</a>
</center>
(search1.php)
<?php
header("content-type:text/html;charset=utf-8");
//设置当前页
$page=isset($_GET['page'])?$_GET['page']:1;
$memcache=new Memcache();
$memcache->connect("127.0.0.1",11211);
$pdo=new PDO("mysql:host=127.0.0.1;dbname=seven_month","root","root");
$pdo->exec("set names utf8");
$num=$pdo->query("select count(*) as num from exam2")->fetchAll(PDO::FETCH_ASSOC);
//总数
$num=$num[0]['num'];
//每页显示
$pagenum=10;
//多少页
$pagetotal=ceil($num/$pagenum);
//偏移量
$limit=($page-1)*$pagenum;
$title=$_POST['title'];
$memcache=new Memcache();
$memcache->connect("127.0.0.1",11211);
//检测memcache是否做过搜索
if($memcache->get("title")){
$arr=$memcache->get("title");
//检查要存入的搜索关键字是否存在这个数组中,如果存在就追加
if(array_key_exists("$title",$arr)){
$arr["$title"]=$arr["$title"]+1;
//更新memcache中的内容
$memcache->replace("title",$arr);
}else{
$arr["$title"]=1;
$memcache->replace("title",$arr);
}
}else{
$memcache->set("title",array("$title"=>1));
}
$arr=$pdo->query("select * from exam2 where title like '%$title%' limit $limit,$pagenum")->fetchAll(PDO::FETCH_ASSOC);
?>
<center>
<form action="search1.php" method="post">
<tr>
<td><input type="text" name="title" ></td>
<td><input type="submit" value="查询"></td>
</tr>
</form>
<?php
$search_word=$memcache->get("title");
arsort($search_word);
foreach ($search_word as $key => $val) {
echo "<span>$key ($val)</span> ";
}
?>
<table border="1">
<tr>
<th>标题</th>
<th>地址</th>
<th>详情</th>
</tr>
<?php foreach ($arr as $key => $val): ?>
<tr>
<td><?php echo $val['title'] ?></td>
<td><?php echo $val['address'] ?></td>
<td><?php echo $val['content'] ?></td>
</tr>
<?php endforeach ?>
</table>
<?php
$up=$page-1;
$down=$page+1;
?>
<a href="search.php?page=1">首页</a>
<a href="search.php?page=<?php echo $up ?>">上一页</a>
<a href="search.php?page=<?php echo $down ?>">下一页</a>
<a href="search.php?page=<?php echo $pagetotal ?>">尾页</a>
</center>