smarty分页程序,模板小改进

作者 : admin 于 2009年07月27日, 15:11:04
2009
07-27

增加了:跳转到第几页的功能

模板部分

  1. <div id="page">
  2. <table>
  3.     <tr>
  4.     <td>
  5. 共{{$page.count}}条数据 每页{{$page.pagesize}}条  共{{$page.pagecount}}页 当前第{{$page.page}}页
  6.                 <!-- 新加的跳转功能  start -->
  7.                 跳转到第
  8.                 <select onchange="window.location.href='{{$page.baseurl}}&page='+this.options[this.selectedIndex].value">
  9.                 {{section name=pagejump loop=4 start=0 step=1 max=4}}
  10.                 <option value="{{$smarty.section.pagejump.index+1}}">{{$smarty.section.pagejump.index+1}}</option>
  11.                 {{/section}}
  12.                 </select> 页
  13.                 <!-- 新加的跳转功能  start -->
  14. </td>
  15. <td>
  16. {{if $page.pagecount>1}}
  17. {{if $page.first eq 1}}
  18. <a href="{{$page.baseurl}}">首页</a>
  19. {{else}}
  20. 首页
  21. {{/if}}
  22. {{if $page.pre eq 1}}
  23. <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
  24. {{else}}
  25. 上一页
  26. {{/if}}
  27. {{foreach from=$page.pagelist item=vols}}
  28.     {{if ($vols.page > 0 ) && ($vols.page < = $page.pagecount) }}
  29. {{if $vols.link eq 1}}
  30. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]
  31. {{else}}
  32. [{{$vols.page}}]
  33. {{/if}}
  34. {{/if}}
  35. {{/foreach}}
  36. {{if $page.next eq 1}}
  37. <a href="{{$page.baseurl|default:"?"}}&page={{$page.page+1}}">下一页</a>
  38. {{else}}
  39. 下一页
  40. {{/if}}
  41. {{if $page.last eq 1}}
  42. <a href="{{$page.baseurl|default:"?"}}&page={{$page.pagecount}}">尾页</a>
  43. {{else}}
  44. 尾页
  45. {{/if}}
  46. {{/if}}
  47. </td>
  48. </tr>
  49. </table>
  50. </div>

其实我在做的时候又出现个问题,如果是url重写了,如何来做这个baseurl变量。问题解决方法是,把url当做模板,比如/blog/index/%d

Smarty分页类修正版

作者 : admin 于 2009年05月08日, 03:59:49
2009
05-8

原来写的分页类有问题,经过排查,发现一个变量用错了,修改之

效果,很简单,黑白配,有心的人可以自己修改一下风格,欢迎共享

e69caae591bde5908d-1

PHP部分

  1. < ?php
  2. /*  Page.Class.php
  3.  *  Page
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2009 05 07  sunboyu@gmail.com
  9.  *  Demo
  10. $page = new Page( 1 , 'v_user' , '*' , '' );
  11. $rs = $page->__getlist();
  12. $smarty->assign("page",$page->__getpagelist());
  13.  */
  14.  class Page
  15.  {
  16. public $count;      #结果总数
  17. public $page;       #当前页
  18. public $pagesize;   #每页结果数
  19. public $pagecount;  #翻页数
  20. public $baseurl;    #url
  21. public $result;     #结果数组集
  22. public $pagelist;   #每翻页数
  23. public $db;         #数据库连接
  24. public $table;      #要查询的表
  25. public $fileds;     #要返回的字段
  26. public $where;      #where条件
  27.  
  28. #构造函数,初始化变量
  29. function __construct( $page , $table , $fields = '*' , $where = false , $baseurl = false )
  30. {
  31.      global $_CFG,$db;
  32. $this->db        = $db;
  33. $this->table     = $table;
  34. $this->page      = isset($page) ? intval($page) : 1;
  35. $this->fileds    = $fields;
  36. $this->pagesize  = $_CFG['pagesize'];
  37. $this->baseurl   = ($baseurl!=false) ? $baseurl : $this->__geturl();
  38. $this->pagelist  = $_CFG['pagelist'];
  39. $this->where     = $where;
  40. }
  41.  
  42. #获得当前url
  43. function __geturl()
  44. {
  45. parse_str($_SERVER['QUERY_STRING'],$str);
  46. if(isset($str['page']))
  47. {
  48. unset($str['page']);
  49. }
  50. return count($str) > 0 ? "?".http_build_query($str) : "?";
  51. }
  52.  
  53. #获得记录集
  54. function __getlist()
  55. {
  56.      #获得count记录
  57. $sql = sprintf("SELECT COUNT(*) AS table_count FROM %s %s",$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''));
  58. $rs = $this->db->fetch( $sql );
  59. $this->count = $rs['table_count'];
  60.      $offset = ($this->page-1)*$this->pagesize-1;
  61.      $sql = sprintf("SELECT %s FROM %s %s LIMIT %d,%d",$this->fileds,$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''),$this->pagesize*($this->page-1),$this->pagesize);
  62. #echo $sql;
  63.          $list = $this->db->fetchAll( $sql );
  64. return $list;
  65. }
  66.  
  67. #获得分页列表
  68. function __getpagelist()
  69. {
  70. $this->result['count'] = $this->count;
  71. $this->result['page'] = $this->page;
  72. $this->result['pagesize'] = $this->pagesize;
  73. $this->result['pagecount'] = ceil($this->count/$this->pagesize);
  74. if($this->result['pagecount']< =1) //只有一页以下
  75. {
  76. $this->result['pagelist'] = 0;
  77. }
  78. else //一页以上
  79. {
  80. #前一页,第一页的算法
  81. $this->result['first'] = ($this->page == 1) ? 0 : 1;
  82. $this->result['pre'] = ($this->page == 1) ? 0 : 1;
  83. #后一页,最后一页的算法
  84. $this->result['next'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
  85. $this->result['last'] = ($this->page == $this->result['pagecount'] ) ? 0 : 1;
  86.  
  87. #起始
  88. $pagearray = array();
  89. $start = floor(($this->page-1)/10)*10+1;
  90. for($i=0;$i&lt;10;$i++)
  91. {
  92.      if( ($start+$i) < = $this->result['pagecount'])
  93. {
  94.      $pagearray[$i]['page'] = $start+$i;
  95. }
  96. if( ($start+$i) != $this->page )
  97. {
  98.      $pagearray[$i]['link'] = 1;
  99. }
  100. }
  101. #分页导航列表
  102. $this->result['pagelist'] = $pagearray;
  103. $this->result['baseurl'] = $this->baseurl;
  104. }
  105. return $this->result;
  106. }
  107. }
  108. ?>

模板部分

  1. <div id="page">
  2. <table>
  3.     <tr>
  4.     <td>
  5. 共{{$page.count}}条数据 每页{{$page.pagesize}}条  共{{$page.pagecount}}页 当前第{{$page.page}}页
  6. </td>
  7. <td>
  8. {{if $page.pagecount>1}}
  9. {{if $page.first eq 1}}
  10. <a href="{{$page.baseurl}}">首页</a>
  11. {{else}}
  12. 首页
  13. {{/if}}
  14. {{if $page.pre eq 1}}
  15. <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
  16. {{else}}
  17. 上一页
  18. {{/if}}
  19. {{foreach from=$page.pagelist item=vols}}
  20.     {{if ($vols.page > 0 ) && ($vols.page < = $page.pagecount) }}
  21. {{if $vols.link eq 1}}
  22. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]
  23. {{else}}
  24. [{{$vols.page}}]
  25. {{/if}}
  26. {{/if}}
  27. {{/foreach}}
  28. {{if $page.next eq 1}}
  29. <a href="{{$page.baseurl|default:"?"}}&page={{$page.page+1}}">下一页</a>
  30. {{else}}
  31. 下一页
  32. {{/if}}
  33. {{if $page.last eq 1}}
  34. <a href="{{$page.baseurl|default:"?"}}&page={{$page.pagecount}}">尾页</a>
  35. {{else}}
  36. 尾页
  37. {{/if}}
  38. {{/if}}
  39. </td>
  40. </tr>
  41. </table>
  42. </div>

补充一个demo

  1. #PHP部分
  2. $page = new Page( $page , $this->area_table , $fields = '*' , $where);
  3. $result['rs'] = $page->__getlist();
  4. $result['page'] = $page->__getpagelist();
  5. $smarty->assign("list",$result['rs']);
  6. $smarty->assign("page",$result['page']);
  7. $smarty->display('list.tpl');
  8. #模板里边只要引用这个分页模板即可
  9. {{include file=$smarty.const.Tpl|cat:"/Page.tpl"}}

Smarty被PHP抛弃,还是Smarty要走自己的路?

作者 : admin 于 2008年09月04日, 11:12:40
2008
09-4

打开     http://smarty.php.net/

发现只剩下了这样的字

Smarty has moved

Smarty is no longer a subproject of the PHP project, and has subsequently moved to its own domain: www.smarty.net

简单分页程序,adodb+smarty

作者 : admin 于 2008年08月31日, 17:56:16
2008
08-31

分页类

  1. <?php
  2. /*  Page.Class.php
  3.  *  Page
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  *  Demo
  10. $page = new Page( 1 , 'v_user' , '*' , '' );
  11. $rs = $page->__getlist();
  12. $smarty->assign("page",$page->__getpagelist());
  13.  */
  14.  class Page
  15.  {
  16. public $count;      #结果总数
  17. public $page;       #当前页
  18. public $pagesize;   #每页结果数
  19. public $pagecount;  #翻页数
  20. public $baseurl;    #url
  21. public $result;     #结果数组集
  22. public $pagelist;   #每翻页数
  23. public $db;         #数据库连接
  24. public $table;      #要查询的表
  25. public $fileds;     #要返回的字段
  26. public $where;      #where条件
  27.  
  28. #构造函数,初始化变量
  29. function __construct( $page , $table , $fields = '*' , $where = false , $baseurl = false )
  30. {
  31.      global $_CFG;
  32. $this->db        = GetDB();
  33. $this->count     = $count;
  34. $this->table     = $table;
  35. $this->page      = isset($page) ? intval($_GET['page']) : 1;
  36. $this->fileds    = $fields;
  37. $this->pagesize  = $_CFG['pagesize'];
  38. $this->baseurl   = ($baseurl!=false) ? $baseurl : $this->__geturl();
  39. $this->pagelist  = $_CFG['pagelist'];
  40. $this->where     = $where;
  41. }
  42.  
  43. #获得当前url
  44. function __geturl()
  45. {
  46. $str = ereg_replace(sprintf("(^|&)page=%d",$this->page),"",$_SERVER['QUERY_STRING']);
  47. return $str=="" ? '?' : $str;
  48. }
  49. #获得记录集
  50. function __getlist()
  51. {
  52.      #获得count记录
  53. $sql = sprintf("SELECT COUNT(0) AS table_count FROM %s",$this->table);
  54. $rs = $this->db->Execute( $sql );
  55. $this->count = $rs->fields['table_count'];
  56.      $offset = ($this->page-1)*$this->pagesize-1;
  57.      $sql = sprintf("SELECT %s FROM %s %s",$this->fileds,$this->table,(($this->where!=false) ? " WHERE ".$this->where : ''));
  58. $rs = $this->db->SelectLimit( $sql , $this->pagesize , $offset );
  59. $list = false;
  60. while(!$rs->EOF)
  61. {
  62.      $list[] = $rs->fields;
  63. $rs->MoveNext();
  64. }
  65. return $list;
  66. }
  67.  
  68. #获得分页列表
  69. function __getpagelist()
  70. {
  71. $this->result['count'] = $this->count;
  72. $this->result['page'] = $this->page;
  73. $this->result['pagesize'] = $this->pagesize;
  74. $this->result['pagecount'] = ceil($this->count/$this->pagesize);
  75. if($this->result['pagecount']<=1) //只有一页以下
  76. {
  77. $this->result['pagelist'] = 0;
  78. }
  79. else //一页以上
  80. {
  81. #前一页,第一页的算法
  82. $this->result['first'] = ($this->page == 1) ? 0 : 1;
  83. $this->result['pre'] = ($this->page == 1) ? 0 : 1;
  84. #后一页,最后一页的算法
  85. $this->result['next'] = ($this->page == $this->pagecount ) ? 0 : 1;
  86. $this->result['last'] = ($this->page == $this->pagecount ) ? 0 : 1;
  87.  
  88.  
  89. #起始
  90. $pagearray = array();
  91. $start = floor(($this->page-1)/10)*10+1;
  92. for($i=0;$i<10;$i++)
  93. {
  94.      if( ($start+$i) <= $this->result['pagecount'])
  95. {
  96.      $pagearray[$i]['page'] = $start+$i;
  97. }
  98. if( ($start+$i) != $this->page )
  99. {
  100.      $pagearray[$i]['link'] = 1;
  101. }
  102. }
  103. #分页导航列表
  104. $this->result['pagelist'] = $pagearray;
  105. $this->result['baseurl'] = $this->baseurl;
  106. }
  107. return $this->result;
  108. }
  109. }
  110. ?>

smarty模版

  1. {{config_load file="Lang.Page.$lang.conf"}}<div id="page">
  2. <table>
  3.     <tr>
  4.     <td>
  5. {{#gongs#}}{{$page.count}}{{#pagecountend#}} {{#pagepagesizeper#}}{{$page.pagesize}}{{#pagepagelistper#}}{{#tiao#}}  {{#gongs#}}{{$page.pagecount}}{{#page#}} {{#pagepage#}}{{$page.page+1}}{{#page#}}
  6. </td>
  7. <td>
  8. {{if $page.pagecount>1}}
  9. {{if $page.first eq 1}}
  10. <a href="{{$page.baseurl}}">{{#pagefirst#}}</a>
  11. {{else}}
  12. {{#pagefirst#}}
  13. {{/if}}
  14. {{if $page.pre eq 1}}
  15. <a href="{{$page.baseurl}}&page={{$page.page-1}}">{{#pagepre#}}</a>
  16. {{else}}
  17. {{#pagepre#}}
  18. {{/if}}
  19. {{foreach from=$page.pagelist item=vols}}
  20.     {{if $vols.link eq 1}}
  21. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]</a>
  22. {{else}}
  23. [{{$vols.page}}]
  24. {{/if}}
  25. {{/foreach}}
  26. {{if $page.next eq 1}}
  27. <a href="{{$page.baseurl|default:"?"}}&page={{$page.page+1}}">{{#pagenext#}}</a>
  28. {{else}}
  29. {{#pagenext#}}
  30. {{/if}}
  31. {{if $page.last eq 1}}
  32. <a href="{{$page.baseurl|default:"?"}}&page={{$page.pagecount}}">{{#pageend#}}</a>
  33. {{else}}
  34. {{#pagelast#}}
  35. {{/if}}
  36. {{/if}}
  37. </td>
  38. </tr>
  39. </table>
  40. </div>

语言包内容

  1. pagecountend      = "条数据"
  2. pagepagesizeper   = "每页"
  3. pagepagesizeend   = "条"
  4. page              = "页"
  5. tiao              = "条"
  6. pagepage          = "当前第"
  7. pagefirst         = "首页"
  8. pagepre           = "上一页"
  9. pagenext          = "下一页"
  10. pagelast          = "尾页"
  11. gongs             = "共"

简单的PHP+SMARTY分页类

作者 : admin 于 2008年08月29日, 23:21:47
2008
08-29

类的代码

  1. <?php
  2. /*  Page.Class.php
  3.  *  Page
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 28  sunboyu@gmail.com
  9.  */
  10.  class Page
  11.  {
  12. public $count;      #结果总数
  13. public $page;       #当前页
  14. public $pagesize;   #每页结果数
  15. public $pagecount;  #翻页数
  16. public $baseurl;    #url
  17. public $result;     #结果数组集
  18. public $pagelist;   #每翻页数
  19.  
  20. #构造函数,初始化变量
  21. function __construct( $count , $page , $pagesize , $pagelist , $baseurl = false )
  22. {
  23. $this->count     = $count;
  24. $this->page      = $page;
  25. $this->pagesize  = $pagesize;
  26. $this->baseurl   = isset($baseurl) ? $baseurl : $this->__geturl();
  27. $this->pagelist = $pagelist;
  28. }
  29.  
  30. #获得当前url
  31. function __geturl()
  32. {
  33. return ereg_replace("(^|&)page={$page}","",$_SERVER['QUERY_STRING']);
  34. }
  35.  
  36. #获得分页列表
  37. function __getpagelist()
  38. {
  39. $this->result['count'] = $this->count;
  40. $this->result['page'] = $this->page;
  41. $this->result['pagesize'] = $this->pagesize;
  42. $this->result['pagecount'] = ceil($this->count/$this->pagesize);
  43. if($this->result['pagecount']<=1) //只有一页以下
  44. {
  45. $this->result['pagelist'] = 0;
  46. }
  47. else //一页以上
  48. {
  49. #前一页,第一页的算法
  50. $this->result['first'] = ($this->page == 1) ? 0 : 1;
  51. $this->result['pre'] = ($this->page == 1) ? 0 : 1;
  52. #后一页,最后一页的算法
  53. $this->result['next'] = ($this->page == $this->pagecount ) ? 0 : 1;
  54. $this->result['last'] = ($this->page == $this->pagecount ) ? 0 : 1;
  55.  
  56.  
  57. #起始
  58. $pagearray = array();
  59. $start = floor(($this->page-1)/10)*10+1;
  60. for($i=0;$i<10;$i++)
  61. {
  62.      if( ($start+$i) <= $this->result['pagecount'])
  63. {
  64.      $pagearray[$i]['page'] = $start+$i;
  65. }
  66. if( ($start+$i) != $this->page )
  67. {
  68.      $pagearray[$i]['link'] = 1;
  69. }
  70. }
  71. #分页导航列表
  72. $this->result['pagelist'] = $pagearray;
  73. $this->result['baseurl'] = $this->baseurl;
  74. }
  75. }
  76. }
  77. ?>

模版代码

  1. <table>
  2.     <tr>
  3.     <td>
  4. 共{{$page.count}}条数据 每页{{$page.pagesize}}条 共{{$page.pagecount}}页 当前第{{$page.page}}页
  5. </td>
  6. <td>
  7. {{if $page.first eq 1}}
  8. <a href="{{$page.baseurl}}">首页</a>
  9. {{else}}
  10. 首页
  11. {{/if}}
  12. {{if $page.pre eq 1}}
  13. <a href="{{$page.baseurl}}&page={{$page.page-1}}">上一页</a>
  14. {{else}}
  15. 上一页
  16. {{/if}}
  17. {{foreach from=$page.pagelist item=vols}}
  18.     {{if $vols.link eq 1}}
  19. <a href="{{$page.baseurl}}&page={{$vols.page}}">[{{$vols.page}}]</a>
  20. {{else}}
  21. [{{$vols.page}}]
  22. {{/if}}
  23. {{/foreach}}
  24. {{if $page.next eq 1}}
  25. <a href="{{$page.baseurl}}&page={{$page.page-1}}">下一页</a>
  26. {{else}}
  27. 下一页
  28. {{/if}}
  29. {{if $page.last eq 1}}
  30. <a href="{{$page.baseurl}}&page={{$page.pagecount}}">尾页</a>
  31. {{else}}
  32. 尾页
  33. {{/if}}
  34. </td>
  35. </tr>
  36. </table>

调用范例

  1. <?php
  2. require_once('Include/Init.inc.php');
  3. require_once('Include/Class/Page.Class.php');
  4. $page = new Page( 999 , 12 , 10 , 6 , '?' );
  5. $page->__getpagelist();
  6. $smarty->assign("page",$page->result);
  7. $smarty->display("page.html");
  8. ?>

多语言风格网站的语言模块处理

作者 : admin 于 2008年07月05日, 22:57:27
2008
07-5

奥运来了,流行国际化,网站也趋向于国际化。多语言网站成了流行,这里,我总结了一下多语言网站的设计。

首先,要设计多个语言包,打个比方,一个cn的,代表汉语,一个en的,代表英语。然后建立一个映射关系。比如,helloword,你好世界,英文,中文,我们给这个短句定一个ID,ID=’HW’,在英文语言包里,HW=’helloword’,在汉语语言包里,HW=’你好世界’。以此类推,语言包就做好了。

然后,我们要确定当前页面是调用哪个语言包。可以由读者去确定,比如,默认是英文,然后用户可以手工设置为汉语。可以通过session来设定一个变量,进行控制,但大多数是通过cookie。也可以根据域名,比如 http://cn.sunboyu.cn ,解析主机名,cn 即为汉语。  得到了客户端语言的标志,我们就可以确定调用哪个语言包。

最后说一下实现机制。最简单的方式,就是定义成数据,比如 $Lang['cn']['HW']=’你好世界’  $Lang['en']['HW'] ,这样,通过数据二级的键值就可以取出值。Smarty支持一个功能,就是config_load的功能,只要模板里有个语言种类变量,就可以动态调用语言包。还有,就是php的gettext扩展,都是一样的道理,只是存储不同。

做好你的语言包,做好语言选择控制,多语言网站就可以出炉了。

ADODB Lite-adodb轻量应用

作者 : admin 于 2008年07月02日, 23:25:11
2008
07-2

原来用ADODB进行开发,效率一直是个不小的问题。虽然他可以兼容多个数据库,但在实际开发中几乎没什么用处。小型项目不会去频繁切换数据库,中大的项目也不会忍受它的速度。

突然发现还有个mini版本的adodb lite,官方这样说的:

A small, fast replacement for ADODB that uses 1/6th of the memory and upto 300% faster while being compatible with ADODB using a subset of the most often used ADODB commands.. Supports Frontbase, MaxDB, MiniSql, MSSQL, MySql, Postgres, SqLite and Sybase.

最近做个小程序,正好用上试试,希望不让我失望。