1. 程式人生 > >生成靜態頁面

生成靜態頁面

<?php
/**
* Created by PhpStorm.
* User: song tong jing
* Date: 2018/11/3
* Time: 8:33
*/
class PdoClass
{
protected $_pdo;
public function __construct()
{
$this->_pdo = new PDO("mysql:host=127.0.0.1;dbname=student","root","root");
}
//分頁
public function fen_ye($page)
{
$count1=$this->_pdo->query("select count(*) from news")->fetchColumn();
$tiao=3;
$zong_page=ceil($count1/$tiao);
// return $zong_page;
$limit=($page-1)*$tiao;
$sql="select * from news limit $limit, $tiao";
$re= $this->_pdo->query($sql)->fetchAll();
$show['shou_page']=1;
$show['shang_page']=$page-1<1?1:$page-1;
$show['xia_page']=$page+1>$zong_page?$zong_page:$page+1;
$show['zong_page']=$zong_page;
$list=['re'=>$re,'show'=>$show];
return $list;
}
//查詢多條
public function sele($title='')
{
if(!empty($title))
{
$sql="select * from news where title LIKE '%$title%'";
return $this->_pdo->query($sql)->fetchAll();
}
$sql="select * from news";
return $this->_pdo->query($sql)->fetchAll();
}
//查詢單條
public function getsel($id)
{
$sql="select * from news where id='$id'";
return $this->_pdo->query($sql)->fetch();
}
//刪除
public function delete($id)
{
$sql="delete from news where id=$id";
$this->_pdo->exec($sql);
return true;
}
//新增
public function add($title,$content,$time)
{
$sql="insert into news VALUES (null,$title,$content,$time)";
$this->_pdo->exec($sql);
return true;
}
//修改
public function update($id,$title,$content)
{
$sql="update news set title='$title',content='$content' where id='$id'";
$this->_pdo->exec($sql);
return true;
}
//靜態化
public function obhtml($id,$title,$content)
{
ob_start();
include "show.html";
$text=ob_get_clean();
$shu_ru='./html/'.$id.'.html';
if(file_exists($shu_ru))
{
$filec_time=filectime($shu_ru);//檔案建立時間
if(time()-$filec_time<20)//沒有過期
{
echo "自動生成靜態頁面";
file_put_contents($shu_ru,$text);
}
else//過期
{
unlink($shu_ru);//刪除過期檔案
ob_start();
echo "這是過期後生成的靜態頁面";
file_put_contents($shu_ru,$text);
}
}

}
}