+ -
当前位置:首页 → 问答吧 → 贴一个自己写的分页类PHP OOP+Smarty(MVC STRUCTURE) By Fanyishun

贴一个自己写的分页类PHP OOP+Smarty(MVC STRUCTURE) By Fanyishun

时间:2007-09-05

来源:互联网

/*C:control page.php(use smarty)
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代码:

<?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;
}
}
?>

//C:controller page.php
复制PHP内容到剪贴板
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');
?>

//V:Vision 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>
//connectSql.class.php
复制PHP内容到剪贴板
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

[ 本帖最后由 forest 于 2007-9-5 16:47 编辑 ]

作者: fanyishun   发布时间: 2007-09-05

楼主最好能  加PHP标签
这样我们看得就舒服了
呵呵

作者: 逆雪寒   发布时间: 2007-09-05

帮你编辑了一些,代码高亮显示了!

作者: forest   发布时间: 2007-09-05

I am sorry,It is my first time

作者: fanyishun   发布时间: 2007-09-05

first night?~~:lol

作者: yufenglx   发布时间: 2007-09-06

先收下

作者: redcolt   发布时间: 2007-09-12

nice work,ur english is excelent

作者: robertvvv   发布时间: 2007-10-24

鼓励一下!
:)

作者: luzhou   发布时间: 2007-10-24

:(

英语很牛哦。

作者: index   发布时间: 2007-10-26

支持一下

作者: 深海的带鱼   发布时间: 2007-10-26

分页的写法很多,支持一下。
:)

作者: luzhou   发布时间: 2007-10-27