memcache的搜索次数,每页数据存在memcache中,分页普通页面

(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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
    ?>
        <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>   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值