目录结构
这里是写一个博客,开发时要注意这里没写自动加载类,在项目入口文件,只是写了一个函数用于自动加载,
通过入口文件的自动加载分发控制,请求到具体方法在完成相应功能。
要点数据库类--
<?php
class Model {
private $pdo;
public function __construct()
{
include_once 'Db.class.php';
$db = Db::getIns();
$this->pdo = $db->pdo;
}
public function find($sql, $params=array()){
try{
$stmt = $this->pdo->prepare($sql);
if($params){
$this->bindValue($params, $stmt);
}
if($stmt->execute()){
return $stmt->fetch(PDO::FETCH_ASSOC);
}
return false;
}catch (PDOException $e){
echo '错误描述:' . $e->getMessage() . '<br>';
echo '出错位置:' . $e->getFile() . '中的第' . $e->getLine() . '行';
exit();
}
}
绑定参数
private function bindValue($params, $stmt){
foreach($params as $key=>$value){
if(is_int($key)){
$stmt->bindValue($key+1, $value);
}else{
//可以使用 desc student;
$stmt->bindValue(":".$key, $value); //$key = n,s,
}
}
}
}
用的pdo数据库抽象层操作数据库,这里写一个基本上增查删改基本上就出来了;改一下返回数据。
底层类里的一些要点
$link = '';
//默认的规律是当前页显示在中间
$s = $this->p - floor($this->link / 2);
$e = $s + $this->link - 1;
//针对前两页,重置s和e的值
if($this->p <= floor($this->link / 2)){
$s = 1;
$e = $this->link;
}
//针对后两页,重置s和e
if($maxPage < $this->p + floor($this->link / 2)){
$s = $maxPage - $this->link + 1;
$e = $maxPage;
}
//如果总页数 不够 $this->link个
if($maxPage < $this->link){
$s = 1;
$e = $maxPage;
}
for($i=$s; $i<=$e; $i++){
if($i == $this->p){
$link .= "<a href='#' class='current'>$i</a>";
}else{
$link .= "<a href='{$str}p=$i'>$i</a>";
分页类中取出以选定页数变化居中得分页字符串。