LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1684|回复: 6

[PHP]用代码说话:怎么写一个类

[复制链接]
发表于 2005-5-26 16:15:14 | 显示全部楼层 |阅读模式
[PHP]
<?php
/*
PAGE CLASS
Copyright (C) 2004-2005 Harry Wang
GNU General Public License 321
This program is free software; you can
redistribute it and/or modify it under the
terms of the GNU General Public License as
published by the Free Software Foundation;
either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU
General Public License along with this
program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
/**
* 數據庫分頁?
* 這個數據庫分頁?可以使用在帶有ADODB的數據庫中
* @author                Weizhen Wang <harry34010493@gmail.com>
* @version        $Id: page.inc.php,v 1.9 2005/03/24 19:38:21 justin Exp $
* @example  /inc/example/page.example.php
* 使用簡介:
* $p = new ShowPage(根据?($DB), 表名字, 分頁條數, 具体sql語句(可選));                //建立新對象
* $p->SetPageVar(pagecount);        //設置翻頁的標示,默認是'p'
* $pVar = $p->GetPageVar();    //返回當前PageVar
* $p->GetTotalPageArray();     //生成下拉菜單
* $p->GetTotalPage();          //返回總頁數
* $p->GetLeftSide();                        //返回向左的箭頭
* $p->GetRightSide();                        //返回向右的箭頭
* $p->GetCurrentPage();                //返回當前頁面
* $p->GetLimit();                                //返回每頁顯示條數                       
* $p->GetRowFrom();                        //返回Select語句用的當前Row的位置    E.G."SELECT * FROM SupplierLogin LIMIT " . $p->GetRowFrom() . ", " . $p->GetLimit();
* $p->SetVar();                                //設置同時需要傳送的參數
*/
class ShowPage
{
        /**
         * 左翻頁
         * @var string
         */
        var $outputLeftSide;
       
        /**
         * 右翻頁
         * @var string
         */
        var $outputRightSide;
        var $file;
       
        /**
         * 翻頁參數
         * @var string
         */
        var $pVar = "p";
       
        /**
         * 當前頁
         * @var int
         */
        var $curr;
        var $varstr;
        var $tPage;
       
        /**
         * 數據庫對象
         * @var DB
         */
        var $DB;
       
        /**
         * 數據庫表名
         * @var string
         */
        var $table;
       
        /**
         * 每頁顯示的數據條數
         * @var string
         */
        var $limit;
       
        /**
         * SQL語句
         * @var string
         */
        var $sqlQuery;
        var $tPageArray;
        var $total;
    var $tRow;
###############################################################################
        /**
         * 構造函數
         * @param  db $DB 數據庫對象,默認是ADODB
         * @param         string $table 數據庫表名
         * @param         int $limit 每頁顯示的數據條數
         * @param         string $sqlQuery SQL語句(可選)
         * @todo         完成其他DB對象
         */
        function ShowPage($DB, $table, $limit, $sqlQuery = "") {   
                // $DB is a object contain some method access to mySql
                // $table is the table name you want to access (if you want to access only some part of the table you should use the fourth parameter instead)
                // $limit is the page limit value
                // $sqlQuery must be a SQL Query (E.G. SELECT * FROM table WHERE id like '100';)
                $this->DB                = $DB;
                $this->table        = $table;
                $this->limit        = $limit;
                $this->sqlQuery        = $sqlQuery;

                $tmpGET = $_GET;
                unset($tmpGET[$this->pVar]);
                $this->setVar($tmpGET);

                if ($sqlQuery == "") {
                        $tmpQuery = "SELECT * FROM $this->table";
                }
                else {
                        $tmpQuery = $sqlQuery;
                }
                //adodb hack start
                $rs =& $DB->Execute($tmpQuery) or die($DB->ErrorMsg());
                //$tPage =$this->total= $DB->Affected_Rows( );
                $tPage = $rs->RecordCount();
        $this->tRow = $tPage;
                //adodb hack end
                $this->tPage = ceil($tPage / $this->limit);    // caculate the total page
                if ($this->tPage > 0) {
                        // build an array which contain all page number (E.G. array(1, 2, 3, 4))
                        for ($i = 1; $i < $this->tPage + 1; $i++) {
                                $this->tPageArray[] = $i;
                        }
                }
                else {
                        $this->tPageArray[] = 0;
                }
        }


    /**
         * 設置翻頁參數
         * @param string $pvar  翻頁參數
         */
        function SetPageVar($pvar = 'p')  {   
        // set type turnpage parameter (E.G. http://www.aa.com/index.php?p=1)
                $this->pVar = $pvar;
        }

        /**
         * 返回總行數
         * @return  int
         */

        function GetTotalRow() {
                return $this->tRow;
        }


    /**
         * 返回翻頁參數
         * @return string
         */
        function GetPageVar() {
                return $this->pVar;
        }


    /**
         * 返回當前頁數
         * @return int
         */
        function GetCurrentPage() {
                return  $_GET[$this->pVar];
        }


    /**
         * 返回總頁數數組
         * @return array
         */
        function GetTotalPageArray() {
                return $this->tPageArray;
        }


    /**
         * 返回總頁數
         * @return int
         */
        function GetTotalPage() {
                return $this->tPage;
        }


    /**
         * 返回每頁顯示條數
         * @return int
         */
        function GetLimit() {   
                return $this->limit;
        }

        /**
         * 返回數據行數
         * @return  int
         */
        function GetRowFrom() {
                if ($this->GetCurrentPage() <= 1) {
                        return 0;
                }
                else {
                        return ($this->GetCurrentPage() - 1) * $this->GetLimit();
                }
        }


        /**
         * 返回朝左翻
         * @return  string
         */
        function GetLeftSide() {
                $current = $_GET[$this->pVar];
               
               
                if (!$current) {
                        $current = 1;
                }

                if ($current<1) {
                        $current = 1;
                }

                if ($current>$this->tPage) {
                        $current = $this->tPage;
                }

                if ($this->tPage > 1) {            
                        if ($current > 10) {
                                $this->outputLeftSide = '<a href='.$this->file.'?'.$this->pVar.'='.($current-10).($this->varstr).' title="revious 10 Pages">&lt;&lt;&lt;</a>&nbsp;';
                        }
            if ($current > 1) {
                                $this->outputLeftSide .= '<a href='.$this->file.'?'.$this->pVar.'='.($current-1).($this->varstr).' title="revious Page">&lt;&lt;</a>&nbsp;';
                        }
                }
               
                return str_replace('"', "'", $this->outputLeftSide);
        }

       
        /**
         * 返回朝右翻
         * @return  string
         */
        function GetRightSide() {
                $current = $_GET[$this->pVar];

                if (!$current) {
                        $current = 1;
                }

                if ($current>$this->tPage) {
                        $current = $this->tPage;
                }

                if ($current<1) {
                        $current = 1;
                }

                if ($this->tPage > 1) {            
                        if ($current<$this->tPage) {
                                $this->outputRightSide = '<a href='.$this->file.'?'.$this->pVar.'='.($current+1).($this->varstr).' title="Next Page">&gt;&gt;</a>&nbsp;';
                        }
            if ($this->tPage>10 && ($this->tPage-$current)>=10 ) {
                                $this->outputRightSide .= '<a href='.$this->file.'?'.$this->pVar.'='.($current+10).($this->varstr).' title="Next 10 Page">&gt;&gt;&gt;</a>';
                        }
                }
               
                return str_replace('"', "'", $this->outputRightSide);
        }

       
        /**
         * 返回一個完整的翻頁選項
         * 包含了向左翻(<< <), 直接選擇頁面數字, 向右翻(> >>), 和所需要的Javascript
         * @return string
         */
        function GetFullSide() {
                $sLeftSide = $this->GetLeftSide();
                $sRightSide = $this->GetRightSide();
                $sCenter = "<select onChange='fnJump(this.value)' name='select'>\n";
                $pages = $this->GetTotalPageArray();
                foreach($pages as $page) {
                        $selected = "";
                        if($page == $this->GetCurrentPage())$selected = "selected";
                        $sCenter .= "        <option value=\"$page\" $selected>$page</option>\n";
                }
                $sCenter .= "</select>\n";

                $sScript = $this->GetTurnPageScript();
               
                return ($sLeftSide ."\n". $sCenter ."\n". $sRightSide ."\n". $sScript);
        } // end func

       
        /**
         * 返回Sql語句
         * @return string
         */
        function GetNewSql() {
                return $this->sqlQuery . " LIMIT " . $this->GetRowFrom() . ", " . $this->GetLimit();
        } // end func

       
        /**
         * 返回翻頁時所需的Javascript
         * @return string
         */
        function GetTurnPageScript() {
                return        "<SCRIPT LANGUAGE=\"JavaScript\">\n" .
                                "<!--\n" .
                                "function fnJump(page)\n" .
                                "{\n" .
                                "location.href='".$_SERVER['PHP_SELF']."?" . $this->GetPageVar() ."='+page" . "+'".($this->varstr) . "';\n" .
                                "}\n" .
                                "//-->\n" .
                                "</SCRIPT>\n";
        } // end func

       
        /**
         * 設置其它參數
         * 設置翻頁時需要通過用GET方法一同傳送過去的其他參數
         * @param  array $data
         */
        function setVar($data) {   
                // set the turnpage addition value need to transfer by get mode
                foreach ($data as $k=>$v) {
                        $this->varstr.='&'.$k.'='.urldecode($v);
                }
        }
}

?>
[/PHP]
发表于 2005-5-26 21:56:09 | 显示全部楼层
好东西,学习中!
回复 支持 反对

使用道具 举报

发表于 2005-5-28 09:38:17 | 显示全部楼层
$tmpQuery = "SELECT * FROM $this->table";
我觉得这种放在字符串里的多个单词的变量, 最好能加上花括号:
$tmpQuery = "SELECT * FROM ${this->table}";
回复 支持 反对

使用道具 举报

发表于 2005-5-28 10:48:56 | 显示全部楼层
楼上懂不懂?不懂不要瞎说!$tmpQuery = "SELECT * FROM $this->table";中Sthis是一个对象,table是$this对象的属性,你用花括号把this->table包起来PHP如何解析???
回复 支持 反对

使用道具 举报

发表于 2005-5-28 11:44:35 | 显示全部楼层
sorry, 我是php初学者, 有点想当然了~ 刚才试了一下, 确实不能这样写.
请问要怎么解决连续字符串的问题呢? 只能通过点号连接么? 如:
$var1 = "nospacetofix$this->tablethesubfixishere";
只能写成:
$var1 = "nospacetofix". $this->table . "thesubfixishere";
吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-28 13:56:00 | 显示全部楼层
Post by lexrus
sorry, 我是php初学者, 有点想当然了~ 刚才试了一下, 确实不能这样写.
请问要怎么解决连续字符串的问题呢? 只能通过点号连接么? 如:
$var1 = "nospacetofix$this->tablethesubfixishere";
只能写成:
$var1 = "nospacetofix". $this->table . "thesubfixishere";
吗?


是$var1 = "nospacetofix{$this->table}thesubfixishere";
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-28 14:06:45 | 显示全部楼层
继续,这个是一个发送邮件的class,需要pear的Mail和mail_Mime class
支持html和多个mime_part,当然还有附件


[PHP]
<?php
/**
* file name here
*
* discription here
*
*
* @author: nbxmedia <nbxmedia@163.com>
* @version: $Id$
*/

//require pear class Mail and Mail_Mime
include_once("Mail.php");
include_once("Mail/mime.php");
include_once("Mail/mimePart.php");



//rebuild this class for using pear
class ComposeMail
{
        //mail headers
        var $headers = array();

        //charset
        var $charset = "utf-8";

        //mail body
        var $body = "";
        var $textbody = "";
        var $htmlbody = "";


        //mail receiver
        var $to = "";

        //form name
        var $from_name = "";
        var $from_addr = "";

        //smtp access
        var $smtp = array();

        //sendmail function
        var $send_type = "mail";

        //mail
        var $mime;

        //constractor
        function ComposeMail($to, $subject, $charset="utf-8")
        {
                $this->to = $to;
                $this->headers['Subject'] = $subject;
                $this->charset = $charset;

                $this->mime =& new Mail_mime();
        }

        //set return-path
        function SH_ReturnPath($rp)
        {
                $this->headers['Return-Path'] = "<$rp>";
        }

        //set Envelope-To
        function SH_EnvelopeTo($et)
        {
                $this->headers['Envelope-to'] = $et;
        }

        /*
        This function sets the FromName header (internal)
        */
        function SH_FromName($fromName)
        {
                $this->from_name=$fromName;
        }
       
        /*
        This function sets the FromAddr header (internal)
        */
        function SH_FromAddr($fromAddr)
        {
                $this->from_addr=$fromAddr;
        }


        //html body
        function addHtmlBodyPart($html)
        {
                $meta = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset={$this->charset}\">\n";
               
                if(strpos($html, $meta) === false)$html = $meta.$html;
                $this->mime->setHTMLBody($html);

        }
       

        //text body
        function addTextPlainBodyPart($text)
        {
                $this->mime->setTXTBody($text);
        }


        //add attach from data or file
        function addAttachFromFile($fullname, $ftype='application/octet-stream')
        {
                $this->mime->addAttachment($fullname, $ftype);
        }

        function addAttachFromData($data, $dname, $dtype='application/octet-stream', $dispos="")
        {
                $this->mime->addAttachment($data, $dtype, $dname, false);
        }


        //complete and send message
        function BuildAndSendMessage()
        {
                $this->headers['From'] = $this->from_name."<$this->from_addr>";
                $param["text_charset"] = $this->charset;
                $param["html_charset"] = $this->charset;
                $param["head_charset"] = $this->charset;
                $param["html_encoding"] = "base64";

                $this->body = $this->mime->get($param);
                $this->headers = $this->mime->headers($this->headers);

                $mail =& Mail::factory($this->send_type, $this->smtp);
                $mail->send($this->to, $this->headers, $this->body);
        }

}

?>
[/PHP]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表