php类收集

<? php
//  +----------------------------------------------------------------------+
// | PHP Version 4                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                     |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,     |
// | that is bundled with this package in the file LICENSE, and is     |
// | available at through the world-wide-web at                   |
// | [url]http://www.php.net/license/2_02.txt.[/url]                       |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to       |
// | [email]license@php.net[/email] so we can mail you a copy immediately.           |
// +----------------------------------------------------------------------+
// | Authors: Richard Heyes <[email]richard@phpguru.org[/email]>                 |
// +----------------------------------------------------------------------+


/* *
* Pager class
*
* Handles paging a set of data. For usage see the example.php provided.
*
*/

class  Pager {

  
/* *
  * Current page
  * @var integer
  
*/
  
var   $_currentPage ;

  
/* *
  * Items per page
  * @var integer
  
*/
  
var   $_perPage ;

  
/* *
  * Total number of pages
  * @var integer
  
*/
  
var   $_totalPages ;

  
/* *
  * Item data. Numerically indexed array...
  * @var array
  
*/
  
var   $_itemData ;

  
/* *
  * Total number of items in the data
  * @var integer
  
*/
  
var   $_totalItems ;

  
/* *
  * Page data generated by this class
  * @var array
  
*/
  
var   $_pageData ;

  
/* *
  * Constructor
  *
  * Sets up the object and calculates the total number of items.
  *
  * @param $params An associative array of parameters This can contain:
  *             currentPage   Current Page number (optional)
  *             perPage     Items per page (optional)
  *             itemData     Data to page
  
*/
  
function  pager( $params   =   array ())
  {
    
global   $HTTP_GET_VARS ;

    
$this -> _currentPage  =   max ((int)@ $HTTP_GET_VARS [ ' pageID ' ] ,   1 );
    
$this -> _perPage    =   8 ;
    
$this -> _itemData    =   array ();

    
foreach  ( $params   as   $name   =>   $value ) {
        
$this -> { ' _ '   .   $name =   $value ;
    }

    
$this -> _totalItems  =   count ( $this -> _itemData);
  }

  
/* *
  * Returns an array of current pages data
  *
  * @param $pageID Desired page ID (optional)
  * @return array Page data
  
*/
  
function  getPageData( $pageID   =   null )
  {
    
if  ( isset ( $pageID )) {
        
if  ( ! empty ( $this -> _pageData[ $pageID ])) {
          
return   $this -> _pageData[ $pageID ];
        } 
else  {
          
return   FALSE ;
        }
    }

    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
return   $this -> getPageData( $this -> _currentPage);
  }

  
/* *
  * Returns pageID for given offset
  *
  * @param $index Offset to get pageID for
  * @return int PageID for given offset
  
*/
  
function  getPageIdByOffset( $index )
  {
    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
if  (( $index   %   $this -> _perPage)  >   0 ) {
        
$pageID   =   ceil (( float ) $index   /  ( float ) $this -> _perPage);
    } 
else  {
        
$pageID   =   $index   /   $this -> _perPage;
    }

    
return   $pageID ;
  }

  
/* *
  * Returns offsets for given pageID. Eg, if you
  * pass it pageID one and your perPage limit is 10
  * it will return you 1 and 10. PageID of 2 would
  * give you 11 and 20.
  *
  * @params pageID PageID to get offsets for
  * @return array First and last offsets
  
*/
  
function  getOffsetByPageId( $pageid   =   null )
  {
    
$pageid   =   isset ( $pageid ?   $pageid   :   $this -> _currentPage;
    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
if  ( isset ( $this -> _pageData[ $pageid ])) {
        
return   array (( $this -> _perPage  *  ( $pageid   -   1 ))  +   1 ,   min ( $this -> _totalItems ,   $this -> _perPage  *   $pageid ));
    } 
else  {
        
return   array ( 0 , 0 );
    }
  }

  
/* *
  * Returns back/next and page links
  *
  * @param $back_html HTML to put inside the back link
  * @param $next_html HTML to put inside the next link
  * @return array Back/pages/next links
  
*/
  
function  getLinks( $back_html   =   ' << Back ' ,   $next_html   =   ' Next >> ' )
  {
    
$url     =   $this -> _getLinksUrl();
    
$back   =   $this -> _getBackLink( $url ,   $back_html );
    
$pages   =   $this -> _getPageLinks( $url );
    
$next   =   $this -> _getNextLink( $url ,   $next_html );

    
return   array ( $back ,   $pages ,   $next ,   ' back '   =>   $back ,   ' pages '   =>   $pages ,   ' next '   =>   $next );
  }

  
/* *
  * Returns number of pages
  *
  * @return int Number of pages
  
*/
  
function  numPages()
  {
    
return   $this -> _totalPages;
  }

  
/* *
  * Returns whether current page is first page
  *
  * @return bool First page or not
  
*/
  
function  isFirstPage()
  {
    
return  ( $this -> _currentPage  ==   1 );
  }

  
/* *
  * Returns whether current page is last page
  *
  * @return bool Last page or not
  
*/
  
function  isLastPage()
  {
    
return  ( $this -> _currentPage  ==   $this -> _totalPages);
  }

  
/* *
  * Returns whether last page is complete
  *
  * @return bool Last age complete or not
  
*/
  
function  isLastPageComplete()
  {
    
return   ! ( $this -> _totalItems  %   $this -> _perPage);
  }

  
/* *
  * Calculates all page data
  
*/
  
function  _generatePageData()
  {
    
$this -> _totalItems  =   count ( $this -> _itemData);
    
$this -> _totalPages  =   ceil (( float ) $this -> _totalItems  /  ( float ) $this -> _perPage);
    
$i   =   1 ;
    
if  ( ! empty ( $this -> _itemData)) {
        
foreach  ( $this -> _itemData  as   $value ) {
          
$this -> _pageData[ $i ][]  =   $value ;
          
if  ( count ( $this -> _pageData[ $i ])  >=   $this -> _perPage) {
            
$i ++ ;
          }
        }
    } 
else  {
        
$this -> _pageData  =   array ();
    }
  }

  
/* *
  * Returns the correct link for the back/pages/next links
  *
  * @return string Url
  
*/
  
function  _getLinksUrl()
  {
    
global   $HTTP_SERVER_VARS ;

    
//  Sort out query string to prevent messy urls
     $querystring   =   array ();
    
if  ( ! empty ( $HTTP_SERVER_VARS [ ' QUERY_STRING ' ])) {
        
$qs   =   explode ( ' & ' ,   $HTTP_SERVER_VARS [ ' QUERY_STRING ' ]);         
        
for  ( $i   =   0 ,   $cnt   =   count ( $qs );  $i   <   $cnt $i ++ ) {
          
list ( $name ,   $value =   explode ( ' = ' ,   $qs [ $i ]);
          
if  ( $name   !=   ' pageID ' ) {
            
$qs [ $name =   $value ;
          }
          
unset ( $qs [ $i ]);
        }
    }
  
if ( is_array ( $qs )){
        
foreach  ( $qs   as   $name   =>   $value ) {
          
$querystring []  =   $name   .   ' = '   .   $value ;
        }   
  }
    
return   $HTTP_SERVER_VARS [ ' SCRIPT_NAME ' .   ' ? '   .   implode ( ' & ' ,   $querystring .  ( ! empty ( $querystring ?   ' & '   :   '' .   ' pageID= ' ;
  }

  
/* *
  * Returns back link
  *
  * @param $url URL to use in the link
  * @param 上一篇     目录     下一篇 HTML to use as the link
  * @return string The link
  
*/
  
function  _getBackLink( $url ,  上一篇     目录     下一篇  =   ' << Back ' )
  {
    
//  Back link
     if  ( $this -> _currentPage  >   1 ) {
        
$back   =   ' <a href=" '   .   $url   .  ( $this -> _currentPage  -   1 .   ' "> '   .  上一篇     目录     下一篇  .   ' </a> ' ;
    } 
else  {
        
$back   =   '' ;
    }
    
    
return   $back ;
  }

  
/* *
  * Returns pages link
  *
  * @param $url URL to use in the link
  * @return string Links
  
*/
  
function  _getPageLinks( $url )
  {
    
//  Create the range
     $params [ ' itemData ' =   range ( 1 ,   max ( 1 ,   $this -> _totalPages));
    
$pager   =&   new  Pager( $params );
    上一篇     目录     下一篇s 
=   $pager -> getPageData( $pager -> getPageIdByOffset( $this -> _currentPage));

    
for  ( $i = 0 $i < count (上一篇     目录     下一篇s);  $i ++ ) {
        
if  (上一篇     目录     下一篇s[ $i !=   $this -> _currentPage) {
          上一篇     目录     下一篇s[
$i =   ' <a href=" '   .   $this -> _getLinksUrl()  .  上一篇     目录     下一篇s[ $i .   ' "> '   .  上一篇     目录     下一篇s[ $i .   ' </a> ' ;
        }
    }

    
return   implode ( '   ' ,  上一篇     目录     下一篇s);
  }

  
/* *
  * Returns next link
  *
  * @param $url URL to use in the link
  * @param 上一篇     目录     下一篇 HTML to use as the link
  * @return string The link
  
*/
  
function  _getNextLink( $url ,  上一篇     目录     下一篇  =   ' Next >> ' )
  {
    
if  ( $this -> _currentPage  <   $this -> _totalPages) {
        
$next   =   ' <a href=" '   .   $url   .  ( $this -> _currentPage  +   1 .   ' "> '   .  上一篇     目录     下一篇  .   ' </a> ' ;
    } 
else  {
        
$next   =   '' ;
    }

    
return   $next ;
  }
}

?>
<? php
//
// +----------------------------------------------------------------------+
// | 分页类                                           |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001 NetFish Software                       |
// |                                               |
// | Author: whxbb([email]whxbbh@21cn.com[/email])                           |
// +----------------------------------------------------------------------+
//
// $Id: pager.class.php,v 0.1 2001/8/2 13:18:13 yf Exp $
//
// 禁止直接访问该页面

if  ( basename ( $HTTP_SERVER_VARS [ ' PHP_SELF ' ])  ==   " pager.class.php " ) {
  
header ( " HTTP/1.0 404 Not Found " );
}
/* *
* 分页类
* Purpose
* 分页
*
* @author : whxbb([email]whxbb@21cn.com[/email])
* @version : 0.1
* @date   : 2001/8/2
*/
class  Pager
{
  
/* * 总信息数  */
  
var   $infoCount ;
  
/* * 总页数  */
  
var   $pageCount ;
  
/* * 每页显示条数  */
  
var   $items ;
  
/* * 当前页码  */
  
var   $pageNo ;
  
/* * 查询的起始位置  */
  
var   $startPos ;
  
var   $nextPageNo ;
  
var   $prevPageNo ;
  
  
function  Pager( $infoCount ,   $items ,   $pageNo )
  {
    
$this -> infoCount  =   $infoCount ;
    
$this -> items    =   $items ;
    
$this -> pageNo    =   $pageNo ;
    
$this -> pageCount  =   $this -> GetPageCount();
    
$this -> AdjustPageNo();
    
$this -> startPos  =   $this -> GetStartPos();
  }
  
function  AdjustPageNo()
  {
    
if ( $this -> pageNo  ==   ''   ||   $this -> pageNo  <   1 )
        
$this -> pageNo  =   1 ;
    
if  ( $this -> pageNo  >   $this -> pageCount)
        
$this -> pageNo  =   $this -> pageCount;
  }
  
/* *
  * 下一页
  
*/
  
function  GoToNextPage()
  {
    
$nextPageNo   =   $this -> pageNo  +   1 ;
    
if  ( $nextPageNo   >   $this -> pageCount)
    {
        
$this -> nextPageNo  =   $this -> pageCount;
        
return   false ;
    }
    
$this -> nextPageNo  =   $nextPageNo ;
    
return   true ;
  }
  
/* *
  * 上一页
  
*/
  
function  GotoPrevPage()
  {
    
$prevPageNo   =   $this -> pageNo  -   1 ;
    
if  ( $prevPageNo   <   1 )
    {
        
$this -> prevPageNo  =   1 ;
        
return   false ;
    }
    
$this -> prevPageNo  =   $prevPageNo ;
    
return   true ;
  }
  
function  GetPageCount()
  {
    
return   ceil ( $this -> infoCount  /   $this -> items);
  }
  
function  GetStartPos()
  {
    
return  ( $this -> pageNo  -   1 *   $this -> items;
  }
}
?>

分页类

------------------

目录操作基类

<?
// 目录操作基类
class  FileDirectory {
var   $servermode ;
var   $serverpath ;    // web服务器目录
var   $pagepath ;    // 当前页目录
var   $path ;      // 当前目录
var   $ffblk ;      // 用于存储有关文件的信息
function  FileDirectory() {
  
set_time_limit ( 0 );    // 设置网页运行时间,0不限
   $this -> serverpath  =   $GLOBALS [DOCUMENT_ROOT] . " / " ;
  
$this -> path  =   $this -> pagepath  =   dirname ( eregi_replace ( " // " , " / " , $GLOBALS [SCRIPT_FILENAME])) . " / " ;
  
if ( eregi ( " Win32 " , getenv ( " SERVER_SOFTWARE " )))
    
$this -> servermode  =   " WIN32 " ;
}
function  first_dir() {
  
return   dirname ( eregi_replace ( " // " , " / " , $GLOBALS [SCRIPT_FILENAME]));
}
// 获取文件信息
function  file_info( $filename ) {
  
$ar [name]  =   $filename ;
  
$ar [type]  =   filetype ( $filename );
  
$ar [read]  =   is_readable ( $filename );
  
$ar [write]  =   is_writeable ( $filename );
  
$ar [ exec =   is_executable ( $filename );
  
$ar [ time =   date ( " Y-m-d H:i:s " , filemtime ( $filename ));
  
$ar [size]  =   filesize ( $filename );
  
$ar [style]  =  ( $ar [type] == " dir " ? " d " : " - " )
        
. ( $ar [read] ? " r " : " - " )
        
. ( $ar [write] ? " w " : " - " )
        
. ( $ar [ exec ] ? " x " : " - " );
  
return   $ar ;
}

function  format_path( $path ){
  
$tar   =   split ( " / " , $path );
  
$sar   =   split ( " / " , $this -> path);
  
$t   =   count ( $tar );
  
$s   =   count ( $sar );
  
if ( $tar [ $t - 1 ==   "" $t -- ;
  
if ( $sar [ $s - 1 ==   "" $s -- ;
  
$j   =   0 ;
  
while ( $tar [ $j ==   " .. " ) {
    
$j ++ ;
    
$s -- ;
  }
  
$p   =   "" ;
  
for ( $i = 0 ; $i < $s ; $i ++ )
    
$p   .=   $sar [ $i ] . " / " ;
  
for ( $i = $j ; $i < $t ; $i ++ )
    
if ( $tar [ $i !=   " . " )
    
$p   .=   $tar [ $i ] . " / " ;
  
$this -> path  =   $p ;
}
// 获取目录信息到数组,成功返回时$this->path为目录的全路径
function  array_dir( $pathname = " . " ) {
  
$old   =   $this -> path;
  
if ( $this -> servermode  ==   " WIN32 " )
    
$path   =   str_replace ( " / " , " / " , $pathname );
  
else
    
$path   =   $pathname ;
  
$this -> format_path( $path );
  
if ( !  ( $handle   =  @ opendir ( $path ))) {
    
$path   =   dirname ( $pathname );
    
$handle   =   opendir ( $path );
  }
  
if (@ chdir ( $this -> path)) {
    
while  ( $file   =   readdir ( $handle )) {
    
$ar []  =   $this -> file_info( $file );
    }
  }
else
    
$this -> path  =   $old ;
  
closedir ( $handle );
  
return   $ar ;
}
}   
// FileDirectory定义结束

?>

<?
// 目录对话框
class  OpenFileDialog  extends  FileDirectory {
var   $filter   =   array ( " *.* " );
function  Execute( $path , $statpath ) {
  
if ( $path   !=   "" ) {
    
chdir ( $statpath );
    
$this -> path  =   $statpath ;
    
$ar   =   $this -> array_dir( $path );
  }
else
    
$ar   =   $this -> array_dir( " . " );
  
array_multisort ( $ar );
echo   "
<style>
td{font-size:9pt;}
select{font-size:9pt;}
#box{border:3px outset #ffffff}
</style>
<form action=
" ;
echo   $GLOBALS [PHP_SELF];
echo   "  method=POST>
<table bgcolor=#cccccc cellspacing=0 cellpadding=0>
<tr><td>
<table border=0 id=box>
<tr><td>
" ;
echo   " 当前路径  " . $this -> path . " <br> " ;
echo   " <input type=hidden name=statpath value=" " . $this -> path . " "> " ;

echo   " <select name=dirlist size=6 style="width:100px" onChange="this.form.submit()"> " ;
for ( $i = 0 ; $i < count ( $ar ); $i ++ )
if ( $ar [ $i ][type]  ==   " dir " )
  
if ( $ar [ $i ][name]  ==   " . " )
    
echo   " <option selected> " . $ar [ $i ][name] . " " ;
  
else
    
echo   " <option> " . $ar [ $i ][name] . " " ;
echo   " </select>  " ;
echo   " <select size=6 style="width:100px"> " ;
for ( $i = 0 ; $i < count ( $ar ); $i ++ )
if ( $ar [ $i ][type]  ==   " file " )
  
echo   " <option> " . $ar [ $i ][name] . " " ;
echo   "
</select>
</td></tr>
</table>
</td></tr>
</table>
</form>
" ;
}
}   
// OpenFileDialog
?>

<?
// 测试

$dir   =   new  OpenFileDialog();
echo   " 服务器类型  " . $dir -> servermode . " <br> " ;
echo   " 服务器路径  " . $dir -> serverpath . " <br> " ;
echo   " 当前页路径  " . $dir -> pagepath . " <br> " ;
echo   " 当前路径  " . $dir -> path . " <br> " ;
$dir -> Execute( $dirlist , $statpath );
?>

 日历类

<? php
class  Calendar{

/*
*         日历
*
*     @作者:sports98
*     Email:flyruns@hotmail.com
*     @版本:V1.0
*/

  
var   $YEAR , $MONTH , $DAY ;
  
var   $WEEK = array ( " 星期日 " , " 星期一 " , " 星期二 " , " 星期三 " , " 星期四 " , " 星期五 " , " 星期六 " );
  
var   $_MONTH = array (
        
" 01 " => " 一月 " ,
        
" 02 " => " 二月 " ,
        
" 03 " => " 三月 " ,
        
" 04 " => " 四月 " ,
        
" 05 " => " 五月 " ,
        
" 06 " => " 六月 " ,
        
" 07 " => " 七月 " ,
        
" 08 " => " 八月 " ,
        
" 09 " => " 九月 " ,
        
" 10 " => " 十月 " ,
        
" 11 " => " 十一月 " ,
        
" 12 " => " 十二月 "
    );

  
// 设置年份
   function  setYear( $year ){
    
$this -> YEAR = $year ;
  }
  
// 获得年份
   function  getYear(){
    
return   $this -> YEAR;
  }
  
// 设置月份
   function  setMonth( $month ){
    
$this -> MONTH = $month ;
  }
  
// 获得月份
   function  getMonth(){
    
return   $this -> MONTH;
  }
  
// 设置日期
   function  setDay( $day ){
    
$this -> DAY = $day ;
  }
  
// 获得日期
   function  getDay(){
    
return   $this -> DAY;
  }
  
// 打印日历
   function  OUT(){
    
$this -> _env();
    
$week = $this -> getWeek( $this -> YEAR , $this -> MONTH , $this -> DAY); // 获得日期为星期几 (例如今天为2003-07-18,星期五)
     $fweek = $this -> getWeek( $this -> YEAR , $this -> MONTH , 1 ); // 获得此月第一天为星期几
     echo   " <div style="margin:0;border:1 solid black;width:300;font:9pt">
        <form action=$_SERVER[PHP_SELF] method="post" style="margin:0">
        <select name="month" οnchange="this.form.submit();">
" ;

    
for ( $ttmpa = 1 ; $ttmpa < 13 ; $ttmpa ++ ){ // 打印12个月
         $ttmpb = sprintf ( " %02d " , $ttmpa );
        
if ( strcmp ( $ttmpb , $this -> MONTH) == 0 ){
          
$select = " selected style="background-color:#c0c0c0" " ;
        }
else {
          
$select = "" ;
        }
        
echo   " <option value="$ttmpb" $select> " . $this -> _MONTH[ $ttmpb ] . " </option> " ;
    }

    
echo   "    </select> <select name="year" οnchange="this.form.submit();"> " ; // 打印年份,前后10年
     for ( $ctmpa = $this -> YEAR - 10 ; $ctmpa < $this -> YEAR + 10 ; $ctmpa ++ ){
        
if ( $ctmpa > 2037 ){
          
break ;
        }
        
if ( $ctmpa < 1970 ){
          
continue ;
        }
        
if ( strcmp ( $ctmpa , $this -> YEAR) == 0 ){
          
$select = " selected style="background-color:#c0c0c0" " ;
        }
else {
          
$select = "" ;
        }
        
echo   " <option value="$ctmpa" $select>$ctmpa</option> " ;
    }
    
echo     " </select>
        </form>
        <table border=0 align=center>
" ;
    
for ( $Tmpa = 0 ; $Tmpa < count ( $this -> WEEK); $Tmpa ++ ){ // 打印星期标头
         echo   " <td> " . $this -> WEEK[ $Tmpa ];
    }
    
for ( $Tmpb = 1 ; $Tmpb <= date ( " t " , mktime ( 0 , 0 , 0 , $this -> MONTH , $this -> DAY , $this -> YEAR)); $Tmpb ++ ){ // 打印所有日期
         if ( strcmp ( $Tmpb , $this -> DAY) == 0 ){    // 获得当前日期,做标记
           $flag = "  bgcolor='#ff0000' " ;
        }
else {
          
$flag = '  bgcolor=#ffffff ' ;
        }
        
if ( $Tmpb == 1 ){     
          
echo   " <tr> " ;      // 补充打印
           for ( $Tmpc = 0 ; $Tmpc < $fweek ; $Tmpc ++ ){
            
echo   " <td> " ;
          }
        }
        
if ( strcmp ( $this -> getWeek( $this -> YEAR , $this -> MONTH , $Tmpb ) , 0 ) == 0 ){
          
echo   " <tr><td align=center $flag>$Tmpb " ;
        }
else {
          
echo   " <td align=center $flag>$Tmpb " ;
        }
    }
    
echo   " </table></div> " ;
  }
  
// 获得方法内指定的日期的星期数
   function  getWeek( $year , $month , $day ){
    
$week = date ( " w " , mktime ( 0 , 0 , 0 , $month , $day , $year )); // 获得星期
     return   $week ; // 获得星期
  }

  
function  _env(){
    
if ( isset ( $_POST [month])){    // 有指定月
         $month = $_POST [month];
    }
else {
        
$month = date ( " m " );    // 默认为本月
    }
    
if ( isset ( $_POST [year])){    // 有指年
         $year = $_POST [year];
    }
else {
        
$year = date ( " Y " );    // 默认为本年
    }
  
$this -> setYear( $year );
  
$this -> setMonth( $month );
  
$this -> setDay( date ( " d " ));

  }
}

$D = new  Calendar;
$D -> OUT();
?>
购物车的类,只用了一个Session
<?
class  Pages{
  
var   $cn ;      // 连接数据库游标
   var   $d ;          // 连接数据表的游标
   var   $result ;    // 结果
   var   $dsn ;      // dsn源
   var   $user ;      // 用户名   
   var   $pass ;      // 密码
  
  
var   $total ;      // 记录总数
   var   $pages ;      // 总页数
   var   $onepage ;    // 每页条数
   var   $page ;      // 当前页
   var   $fre ;      // 上一页
   var   $net ;      // 下一页
   var   $i ;        // 控制每页显示

  
# #############################
   # ####### 连接数据库 ##########
   function  getConnect( $dsn , $user , $pass ){
    
$this -> cn = @ odbc_connect ( $dsn , $user , $pass );
    
if ( ! $this -> cn){
        
$error = " 连接数据库出错 " ;
        
$this -> getMess( $error );
    } 
  }

  
# #############################
     # ####### 进行表的查询 #########
   function  getDo( $sql ){      // 从表中查询数据
     $this -> d = @ odbc_do ( $this -> cn , $sql );
    
if ( ! $this -> d){
        
$error = " 查询时发生了小错误...... " ;
        
$this -> getMess( $error );
    }
    
return   $this -> d;
  }
  
    
# ################################
     # ####### 求表中数据的总量 #########
   function  getTotal( $sql ){
    
$this -> sql = $sql ;
    
$dT = $this -> getDo( $this -> sql);      // 求总数的游标
     $this -> total = odbc_result ( $dT , " total " );      // 这里为何不能$this->d,total 为一个字段
    //   $this->total=@odbc_num_rows($dT);       //应唠叨老大的意见,odbc_num_rows 不能用,不知为何,返回为-1

     return   $this -> total;
  }

  
# #############################
   # ####### 进行表的查询 #########
   function  getList( $sql , $onepage , $page ){
    
if  ( $page <> ""  and  $page < 1 )        // 当此页小于时的处理
         $page = 1 ;
    
$this -> s = $sql ;
    
$this -> onepage = $onepage ;      // 每页显示的记录数
     $this -> page = $page ;      // 页数
     $this -> dList = $this -> getDo( $this -> s);      // 连接表的游标
     $this -> pages = ceil ( $this -> total / $this -> onepage);    // 计算大于指定数的最小整数
     if  ( $this -> page > $this -> pages)        // 当此页大于最大页的处理
         $this -> page = $this -> pages;
    
if ( $this -> pages == 0
        
$this -> pages ++ ;      // 不能取到第0页
     if ( ! isset ( $this -> page)) 
        
$this -> page = 1 ;
    
$this -> fre  =   $this -> page - 1 ;      // 将显示的页数
     $this -> nxt  =   $this -> page + 1
    
$this -> nums = ( $this -> page - 1 ) * $this -> onepage; 
    
return   $this -> dList; 
  }
  
  
# #############################
   function  getfetch_row( $dList ){
    
return   odbc_fetch_row ( $dList );
  }   

    
# #############################
   function  getresult( $dList , $num ){
    
return   odbc_result ( $dList , $num );
  }   
  
# #############################
   # ####### 翻页 ################
   function  getFanye(){
    
$str = "" ;
    
if ( $this -> page != 1 )
        
$str .= " <form name=go2to form method=Post action=' " . $PHP_SELF . " '><a href= " . $PHP_SELF . " ?page=1> 首页 </a><a href= " . $PHP_SELF . " ?page= " . $this -> fre . " > 前页 </a> " ;
    
else
        
$str .= " <font color=999999>首页 前页</font> " ;
    
if ( $this -> page < $this -> pages)
        
$str .= " <a href= " . $PHP_SELF . " ?page= " . $this -> nxt . " > 后页 </a> " ;
    
else
        
$str .= " <font color=999999> 后页 </font> " ;
    
if ( $this -> page != $this -> pages)
        
$str .= " <a href= " . $PHP_SELF . " ?page= " . $this -> pages . " > 尾页 </a> " ;
    
else
        
$str .= " <font color=999999> 尾页 </font> " ;
    
    
$str .= " " . $this -> pages . " " ;
    
$str .= " <font color='000064'> 转到 第<input type='text' name='page' size=2 maxLength=3 style='font-size: 9pt; color:#00006A; position: relative; height: 18' value= " . $this -> page . " >页</font>  "
    
$str .= " <input class=button type='button' value='确 定' οnclick=check() style='font-family: 宋体; font-size: 9pt; color: #000073; position: relative; height: 19'></form> " ;
    
return   $str ;
  }
  
  
# ###################################
   # ####### 对进行提交表单的验验 #########
   function  check()
  {
    
if  (isNaN(go2to . page . value))
        
echo   " javascript:alert('请正确填写转到页数!'); " ;
      
else   if  (go2to . page . value == "" ){
        
echo   " javascript:alert('请输入转到页数!'); " ;
      }
    
else {
        go2to
. submit();
    }
  }

  
function  getNums(){      // 每页最初的记录数
     return   $this -> nums;
  }
  
  
function  getOnepage(){      // 每页实际条数
     return   $this -> onepage;
  }

  
function  getI(){      // 暂未用
  //   $this->i=$this-pageone*($this->page-1)

     return   $this -> i;
  }
  
  
function  getPage(){
    
return   $this -> page;
  }

  
function  getMess( $error ){      // 定制消息
     echo " <center>$error</center> " ;
    
exit ;
  }
}
?>

<? php
//  测试程序
     $pg = new  Pages();
    
$pg -> getConnect( " dsn " , " user " , " password " );
    
$pg -> getTotal( " select count(*) as total from bul_file " );          // 连学生表求总数
     $pg -> getList( $sql , 3 , $page ); 
    
if ( $pg -> getNums() != 0 ){
      
for ( $i = 0 ; $i < $pg -> getNums(); $pg -> getfetch_row( $pg -> dList) , $i ++ );      // 同上
    }
    
echo   " <table width="400" border=1 cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#000000"> " ;
    
echo   $pg -> getFanye();
    
echo   " </td></tr></table> " ;
    
echo   " <table width="400" border="1" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#000000"> " ;
    
echo   " <tr bgcolor="#CCFF99"><td width="5%"><font face="隶书" size="4"><div align="center">ID</div></font></td>     <td width="50%"><font face="隶书" size="4"><div align="center">标题</div></font></td>     <td width="30%"><font face="隶书" size="4"><div align='center'>日期</div></font></td>     </tr>";
    $i=1;
    while($pg->getfetch_row($pg->dList)){
      $pg->getNums();
      echo 
" < tr >< td width = " 5%"> " . ( $pg -> nums + $i ) . " </a></td><td width="50%"><div align='center'><a href="list_bul.php?id= " . $pg -> getresult( $pg -> dList , 1 ) . " " target="left"> " . $pg -> getresult( $pg -> dList , 2 ) . " </a></div></td><td width="30%"><div align='center'> " . substr ( $pg -> getresult( $pg -> dList , 4 ) , 1 , 10 ) . " </div></td></tr> " ;
      
if ( $i == $pg -> getOnepage()){      // 跳出循环
           break ;
    }
    
$i ++ ;
?>

支持stmp认证、HTML格式邮件的类

 

<? php
/*  smtp client class  */
class  c_smtp_client 

  
var   $connection
  
var   $server
  
var   $elog_fp
  
var   $log_file = ' ./smtp_client.log '
  
var   $do_log = true
  
var   $need_auth = true ;
  
var   $username ;
  
var   $password ;

  
//  构造器
   function  c_smtp_client( $server = ''
  { 
    
if  ( ! $server )
    {
        
$this -> server = " localhost "
    }
    
else  
    {
        
$this -> server = $server
    }

    
$this -> connection  =   fsockopen ( $this -> server ,   25 ); 
    
if  ( $this -> connection  <=   0 return   0
    
fputs ( $this -> connection , " HELO xyz " ); 
  } 
  
  
function  email( $from_mail ,   $to_mail ,   $to_name ,   $header ,   $subject ,   $body
  { 
    
if  ( $this -> connection  <=   0 return   0
    
    
//  邮件用户认证
     if  ( $this -> need_auth)
    {
        
$this -> elog( " AUTH LOGIN " ,   1 ); 
        
fputs ( $this -> connection , " AUTH LOGIN " ); 
        
$this -> elog( fgets ( $this -> connection ,   1024 )); 
        
        
$base64_username = base64_encode ( $this -> username);
        
$this -> elog( " $base64_username " ,   1 );
        
fputs ( $this -> connection , " $base64_username " ); 
        
$this -> elog( fgets ( $this -> connection ,   1024 ));

        
$base64_password = base64_encode ( $this -> password);
        
$this -> elog( " $base64_password " ,   1 ); 
        
fputs ( $this -> connection , " $base64_password " ); 
        
$this -> elog( fgets ( $this -> connection ,   1024 ));
    }

    
$this -> elog( " MAIL FROM:$from_mail " ,   1 ); 
    
fputs ( $this -> connection , " MAIL FROM:$from_mail " ); 
    
$this -> elog( fgets ( $this -> connection ,   1024 )); 
    
    
$this -> elog( " RCPT TO:$to_mail " ,   1 ); 
    
fputs ( $this -> connection ,   " RCPT TO:$to_mail " ); 
    
$this -> elog( fgets ( $this -> connection ,   1024 )); 
    
    
$this -> elog( " DATA " ,   1 ); 
    
fputs ( $this -> connection ,   " DATA " ); 
    
$this -> elog( fgets ( $this -> connection ,   1024 )); 

    
$this -> elog( " Subject: $subject " ,   1 ); 
    
$this -> elog( " To: $to_name " ,   1 ); 
    
fputs ( $this -> connection , " Subject: $subject " ); 
    
fputs ( $this -> connection , " To: $to_name " ); 

    
if  ( $header
    { 
        
$this -> elog( $header ,   1 ); 
        
fputs ( $this -> connection ,   " $header " ); 
    } 

    
$this -> elog( "" ,   1 ); 
    
$this -> elog( $body ,   1 ); 
    
$this -> elog( " . " ,   1 ); 
    
fputs ( $this -> connection , " " ); 
    
fputs ( $this -> connection , " $body  " ); 
    
fputs ( $this -> connection , " . " ); 
    
$this -> elog( fgets ( $this -> connection ,   1024 )); 

    
return   1
  } 


  
function  send() 
  { 
    
if  ( $this -> connection) 
    { 
        
fputs ( $this -> connection ,   " QUIT " ); 
        
fclose ( $this -> connection); 
        
$this -> connection = 0
    } 
  } 

  
function  close() 
  { 
    
$this -> send(); 
  } 

  
function  elog( $text ,   $mode = 0
  { 
    
if  ( ! $this -> do_log)  return

    
//  open file 
     if  ( ! $this -> elog_fp) 
    { 
        
if  ( ! ( $this -> elog_fp = fopen ( $this -> log_file ,   ' a ' )))  return
        
fwrite ( $this -> elog_fp ,   " ------------------------------------------- " ); 
        
fwrite ( $this -> elog_fp ,   "  Sent  "   .   date ( " Y-m-d H:i:s " .   " " ); 
        
fwrite ( $this -> elog_fp ,   " ------------------------------------------- " ); 
    } 

    
//  write to log 
     if  ( ! $mode
    {
        
fwrite ( $this -> elog_fp ,   "    $text " ); 
    }
    
else
    {
        
fwrite ( $this -> elog_fp ,   " $text " ); 
    }
  }

?>  



c_mail
. php

Copy  code

<? php

  
/*  邮件发送类  */
  
require_once   dirname ( __FILE__ ) . " /c_smtp_client.php " ;
  
static   $c_smtp_client ;
  
if ( ! isset ( $c_smtp_client ))
  {
    
$c_smtp_client           =     &   new  c_smtp_client( $smtp_server [ ' name ' ]);
    
$c_smtp_client -> do_log      =     false ;
    
$c_smtp_client -> need_auth    =     $smtp_server [ ' need_auth ' ];
    
$c_smtp_client -> username    =     $smtp_server [ ' username ' ];
    
$c_smtp_client -> password    =     $smtp_server [ ' password ' ];   
  }

  
class  c_mail
  { 
    
//  html格式的Mail信笺
     function  html_mailer( $from_name , $from_mail , $to_name , $to_mail , $subject , $message , $reply_mail )
    {
        
$headers     =     "" ;
        
$headers     .=     " MIME-Version: 1.0 " ;
        
$headers     .=     " Content-type: text/html; charset=gb2312 " ;
        
$headers     .=     " From:  " . $from_name . " < " . $from_mail . " > " ;
        
$headers     .=     " To:  " . $to_name . " < " . $to_mail . " > " ;
        
$headers     .=     " Reply-To:  " . $from_name . " < " . $reply_mail . " > " ;
        
$headers     .=     " X-Priority: 1 " ;
        
$headers     .=     " X-MSMail-Priority: High " ;
        
$headers     .=     " X-Mailer: WebCMS MAIL SERV " ;

        
//  开始发送邮件
         if ( $smtp_server [ ' name ' ] == '' )
        {
          @
mail ( $to_mail ,   $subject ,   $message ,   $headers );
        }
        
else
        {
          
$c_smtp_client -> email( $from_mail , $to_mail , $to_name , $headers , $subject , $message );
          
$c_smtp_client -> send();     
        }
    }
}
?>


config
. inc . php

Copy  code
<? php
  
// smtp_server['name']=='';则表示直接使用mail();       
  //smtp_server['name']=='localhost';表示程序所在主机的smtp服务器 

   $smtp_server [ ' name ' ]          =     "" ;
  
$smtp_server [ ' need_auth ' ]      =     true ;
  
$smtp_server [ ' username ' ]      =     '' ;
  
$smtp_server [ ' password ' ]      =     '' ;
?>


sendmail
. php

<? php
  
// 调用方法
   require_once   dirname ( __FILE__ ) . " /config.inc.php " ;
  
require_once   dirname ( __FILE__ ) . " /c_mail.php " ;
  
$c_mail     =   new  c_mail();
  
$c_mail -> html
一个操作xml的类
<?
/*  
  (c) 2000 Hans Anderson Corporation. All Rights Reserved. 
  You are free to use and modify this class under the same 
  guidelines found in the PHP License. 

  ----------- 

  bugs/me: 
  [url]http://www.hansanderson.com/php/[/url] 
  [email]me@hansanderson.com[/email] 
  [email]showstv@163.com[/email]

  ----------- 

  Version 1.0 

    - 1.0 is the first actual release of the class. It's 
      finally what I was hoping it would be, though there 
      are likely to still be some bugs in it. This is 
      a much changed version, and if you have downloaded 
      a previous version, this WON'T work with your existing 
      scripts! You'll need to make some SIMPLE changes. 

    - .92 fixed bug that didn't include tag attributes 

      (to use attributes, add _attributes[array_index] 
      to the end of the tag in question: 
        $xml_html_head_body_img would become 
        $xml_html_head_body_img_attributes[0], 
      for example) 

      -- Thanks to Nick Winfield <[email]nick@wirestation.co.uk[/email]> 
        for reporting this bug. 

    - .91 No Longer requires PHP4! 

    - .91 now all elements are array. Using objects has 
      been discontinued. 
*/

class  xml_container{ 

  
function  store( $k , $v ) { 
    
$this -> { $k }[]  =   $v
  } 
}


/*  parses the information  */  
/* ********************************
*   类定义开始
*
********************************
*/
class  xml{
  
  
//  initialize some variables 
   var   $current_tag = array (); 
  
var   $xml_parser
  
var   $Version   =   1.0
  
var   $tagtracker   =   array (); 

  
/*  Here are the XML functions needed by expat  */  


  
/*  when expat hits an opening tag, it fires up this function  */  
  
function  startElement( $parser ,   $name ,   $attrs ){ 

    
array_push ( $this -> current_tag ,   $name );  //  add tag to the cur. tag array 
     $curtag   =   implode ( " _ " , $this -> current_tag);  //  piece together tag 

    
/*  this tracks what array index we are on for this tag  */  

    
if ( isset ( $this -> tagtracker[ " $curtag " ])) { 
        
$this -> tagtracker[ " $curtag " ] ++
    }
    
else
        
$this -> tagtracker[ " $curtag " ] = 0
    }

    
/*  if there are attributes for this tag, we set them here.  */  

    
if ( count ( $attrs ) > 0 ) { 
        
$j   =   $this -> tagtracker[ " $curtag " ]; 
        
if ( ! $j $j   =   0

        
if ( ! is_object ( $GLOBALS [ $this -> identifier][ " $curtag " ][ $j ])) { 
          
$GLOBALS [ $this -> identifier][ " $curtag " ][ $j =   new  xml_container; 
        } 

        
$GLOBALS [ $this -> identifier][ " $curtag " ][ $j ] -> store( " attributes " , $attrs ); 
    } 
  
  }
//  end function startElement 



  
/*  when expat hits a closing tag, it fires up this function  */  
  
function  endElement( $parser ,   $name ) { 
    
$curtag   =   implode ( " _ " , $this -> current_tag);  //  piece together tag 
    
    // before we pop it off, 
    // so we can get the correct 
    // cdata 


    
if ( ! $this -> tagdata[ " $curtag " ]) { 
        
$popped   =   array_pop ( $this -> current_tag);  //  or else we screw up where we are 
         return //  if we have no data for the tag 
    }
    
else
        
$TD   =   $this -> tagdata[ " $curtag " ]; 
        
unset ( $this -> tagdata[ " $curtag " ]); 
    } 

    
$popped   =   array_pop ( $this -> current_tag); 
    
//  we want the tag name for 
    // the tag above this, it 
    // allows us to group the 
    // tags together in a more 
    // intuitive way. 


    
if ( sizeof ( $this -> current_tag)  ==   0 return //  if we aren't in a tag 

    
$curtag   =   implode ( " _ " , $this -> current_tag);  //  piece together tag 
    // this time for the arrays 


    
$j   =   $this -> tagtracker[ " $curtag " ]; 
    
    
if ( ! $j $j   =   0

    
if ( ! is_object ( $GLOBALS [ $this -> identifier][ " $curtag " ][ $j ])) { 
        
$GLOBALS [ $this -> identifier][ " $curtag " ][ $j =   new  xml_container; 
    }

    
$GLOBALS [ $this -> identifier][ " $curtag " ][ $j ] -> store( $name , $TD );
    
# $this->tagdata["$curtag"]); 
     unset ( $TD ); 
    
return   TRUE
  } 
//  end function endElement


  
/*  when expat finds some internal tag character data, 
    it fires up this function 
*/  

  
function  characterData( $parser ,   $cdata ) { 
    
$curtag   =   implode ( " _ " , $this -> current_tag);  //  piece together tag 
     $this -> tagdata[ " $curtag " .=   $cdata
  }


  
function  xml( $data , $identifier = ' xml ' ) {   

    
$this -> identifier  =   $identifier

    
//  create parser object 
     $this -> xml_parser  =   xml_parser_create (); 

    
//  set up some options and handlers 
     xml_set_object ( $this -> xml_parser , $this ); 
    
xml_parser_set_option ( $this -> xml_parser , XML_OPTION_CASE_FOLDING , 0 ); 
    
xml_set_element_handler ( $this -> xml_parser ,   " startElement " ,   " endElement " ); 
    
xml_set_character_data_handler ( $this -> xml_parser ,   " characterData " ); 

    
if  ( ! xml_parse ( $this -> xml_parser ,   $data ,   TRUE )) { 
        
sprintf ( " XML error: %s at line %d " ,  
        
xml_error_string ( xml_get_error_code ( $this -> xml_parser)) ,  
        
xml_get_current_line_number ( $this -> xml_parser)); 
    }

    
//  we are done with the parser, so let's free it 
     xml_parser_free ( $this -> xml_parser); 

  }
// end constructor: function xml() 


}
// thus, we end our class xml 

?>  




操作方法:

require ( ' class.xml.php ' ); 
$file   =   " data.xml "
$data   =   implode ( "" , file ( $file )) or  die ( " could not open XML input file " ); 
$obj   =   new  xml( $data , " xml " ); 


print   $xml [ " hans " ][ 0 ] -> num_results[ 0 ]; 
for ( $i = 0 ; $i < sizeof ( $xml [ " hans " ]); $i ++ ) { 
print   $xml [ " hans " ][ $i ] -> tag[ 0 .   "   "


To 
print  url attributes ( if  they exist) :  

print   $xml [ " hans " ][ 0 ] -> attributes[ 0 ][ " size " ];

php输出控制类

 

<? php
/* *

* 作者: 徐祖宁 (唠叨)
* 邮箱: [email]czjsz_ah@stats.gov.cn[/email]
* 开发: 2002.07


* 类: outbuffer
* 功能: 封装部分输出控制函数,控制输出对象。

* 方法:
* run($proc)           运行php程序
*   $proc   php程序名
* display()           输出运行结果
* savetofile($filename)   保存运行结果到文件,一般可用于生成静态页面
*   $filename 文件名
* loadfromfile($filename)   装入保存的文件
*   $filename 文件名

* 示例:
* 1.
* require_once "outbuffer.php";
* $out = new outbuffer();
* $out->run("test.php");
* $out->display();

* 2.
* require_once "outbuffer.php";
* require_once "outbuffer.php";
* $out = new outbuffer("test.php");
* $out->savetofile("temp.htm");

* 3.
* require_once "outbuffer.php";
* $out = new outbuffer();
* $out->loadfromfile("temp.htm");
* $out->display();

*/

class  outbuffer {
var   $length ;
var   $buffer ;
function  outbuffer( $proc = "" ) {
  
$this -> run( $proc );
}
function  run( $proc = "" ) {
  
ob_start ();
  
include ( $proc );
  
$this -> length  =   ob_get_length ();
  
$this -> buffer  =   ob_get_contents ();
  
$this -> buffer  =   eregi_replace ( " ? " , " " , $this -> buffer);
  
ob_end_clean ();
}
function  display() {
  
echo   $this -> buffer;
}
function  savetofile( $filename = "" ) {
  
if ( $filename   ==   "" return ;
  
$fp   =   fopen ( $filename , " w " );
  
fwrite ( $fp , $this -> buffer);
  
fclose ( $fp );
}
function  loadfromfile( $filename = "" ) {
  
if ( $filename   ==   "" return ;
  
$fp   =   fopen ( $filename , " w " );
  
$this -> buffer  =   fread ( $fp , filesize ( $filename ));
  
fclose ( $fp );
}
}
?>

一个分页导航类

 

<? php
//  +----------------------------------------------------------------------+
// | PHP Version 4                                     |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                     |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,     |
// | that is bundled with this package in the file LICENSE, and is     |
// | available at through the world-wide-web at                   |
// | [url]http://www.php.net/license/2_02.txt.[/url]                       |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to       |
// | [email]license@php.net[/email] so we can mail you a copy immediately.           |
// +----------------------------------------------------------------------+
// | Authors: Richard Heyes <[email]richard@phpguru.org[/email]>                 |
// +----------------------------------------------------------------------+


/* *
* Pager class
*
* Handles paging a set of data. For usage see the example.php provided.
*
*/

class  Pager {

  
/* *
  * Current page
  * @var integer
  
*/
  
var   $_currentPage ;

  
/* *
  * Items per page
  * @var integer
  
*/
  
var   $_perPage ;

  
/* *
  * Total number of pages
  * @var integer
  
*/
  
var   $_totalPages ;

  
/* *
  * Item data. Numerically indexed array...
  * @var array
  
*/
  
var   $_itemData ;

  
/* *
  * Total number of items in the data
  * @var integer
  
*/
  
var   $_totalItems ;

  
/* *
  * Page data generated by this class
  * @var array
  
*/
  
var   $_pageData ;

  
/* *
  * Constructor
  *
  * Sets up the object and calculates the total number of items.
  *
  * @param $params An associative array of parameters This can contain:
  *             currentPage   Current Page number (optional)
  *             perPage     Items per page (optional)
  *             itemData     Data to page
  
*/
  
function  pager( $params   =   array ())
  {
    
global   $HTTP_GET_VARS ;

    
$this -> _currentPage  =   max ((int)@ $HTTP_GET_VARS [ ' pageID ' ] ,   1 );
    
$this -> _perPage    =   8 ;
    
$this -> _itemData    =   array ();

    
foreach  ( $params   as   $name   =>   $value ) {
        
$this -> { ' _ '   .   $name =   $value ;
    }

    
$this -> _totalItems  =   count ( $this -> _itemData);
  }

  
/* *
  * Returns an array of current pages data
  *
  * @param $pageID Desired page ID (optional)
  * @return array Page data
  
*/
  
function  getPageData( $pageID   =   null )
  {
    
if  ( isset ( $pageID )) {
        
if  ( ! empty ( $this -> _pageData[ $pageID ])) {
          
return   $this -> _pageData[ $pageID ];
        } 
else  {
          
return   FALSE ;
        }
    }

    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
return   $this -> getPageData( $this -> _currentPage);
  }

  
/* *
  * Returns pageID for given offset
  *
  * @param $index Offset to get pageID for
  * @return int PageID for given offset
  
*/
  
function  getPageIdByOffset( $index )
  {
    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
if  (( $index   %   $this -> _perPage)  >   0 ) {
        
$pageID   =   ceil (( float ) $index   /  ( float ) $this -> _perPage);
    } 
else  {
        
$pageID   =   $index   /   $this -> _perPage;
    }

    
return   $pageID ;
  }

  
/* *
  * Returns offsets for given pageID. Eg, if you
  * pass it pageID one and your perPage limit is 10
  * it will return you 1 and 10. PageID of 2 would
  * give you 11 and 20.
  *
  * @params pageID PageID to get offsets for
  * @return array First and last offsets
  
*/
  
function  getOffsetByPageId( $pageid   =   null )
  {
    
$pageid   =   isset ( $pageid ?   $pageid   :   $this -> _currentPage;
    
if  ( ! isset ( $this -> _pageData)) {
        
$this -> _generatePageData();
    }

    
if  ( isset ( $this -> _pageData[ $pageid ])) {
        
return   array (( $this -> _perPage  *  ( $pageid   -   1 ))  +   1 ,   min ( $this -> _totalItems ,   $this -> _perPage  *   $pageid ));
    } 
else  {
        
return   array ( 0 , 0 );
    }
  }

  
/* *
  * Returns back/next and page links
  *
  * @param $back_html HTML to put inside the back link
  * @param $next_html HTML to put inside the next link
  * @return array Back/pages/next links
  
*/
  
function  getLinks( $back_html   =   ' << Back ' ,   $next_html   =   ' Next >> ' )
  {
    
$url     =   $this -> _getLinksUrl();
    
$back   =   $this -> _getBackLink( $url ,   $back_html );
    
$pages   =   $this -> _getPageLinks( $url );
    
$next   =   $this -> _getNextLink( $url ,   $next_html );

    
return   array ( $back ,   $pages ,   $next ,   ' back '   =>   $back ,   ' pages '   =>   $pages ,   ' next '   =>   $next );
  }

  
/* *
  * Returns number of pages
  *
  * @return int Number of pages
  
*/
  
function  numPages()
  {
    
return   $this -> _totalPages;
  }

  
/* *
  * Returns whether current page is first page
  *
  * @return bool First page or not
  
*/
  
function  isFirstPage()
  {
    
return  ( $this -> _currentPage  ==   1 );
  }

  
/* *
  * Returns whether current page is last page
  *
  * @return bool Last page or not
  
*/
  
function  isLastPage()
  {
    
return  ( $this -> _currentPage  ==   $this -> _totalPages);
  }

  
/* *
  * Returns whether last page is complete
  *
  * @return bool Last age complete or not
  
*/
  
function  isLastPageComplete()
  {
    
return   ! ( $this -> _totalItems  %   $this -> _perPage);
  }

  
/* *
  * Calculates all page data
  
*/
  
function  _generatePageData()
  {
    
$this -> _totalItems  =   count ( $this -> _itemData);
    
$this -> _totalPages  =   ceil (( float ) $this -> _totalItems  /  ( float ) $this -> _perPage);
    
$i   =   1 ;
    
if  ( ! empty ( $this -> _itemData)) {
        
foreach  ( $this -> _itemData  as   $value ) {
          
$this -> _pageData[ $i ][]  =   $value ;
          
if  ( count ( $this -> _pageData[ $i ])  >=   $this -> _perPage) {
            
$i ++ ;
          }
        }
    } 
else  {
        
$this -> _pageData  =   array ();
    }
  }

  
/* *
  * Returns the correct link for the back/pages/next links
  *
  * @return string Url
  
*/
  
function  _getLinksUrl()
  {
    
global   $HTTP_SERVER_VARS ;

    
//  Sort out query string to prevent messy urls
     $querystring   =   array ();
    
if  ( ! empty ( $HTTP_SERVER_VARS [ ' QUERY_STRING ' ])) {
        
$qs   =   explode ( ' & ' ,   $HTTP_SERVER_VARS [ ' QUERY_STRING ' ]);         
        
for  ( $i   =   0 ,   $cnt   =   count ( $qs );  $i   <   $cnt $i ++ ) {
          
list ( $name ,   $value =   explode ( ' = ' ,   $qs [ $i ]);
          
if  ( $name   !=   ' pageID ' ) {
            
$qs [ $name =   $value ;
          }
          
unset ( $qs [ $i ]);
        }
    }
  
if ( is_array ( $qs )){
        
foreach  ( $qs   as   $name   =>   $value ) {
          
$querystring []  =   $name   .   ' = '   .   $value ;
        }   
  }
    
return   $HTTP_SERVER_VARS [ ' SCRIPT_NAME ' .   ' ? '   .   implode ( ' & ' ,   $querystring .  ( ! empty ( $querystring ?   ' & '   :   '' .   ' pageID= ' ;
  }

  
/* *
  * Returns back link
  *
  * @param $url URL to use in the link
  * @param 上一篇     目录     下一篇 HTML to use as the link
  * @return string The link
  
*/
  
function  _getBackLink( $url ,  上一篇     目录     下一篇  =   ' << Back ' )
  {
    
//  Back link
     if  ( $this -> _currentPage  >   1 ) {
        
$back   =   ' <a href=" '   .   $url   .  ( $this -> _currentPage  -   1 .   ' "> '   .  上一篇     目录     下一篇  .   ' </a> ' ;
    } 
else  {
        
$back   =   '' ;
    }
    
    
return   $back ;
  }

  
/* *
  * Returns pages link
  *
  * @param $url URL to use in the link
  * @return string Links
  
*/
  
function  _getPageLinks( $url )
  {
    
//  Create the range
     $params [ ' itemData ' =   range ( 1 ,   max ( 1 ,   $this -> _totalPages));
    
$pager   =&   new  Pager( $params );
    上一篇     目录     下一篇s 
=   $pager -> getPageData( $pager -> getPageIdByOffset( $this -> _currentPage));

    
for  ( $i = 0 $i < count (上一篇     目录     下一篇s);  $i ++ ) {
        
if  (上一篇     目录     下一篇s[ $i !=   $this -> _currentPage) {
          上一篇     目录     下一篇s[
$i =   ' <a href=" '   .   $this -> _getLinksUrl()  .  上一篇     目录     下一篇s[ $i .   ' "> '   .  上一篇     目录     下一篇s[ $i .   ' </a> ' ;
        }
    }

    
return   implode ( '   ' ,  上一篇     目录     下一篇s);
  }

  
/* *
  * Returns next link
  *
  * @param $url URL to use in the link
  * @param 上一篇     目录     下一篇 HTML to use as the link
  * @return string The link
  
*/
  
function  _getNextLink( $url ,  上一篇     目录     下一篇  =   ' Next >> ' )
  {
    
if  ( $this -> _currentPage  <   $this -> _totalPages) {
        
$next   =   ' <a href=" '   .   $url   .  ( $this -> _currentPage  +   1 .   ' "> '   .  上一篇     目录     下一篇  .   ' </a> ' ;
    } 
else  {
        
$next   =   '' ;
    }

    
return   $next ;
  }
}

?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值