贴一个自己写的分页类PHP OOP+Smarty(MVC STRUCTURE) By Fanyishun
时间:2007-09-05
来源:互联网
To see the code below C and V,we divide the logic code and vision control in 3 files,
M:like the module only for template: How to do
V:Vision Only for Css+Div+Html with few logic control :How to show
C:controller the logic control :What to do
*/
//M:module...page.class.php
PHP代码:
<?php
require("connectsql.class.php");
class Page extends ConnectSql
{
private $pagesize=5;
private $recordstart;
private $totalpages;
private $currentpage;
private $totalrows;
private $sqlson;
public function setRecordstart($input)
{$this->recordstart=$input;
}
public function setTotalPages($input)
{$this->totalpages=$input;
}
public function setCurrentpages($input=0)
{$this->currentpage=$input;
}
public function setTotalrows($input)
{$this->totalrows=$input;
}
public function setSqlSon($input)
{$this->sqlson=$input;
}
public function getPage()
{$this->setRecordstart((isset($_GET['recordstart']))? $_GET['recordstart'] :0);
$this->setCurrentPages($this->recordstart);
$this->connect($this->sql." LIMIT $this->recordstart,$this->pagesize");
while($row=mysql_fetch_array($this->result,MYSQL_ASSOC))
$array[]=$row;
return $array;
}
public function getNum()
{$this->connect($this->sql);
$row=mysql_fetch_row($this->result);
$this->setTotalrows($row[0]);
$this->setTotalPages(ceil($this->totalrows/$this->pagesize));
}
public function showLinks()
{$str="";
for($num=0;$num<=$this->totalpages;$num++)
if($num!=$this->currentpage)
$str.=" <a href=".$_SERVER['PHP_SELF']."?recordstart=".$num.">".$num."</a>";
else
$str.=$num;
return $str;
}
}
?>
PHP代码:
<?php
require('Smarty.class.php');
require ("classes/page.class.php");
$smarty= new Smarty;
require('SmartyPath.php');
$page=new Page();
$page->setSql("SELECT count(ProjectName) as amount From general_pro");
$page->getNum();
$page->setSql("SELECT * FROM general_pro");
$pages=$page->getPage();
$pagelinks=$page->showLinks();
$smarty->assign("pages",$pages);
$smarty->assign("pagelinks",$pagelinks);
$smarty->display('page.tpl');
?>
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c />
<title>Untitled Document</title>
{literal}<style type="text/css">
<!--
.page {
font-family: "Times New Roman", Times, serif;
font-size: 14px;
text-align: center;
position: absolute;
left: 20%;
top: 20%;
right: 20%;
bottom: 20%;
}
-->
</style>{/literal}
</head>
<body>
<p class="page"> </p>
<div id="page" class="page">
<table width="100%" border="1">
<tr>
<td>NO</td>
<td>ClientName</td>
<td>ProjectName</td>
{foreach key=key item=item from=$pages}
<tr>
<td>{$key}</td>
{foreach key=key item=item from=$item}
<td>{$item}</td>
{/foreach}
</tr>
{/foreach}
</tr>
</table>
{$pagelinks}
</div>
</body>
</html>
PHP代码:
class ConnectSql{
static protected $sql;
static public $result;
public function setSql($input)
{
$this->sql=$input;
}
public function getSql()
{
echo $this->sql;
}
public function conSql($server,$name,$password)
{
try{
$con=mysql_connect($server,$name,$password)
or die("something is wrong with the database,can not connected");
if (!$con)
throw new Exception("cannot connect to the Mysql Server") ;
}catch (Exception $e)
{
echo $e."Please Check the password and the username";
}
}
public function choseDatabase($name)
{
try{
$con=mysql_select_db($name)
or die("can not select database");
if(!$con)
throw new Exception("cannot connect to the database") ;
}catch (Exception $e)
{
echo $e."Please check the name of the database";
}
}
public function result($input=null)
{
if(isset($input))
try{
$this->result=mysql_query($input);
if(!$this->result)
throw new Exception("cannot use the SQL") ;
}
catch(Exception $e)
{echo $e."<br/>".mysql_error();
}
else
try{
$this->result=mysql_query($this->sql);
if(!$this->result)
throw new Exception("cannot use the SQL") ;
}
catch(Exception $e)
{echo $e."<br/>".mysql_error();
}
}
//For safety consideration,without connectSql::connect;
//It is not the officail MVC structure support by php,only by me
//I will try more about the ajax with this Smarty
作者: fanyishun 发布时间: 2007-09-05
这样我们看得就舒服了
呵呵
作者: 逆雪寒 发布时间: 2007-09-05
作者: forest 发布时间: 2007-09-05
作者: fanyishun 发布时间: 2007-09-05
作者: yufenglx 发布时间: 2007-09-06
作者: redcolt 发布时间: 2007-09-12
作者: robertvvv 发布时间: 2007-10-24
:)
作者: luzhou 发布时间: 2007-10-24
英语很牛哦。
作者: index 发布时间: 2007-10-26
作者: 深海的带鱼 发布时间: 2007-10-26
:)
作者: luzhou 发布时间: 2007-10-27
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28