我有个类似的问题:为什么要用分页函数?直接用一段代码,COPY过来,COPY过去不也可以么?用函数很麻烦还要跟参数才能使用。----------------呵呵,开玩笑,道理差不多,体会体会~~

解决方案 »

  1.   

    个人觉得,如果你只需要输出一个值的话,那用函数也可以.
    如果需要输出多个值的话,还是用类好一点.其实,何必在意是类还是函数???能写出简洁有效的代码就可以了.
    我们还用小米加步枪打败了.X...X....X呢....赢了才是好汉
      

  2.   

    我们还用小米加步枪打败了.X...X....X呢....
    但是如果能有飞机大炮不是更好么
      

  3.   

    <?
    class page
    {
            /*
            分页类 zonwen Wong  2004-1-5 PM
            */        var $total_RecordNum = 0;
            var $curr_PageNum = 1;
            var $limit_Num = 21;
            var $total_Pages = 1;
            var $startRecord = 1;
            var $endRecord = 20;
            var $thisPageRecords = 0;
            var $queryString = "";        /*
            function page($totalRecordNum, $limit, $pagenum = 1)
            {
                    $this->setLimit($limit);
                    $this->setTotalRecordNum($totalRecordNum);
                    $this->setCurrPageNum($pagenum);
                    $this->computePage();
            }
            */
            function page($totalRecordNum, $limit, $pagenum = 0)
            {
                    $this->setLimit($limit);
                    $this->setTotalRecordNum($totalRecordNum);
                    if($pagenum !=0)
                            $this->setCurrPageNum($pagenum);
                    else
                            $this->setCurrPageNum($_REQUEST[pageNum]);
                    $this->computePage();
            }
            function computePage()
            {
                    $this->total_Pages = ceil($this->total_RecordNum / $this->limit_Num);
                    if ($this->curr_PageNum > $this->total_Pages)
                            $this->curr_PageNum = $this->total_Pages;
                    if ($this->total_Pages == 0)
                    {
                            $this->startRecord = 0;
                            $this->endRecord = 0;
                            $this->thisPageRecords = 0;
                            $this->total_Pages = 1;
                            $this->curr_PageNum = 1;
                    }
                    else
                    {
                            $this->startRecord = $this->limit_Num * ($this->curr_PageNum - 1) + 1;
                            if ($this->curr_PageNum < $this->total_Pages)
                            {
                                    $this->endRecord = $this->curr_PageNum * $this->limit_Num;
                                    $this->thisPageRecords = $this->limit_Num;
                            }
                            else
                            {
                                    $this->endRecord = $this->total_RecordNum;
                                    $this->thisPageRecords = $this->total_RecordNum - ($this->curr_PageNum-1) * $this->limit_Num;
                            }
                    }
            }
            function setLimit($Num)
            {
                    if ($Num > 0 && (int)$Num == $Num)
                            $this->limit_Num = $Num;
            }        function setTotalRecordNum($Num)
            {
                    if ($Num > 0 && (int)$Num == $Num)
                            $this->total_RecordNum = $Num;
            }        function setCurrPageNum($Num)
            {
                    if ($Num > 0 && (int)$Num == $Num)
                            $this->curr_PageNum = $Num;
            }        function setQueryString($string)
            {
                    $this->queryString = $string;
            }        function getStartRecord()
            {
                    return $this->startRecord;
            }        function getEndRecord()
            {
                    return $this->endRecord;
            }        function getCurrPageNum()
            {
                    return $this->curr_PageNum;
            }        function getThisPageRecords()
            {
                    return $this->thisPageRecords;
            }        function getTotalPages()
            {
                    return $this->total_Pages;
            }
            function getLimit()
            {
                    return $this->limit_Num;
            }
            function getTotalRecordNum()
            {
                    return $this->total_RecordNum;
            }
            function getQueryString()
            {
                    return $this->queryString;
            }
            function listPages()
            {
                    for($i =1; $i<=$this->getTotalPages(); $i++)
                    {
                            if($i == $this->getCurrPageNum())
                                    echo "<font color=\"#FF0000\"><strong>".$i."</strong></font> ";
                            else
                                    echo "<a href=\"".$_SERVER[PHP_SELF]."?pageNum=".$i."&".$this->getQueryString()."\">$i</a> ";
                    }
            }        function pageText()
            {
                    echo "&sup1;&sup2;".$this->getTotalRecordNum()."&Igrave;&otilde;&frac14;&Ccedil;&Acirc;&frac14;&pound;&not;·&Ouml;".$this->getTotalPages()."&Ograve;&sup3;&pound;&not;&Atilde;&iquest;&Ograve;&sup3;&Iuml;&Ocirc;&Ecirc;&frac34;".$this->getLimit()."&Igrave;&otilde;&frac14;&Ccedil;&Acirc;&frac14; &Auml;&iquest;&Ccedil;°&Icirc;&ordf;&micro;&Uacute;".$this->getCurrPageNum()."&Ograve;&sup3;";
            }        function PreviousNext()
            {
                    if($this->getCurrPageNum()>1)
                            echo "<a href=\"".$_SERVER[PHP_SELF]."?pageNum=1&".$this->getQueryString()."\">&Ecirc;×&Ograve;&sup3;</a> ";
                    else
                            echo "&Ecirc;×&Ograve;&sup3; ";                if($this->getCurrPageNum()>1)
                            echo "<a href=\"".$_SERVER[PHP_SELF]."?pageNum=".($this->getCurrPageNum()-1)."&".$this->getQueryString()."\">&Eacute;&Iuml;&Ograve;&raquo;&Ograve;&sup3;</a> ";
                    else
                            echo "&Eacute;&Iuml;&Ograve;&raquo;&Ograve;&sup3; ";                if($this->getCurrPageNum()<$this->getTotalPages())
                            echo "<a href=\"".$_SERVER[PHP_SELF]."?pageNum=".($this->getCurrPageNum()+1)."&".$this->getQueryString()."\">&Iuml;&Acirc;&Ograve;&raquo;&Ograve;&sup3;</a> ";
                    else
                            echo "&Iuml;&Acirc;&Ograve;&raquo;&Ograve;&sup3; ";                if($this->getCurrPageNum()<$this->getTotalPages())
                            echo "<a href=\"".$_SERVER[PHP_SELF]."?pageNum=".$this->getTotalPages()."&".$this->getQueryString()."\">&Icirc;&sup2;&Ograve;&sup3;</a> ";
                    else
                            echo "&Icirc;&sup2;&Ograve;&sup3; ";        }        function goToPage()
            {
                    echo "<form name=\"gotopage\" method=\"post\" action=\"".$_SERVER[PHP_SELF]."?".$this->getQueryString()."\">
      ×&ordf;&micro;&frac12;&micro;&Uacute;
      <input name=\"pageNum\" type=\"text\" id=\"pageNum\" size=\"3\" value=\"".$this->getCurrPageNum()."\">
      &Ograve;&sup3; <a href=\"javascript:gotopage.submit()\">go</a>
    </form>";
            }
    }/*
    //example
    $page = new page(90, 3);
    $page->setQueryString("aa=$aa&b=$b&c=$c");
    $page->listPages();
    echo "<hr>";
    $page->PreviousNext();
    echo "<hr>";
    $page->pageText();
    echo "<hr>";
    $page->goToPage();
    */?>
      

  4.   

    syre(神仙) 上面一贴,明白了。用类是从扩展性上考虑的是吧。
    那句“用函数,你不能方便的设置参数的默认值”不敢苟同。函数也可以,在function a($var=a)这里就可以设置。
      

  5.   

    最近正想写个结合smarty的分页类
      

  6.   

    没所谓了。扣分有什么大惊小怪的。谁让我兴趣广泛呢。
    下山容易上山难,就是这个道理。我上网这么久,尤其上论坛,被封过几百次都不止了,所幸还有26个弟兄轮流用。
    楼上恭喜啊,四颗星了,你的星和信誉成正比的,我的正好反着。
    你的话虽然不是狠明白,不过大体了解了。 feixuehenshui(飞雪恨水) ,等你好消息啊,正好想找个这样的类用。