您的位置:首页 > 编程语言 > PHP开发

php中的数据库操作类、分页类,以及smarty扩展类

2016-12-01 10:40 267 查看
相关链接

smarty扩展类

<?php

$file=dirname(dirname(str_replace("\\","/",__FILE__)));

require $file."/smarty/Smarty.class.php";

class smartyProject extends smarty

{

function __construct()

{

parent::__construct();//基类的构造函数不能少

$this->template_dir=dirname(dirname(str_replace("\\","/",__FILE__)))."/smarty/templates";

$this->compile_dir=dirname(dirname(str_replace("\\","/",__FILE__)))."/smarty/templates_c";

$this->config_dir=dirname(dirname(str_replace("\\","/",__FILE__)))."/smarty/configs";

$this->cache_dir=dirname(dirname(str_replace("\\","/",__FILE__)))."/smarty/cache";

}

}

?>

数据库操作类、分页类

<style>

.link1

{

text-decoration:none;

}

</style>

<?php

class OperateDb

{

public $dbms='mysql';

public $host='localhost';

public $dbname='db_business';

public $user='root';

public $pwd='root';

public $pdo="";

public $sql="";

function __construct($dbms='mysql',$host='localhost',$dbname='db_business',$user='root',$pwd='root')

{

$this->dbms=$dbms;

$this->host=$host;

$this->dbname=$dbname;

$this->user=$user;

$this->pwd=$pwd;

$dsn="$dbms:host=$host;dbname=$dbname";

$this->pdo=new PDO($dsn,$user,$pwd);

$this->pdo->exec("set names utf8");

return $this->pdo;

}

function handleSql($sql)

{

$this->sql=$sql;

$subsql=substr($sql,0,5);

//插入、删除、更新

if($substr=='insert'||$substr=='update'||$substr=='delete')

{

if($this->pdo->exec($this->sql))

{

return "操作成功!";

}

}

else if($subsql='select')

{

echo $this->sql;

$result=$this->pdo->prepare($this->sql);

$result->execute();

if(!$result)

{

echo "数据查询失败!";

}

else

{

$arrarr=$result->fetchall();

return $arrarr;

}

}

}

}

class SepPage

{

public $ceilcount="";

public $pagecount="";

public $nowpage="";

public $pagesize="";

//public $offset;

function showdata($sql,$pdo,$pagesize,$page)

{

$this->pagesize=$pagesize;

if($page=="")

{

$page=1;

}

$this->nowpage=$page;

$offset=($page-1)*$pagesize;

$arrarr0=$pdo->handlesql($sql);

$arrarr=$pdo->handlesql($sql." limit $offset,$pagesize");

$this->ceilcount=count($arrarr0);

$this->pagecount=ceil($this->ceilcount/$this->pagesize);

return $arrarr;

}

function showpage($name,$unit)

{

$str="共有$name ".$this->ceilcount." $unit ";

$str.="每页显示".$this->pagesize.$unit;

$str.=" 第".$this->nowpage."页/共".$this->pagecount."页";

$str.=" <a class='link1' href='".$_SERVER['PHP_SELF']."?page=1'>首页</a>";

//上一页 要分nowpage大于1或等于1的情况

if($this->nowpage>1)

{

$str.="<a class='link1' href='".$_SERVER['PHP_SELF']."?page=".($this->nowpage-1)."'>上一页</a>";

}

else if($this->nowpage=1)

{

$str.="<a class='link1' href='".$_SERVER['PHP_SELF']."?page=1'>上一页</a>";

}

//下一页 要分nowpage小于pagecount或等于pagecount的情况

if($this->nowpage<$this->pagecount)

{

$str.="<a class='link1' href='".$_SERVER['PHP_SELF']."?page=".($this->nowpage+1)."'>下一页</a>";

}

else if($this->nowpage=$this->pagecount)

{

$str.="<a class='link1' href='".$_SERVER['PHP_SELF']."?page=".($this->pagecount)."'>下一页</a>";

}

$str.="<a class='link1' href='".$_SERVER['PHP_SELF']."?page=".($this->pagecount)."'>末页</a>";

return $str;

}

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: