简单分页程序,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. ?>

Mysql视图应用

作者 : admin 于 2008年08月28日, 20:55:26
2008
08-28

mysql视图创建方法,以我的OpenOA为例,创建user表left jion user_info表的视图

CREATE VIEW v_user AS SELECT * FROM user LEFT JOIN user_info ON (user.id = user_info.user_id)

然后查看

mysql>use oa

mysql>show tables

结果显示,v_user是库oa中的一个表。因此我们使用的时候,可以直接使用语句 SELECT * FROM v_user

而这句也就等于 SELECT * FROM user LEFT JOIN user_info ON (user.id = user_info.user_id)

而实习在系统中,v_user 只有这么一个文件v_user.frm  也就是只有结构,没有数据和索引.打开后,可以看到详细的存储的内容

TYPE=VIEW
query=select `oa`.`user`.`id` AS `id`,`oa`.`user`.`username` AS `username`,`oa`.`user`.`nickname` AS `nickname`,`oa`.`user`.`password` AS `password`,`oa`.`user`.`lastlogintime` AS `lastlogintime`,`oa`.`user`.`lastloginip` AS `lastloginip`,`oa`.`user`.`loginrand` AS `loginrand`,`oa`.`user_info`.`user_id` AS `user_id`,`oa`.`user_info`.`user_realname` AS `user_realname`,`oa`.`user_info`.`user_sex` AS `user_sex`,`oa`.`user_info`.`user_age` AS `user_age`,`oa`.`user_info`.`user_birthplace` AS `user_birthplace`,`oa`.`user_info`.`user_homeaddress` AS `user_homeaddress`,`oa`.`user_info`.`user_idcard` AS `user_idcard`,`oa`.`user_info`.`user_graduateschool` AS `user_graduateschool`,`oa`.`user_info`.`user_educational` AS `user_educational`,`oa`.`user_info`.`user_graduatetime` AS `user_graduatetime`,`oa`.`user_info`.`user_homephone` AS `user_homephone`,`oa`.`user_info`.`user_cellphone` AS `user_cellphone`,`oa`.`user_info`.`user_workphone` AS `user_workphone`,`oa`.`user_info`.`user_fax` AS `user_fax`,`oa`.`user_info`.`user_email` AS `user_email`,`oa`.`user_info`.`user_oicq` AS `user_oicq`,`oa`.`user_info`.`user_msn` AS `user_msn` from (`oa`.`user` left join `oa`.`user_info` on((`oa`.`user`.`id` = `oa`.`user_info`.`user_id`)))
md5=19f7a28463ad7447c3b4cc5428888fa6
updatable=0
algorithm=0
definer_user=root
definer_host=localhost
suid=1
with_check_option=0
revision=1
timestamp=2008-08-28 11:58:17
create-version=1
source=SELECT * FROM user LEFT JOIN user_info ON ( user.id = user_info.user_id )

都是啥意思,没有深究,只是能根据字面猜出来,如果只是用,到此位置即可。

CSS注释兼容性测试

作者 : admin 于 2008年08月28日, 00:21:34
2008
08-28

调试CSS差点调吐了,ff说啥都不认。后来起到html注释有兼容性,进行了一下测试。

风格1:手误的一种注释  <!– *** –>这本是html的注释,一不小心写了出来,结果,firefox不认,差点没调吐。

风格2:标准的注释 /* &&&&&   */ ie6,firefox都兼容的。

另外写注释,注释跟两边边界空出一点,会好点,降低错误率。

PHP中FTP模块的一些应用

作者 : admin 于 2008年08月27日, 10:20:06
2008
08-27
  1. <?php
  2. class ftp
  3. {
  4. //connect id
  5. var $ftp;
  6. //remote dir
  7. var $remotedir = '/';
  8. //local dir
  9. var $localdir = './';
  10. //write log
  11. var $writelog = false;
  12. //log file
  13. var $logfile = 'ftp_log.txt';
  14. //print log
  15. var $printlog = false;
  16. //session time
  17. var $timeout = 60;
  18. //construct connetc
  19. function __construct( $host , $user , $pass )
  20. {
  21. $this->ftp = @ftp_connect( $host );
  22. ftp_login( $this->ftp, $user , $pass );
  23. //ftp_set_option( $this->ftp , FTP_TIMEOUT_SEC , $this->timeout );
  24. }
  25. //list sub files
  26. function __list( $dir = false , $subtree = false )
  27. {
  28. $rlist = array();
  29. if(!$dir)
  30. {
  31. $dir = $this->remotedir;
  32. }
  33. $list = ftp_rawlist($this->ftp,$dir,TRUE);
  34. if(is_array($list))
  35. {
  36. foreach( $list as $key => $value )
  37. {
  38. $tmp = explode( ' ' , $value );
  39. $rlist[$key] = end($tmp);
  40. if(@ftp_chdir($this->ftp,$dir.$rlist[$key].'/'))
  41. {
  42. $rlist[$rlist[$key]] = $this->__list( $dir.$rlist[$key].'/' );
  43. unset($rlist[$key]);
  44. }
  45. unset($tmp);
  46. }
  47. }
  48. return $rlist;
  49. }
  50. //change dir  $isabs = false,use rela dir default
  51. function __chdir( $dir , $isabs = false )
  52. {
  53. if(!$isabs)
  54. {
  55. $nextdir = $this->remotedir.$dir.'/';
  56. }
  57. else
  58. {
  59. $nextdir = $dir;
  60. }
  61. if(ftp_chdir($this->ftp,$nextdir))
  62. {
  63. $this->remotedir = $nextdir;
  64. return true;
  65. }
  66. else
  67. {
  68. return false;
  69. }
  70. }
  71. //close connect
  72. function __close()
  73. {
  74. @ftp_close($this->ftp);
  75. }
  76. }
  77. $ftp = new ftp( 'www.sunboyu.cn' , '**', '***' );
  78. $ftp->__chdir('website',0);
  79. $list = $ftp->__list('/');
  80. echo $a = time();
  81. print_r($list);
  82. echo time() - $a;
  83. ?>

放弃ADODB_lite数据字典功能

作者 : admin 于 2008年08月26日, 23:33:31
2008
08-26

对adodb_lite的数据字典功能进行测试使用后,发现这玩意是一块鸡肋。其优秀之处在于多种数据库的无缝切换,当使用范围扩大的时候,弊端随之暴露:

1:对字段类型的支持不够完全,比如enum类型

2:不支持存储过程触发器之类

而一个项目中,存储过程触发器之类使用很多,没有这些,但出写表的数据字典感觉意义不大,不过简单的表还是会用这种方式,其他的就爹生成sql语句了

今日北京风雨大作,浇老夫于知春路上

作者 : admin 于 2008年08月26日, 09:20:13
2008
08-26

PHP调用COM获得服务器硬件信息

作者 : admin 于 2008年08月24日, 10:23:19
2008
08-24
  1. function sys_windows() {
  2. $objLocator = new COM("WbemScripting.SWbemLocator");
  3. $wmi = $objLocator->ConnectServer();
  4. $prop = $wmi->get("Win32_PnPEntity");
  5. //CPU
  6. $cpuinfo = GetWMI($wmi,"Win32_Processor", array("Name","L2CacheSize","NumberOfCores"));
  7. $res['CPU个数'] = $cpuinfo[0]['NumberOfCores'];
  8. if (null == $res['CPU个数']) {
  9. $res['CPU个数'] = 1;
  10. }
  11. for ($i=0;$i<$res['cpu']['num'];$i++){
  12. $res['CPU型号'] .= $cpuinfo[0]['Name']."<br>";
  13. $res['二级缓存'] .= $cpuinfo[0]['L2CacheSize']."<br>";
  14. }
  15. // SYSINFO
  16. $sysinfo = GetWMI($wmi,"Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize','FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate'));
  17. $res['操作系统版本'] = $sysinfo[0]['Caption']." ".$sysinfo[0]['CSDVersion'];
  18. $res['操作系统序列号'] = "{$sysinfo[0]['SerialNumber']} 于".date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装";
  19. //UPTIME
  20. $res['最后重启时间'] = $sysinfo[0]['LastBootUpTime'];
  21.  
  22.  
  23. $sys_ticks = 3600*8 + time() - strtotime(substr($res['最后重启时间'],0,14));
  24. $min = $sys_ticks / 60;
  25. $hours = $min / 60;
  26. $days = floor($hours / 24);
  27. $hours = floor($hours - ($days * 24));
  28. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  29. if ($days !== 0) $ress['day'] = $days."天";
  30. if ($hours !== 0) $ress['hours'] .= $hours."小时";
  31. $res['最后重启时间'] = $ress['day'].$ress['hours'].$min."分钟";
  32.  
  33. //MEMORY
  34. $res['物理内存'] = $sysinfo[0]['TotalVisibleMemorySize'];
  35. $res['剩余内存'] = $sysinfo[0]['FreePhysicalMemory'];
  36. $res['已使用内存'] = $res['物理内存'] - $res['剩余内存'];
  37. $res['使用率'] = round($res['已使用内存'] / $res['物理内存']*100,2);
  38.  
  39. $swapinfo = GetWMI($wmi,"Win32_PageFileUsage", array('AllocatedBaseSize','CurrentUsage'));
  40.  
  41. // TODO swp区获取
  42. $res['交换分区'] = $swapinfo[0][AllocatedBaseSize];
  43. $res['已经使用'] = $swapinfo[0][CurrentUsage];
  44. $res['剩余内存'] = $res['swapTotal'] - $res['swapUsed'];
  45. $res['使用率'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0;
  46.  
  47. // LoadPercentage
  48. $loadinfo = GetWMI($wmi,"Win32_Processor", array("LoadPercentage"));
  49. $res['系统平均负载'] = $loadinfo[0]['LoadPercentage'];
  50.  
  51. return $res;
  52. }
  53.  
  54. function GetWMI($wmi,$strClass, $strValue = array()) {
  55. $arrData = array();
  56.  
  57. $objWEBM = $wmi->Get($strClass);
  58. $arrProp = $objWEBM->Properties_;
  59. $arrWEBMCol = $objWEBM->Instances_();
  60. foreach($arrWEBMCol as $objItem) {
  61. @reset($arrProp);
  62. $arrInstance = array();
  63. foreach($arrProp as $propItem) {
  64. eval("\$value = \$objItem->" . $propItem->Name . ";");
  65. if (empty($strValue)) {
  66. $arrInstance[$propItem->Name] = trim($value);
  67. } else {
  68. if (in_array($propItem->Name, $strValue)) {
  69. $arrInstance[$propItem->Name] = trim($value);
  70. }
  71. }
  72. }
  73. $arrData[] = $arrInstance;
  74. }
  75. return $arrData;
  76. }

小一探针

作者 : admin 于 2008年08月21日, 11:26:28
2008
08-21

闲来没事,突然想写个探针
http://www.sunboyu.cn/info.php
难点很多,逐一突破。写这么个东西对PHP和系统的认识提高很快。

史上最牛的中文验证码,2过腾讯!

作者 : admin 于 2008年08月18日, 23:39:02
2008
08-18

测试地址 http://www.sunboyu.cn/sourse/nbcode.php

下载地址 http://www.sunboyu.cn/sourse/nbcode.rar

 Page 1 of 4  1  2  3  4 »