showList.html
<!DOCTYPE html>
<html lang="en" id="box">
<head>
<meta charset="UTF-8">
<title>展示</title>
<base href="<?php echo base_url("/public/") ?>">
</head>
<body>
<center>
<tr>
<td>标题<input type="text" id="title" name="title" value="<?php echo $title ?>"></td><br>
<td>作者<input type="text" id="author" name="author" value="<?php echo $author ?>"></td><br>
<td>时间<input type="text" id="start_time" name='start_time' value="<?php echo $start_time ?>">-<input type="text" id="end_time" name="end_time" value="<?php echo $end_time ?>"></td><br>
<td><button id="button">查询</button></td>
</tr>
<table border="1">
<tr>
<th>标题</th>
<th>时间</th>
<th>地址</th>
<th>作者</th>
</tr>
<?php foreach ($data as $key => $val): ?>
<tbody class="tbody">
<td><?php echo $val['title'] ?></td>
<td><?php echo $val['time'] ?></td>
<td><?php echo $val['link'] ?></td>
<td><?php echo $val['author'] ?></td>
</tbody>
<?php endforeach ?>
</table>
<?php echo $page ?>
</center>
</body>
</html>
<script src="jquery-2.1.4.min.js"></script>
<script>
$("#button").click(function(){
var title=$("#title").val();
var author=$("#author").val();
var start_time=$("#start_time").val();
var end_time=$("#end_time").val();
$.ajax({
type: "POST",
url: "<?php echo site_url('Month10/showList') ?>",
data: {title:title,author:author,start_time:start_time,end_time:end_time},
success: function(msg){
$("#box").html(msg);
}
});
})
</script>
Month10.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Month10 extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('News_model',"news");
}
public function index(){
$url="http://wenxue.yjbys.com/xiaoshuo/";
$a=file_get_contents($url);
$a= iconv('GB2312', 'UTF-8', $a);
$preg='#<dt>.*<span class="time">(.*)</span>.*<a href="(.*)" target="_blank">(.*)</a>.*</dt>#isU';
preg_match_all($preg,$a,$arr);
//获取文章的url
$url2=$arr[2];
$arr['time']=$arr[1];
for($i=0;$i<count($arr['time']);$i++){
$arr['time'][$i]=substr($arr['time'][$i],13);
}
// print_r($arr['time']);die;
$arr['title']=$arr[3];
$arr['link']=$arr[2];
//循环取出每页的文章信息
for($i=0;$i<count($url2);$i++){
$a1=file_get_contents($url2[$i]);
$preg1='#<span class="from">(.*)</span>#isU';
preg_match_all($preg1,$a1,$arr1);
}
//作者
$arr1['author']=$arr1[1][0];
$arr1['author']=iconv('GB2312','UTF-8',$arr1['author']);
$arr1['author']=substr($arr1['author'],9);
//文章
// for($i=0;$i<count($url2);$i++){
// $a2=file_get_contents($url2[$i]);
// $preg2='#<div class="blank1">.*</div>.*<div class="ad_top_left2">.*</div>(.*)<div style="text-align: center;">#isU';
// preg_match_all($preg2,$a2,$arr2);
// print_r($arr2);
// }
/*$rr = strip_tags("<p><strong> 浴火城里。</strong></p>",'<img><a><p><br> ');
echo $rr;*/
$sql="insert into month10 (time,author,link,title) values";
// print_r($arr1);die;
for ($i=0; $i < count($arr['title']); $i++) {
$sql.="('".mysql_escape_string($arr['time'][$i])."','".mysql_escape_string($arr1['author'])."','".mysql_escape_string($arr['link'][$i])."','".mysql_escape_string($arr['title'][$i])."'),";
}
$sql=substr($sql,0,-1);
// echo $sql;die;
$res=$this->db->conn_id->exec("set names utf8");
$res=$this->db->conn_id->exec($sql);
if($res){
redirect("Month10/showList");
}else{
echo "添加失败";
}
}
/**
* 列表展示
*/
public function showList(){
$page = $this->uri->segment(3,1);
//分页的每页显示条数
$page_num=5;
$count=count($this->db->conn_id->query("select * from month10")->fetchAll(PDO::FETCH_ASSOC));
//连接memcache
$memcache=new Memcache;
$memcache->connect("127.0.0.1",11211);
$title=$this->input->post("title");
$author=$this->input->post("author");
$start_time=$this->input->post("start_time");
$end_time=$this->input->post("end_time");
$where=1;
if($title!=""){
$where.=" and title like '%$title%'";
}
if($author!=""){
$where.=" and author like '%$author%'";
}
if($start_time!=""&&$end_time!=""){
$where.=" and time between '$start_time' and '$end_time'";
}
//分页
$pages=ceil($count/$page_num);
$prev=$page<=1?1:$page-1;
$next=$page>=$pages?$pages:$page+1;
//偏移量
$limit=($page-1)*$page_num;
// $memcache->delete("search");die;
//检测memcache是否做过搜索
if($memcache->get("search")){
$arr=$memcache->get("search");
//检查要存入的搜索关键字是否存在这个数组中,如果存在就追加
if(array_key_exists("$where.'_'.$page",$arr)){
$data=$arr["$where.'_'.$page"];
echo "缓存读取";
$this->load->vars("data",$data);
}else{
$data=$this->db->conn_id->query("select * from month10 where $where limit $limit,$page_num")->fetchAll(PDO::FETCH_ASSOC);
$res["$where.'_'.$page"]=$data;
$memcache->replace("search",$res);
$this->load->vars("data",$data);
}
}else{
echo "这个数据库的数据";
$data=$this->db->conn_id->query("select * from month10 where $where limit $limit,$page_num")->fetchAll(PDO::FETCH_ASSOC);
$memcache->set("search",array("$where.'_'.$page"=>$data));
$this->load->vars("data",$data);
}
$data=$this->db->conn_id->query("select * from month10 where $where limit $limit,$page_num")->fetchAll(PDO::FETCH_ASSOC);
$url=base_url()."Month10/showList";
$links='<a href="'.$url.'/1.html">首页</a>
<a href="'.$url.'/'.$prev.'.html">上一页</a>
<a href="'.$url.'/'.$next.'.html">下一页</a>
<a href="'.$url.'/'.$pages.'.html">尾页</a>';
$this->load->vars('page',$links);
$this->load->vars('title',$title);
$this->load->vars('author',$author);
$this->load->vars('start_time',$start_time);
$this->load->vars('end_time',$end_time);
$this->load->view("month/showList.html");
}
}