gusucode.com > 同城苏州黄页系统php源码程序 > lib/pageIdx.class.php

    <?
/**********
| +---------------------------------------------------
|    CLASS NAME: PageBar
| +---------------------------------------------------
|    Author:    Arvan [E-mail:Arvan@5n9.com QQ:8817776]
|    Create date:    2003-7-17
|    Note:
|        Do for pagination
| +---------------------------------------------------
|    Warning: no...
| +---------------------------------------------------
**********/

class pageIdx
{
        var $total;
        var $onepage;
        var $num;
        var $page;
        var $total_page;
        var $offset;
        var $linkhead;
        var $non_link_color;//無連接的顔色CSS名
        var $link_color;//有連接
        var $curr_page_color;//當前页

        function pageIdx($total, $onepage,$non_link_color='', $link_color='',$curr_page_color='', $form_vars='')
        {
                $this->non_link_color = $non_link_color;
                $this->link_color     = $link_color;
                $this->curr_page_color=$curr_page_color;

                $page = @$_GET['page'];
                $this->total      = $total;
                $this->onepage    = $onepage;
                $this->total_page = ceil($total/$onepage);

                if (empty($page))
                {
                        $this->page = 1;
                        $this->offset    = 0;
                }
                else
                {
                        $this->page = $page;
                        $this->offset    = ($page-1)*$onepage;
                }

                if (!empty($form_vars))
                {

                        $vars = explode("|", $form_vars);
                        $chk = $vars[0];
                        $chk_value = $_POST["$chk"];
                        if (empty($chk_value))
                        {
                                $formlink = "";
                        }
                        else
                        {
                                for ($i=0; $i<sizeof($vars); $i++)
                                {
                                        $var     = $vars[$i];
                                        $value   = $_POST["$var"];
                                        $addchar = $var."=".$value;

                                        $formlink = $formlink.$addchar."&";
                                }
                        }
                }
                else
                {
                        $formlink = "";
                }
				//echo $_SERVER['QUERY_STRING']."<br>";//exit;
                $linkarr = explode("page=", $_SERVER['QUERY_STRING']);
                $linkft  = $linkarr[0];
				//echo $formlink.":".$linkft.":".$_SERVER['QUERY_STRING'];exit;

                if (empty($linkft))
                {
                        $this->linkhead = $_SERVER['PHP_SELF']."?".$formlink;
                }
                else
                {
                        $linkft    = (substr($linkft, -1)=="&")?$linkft:$linkft."&";
                        $this->linkhead = $_SERVER['PHP_SELF']."?".$linkft.$formlink;
                }

        }
        #End function PageBar();

        function offset()
        {
                return $this->offset;
        }
        #End function offset();

        function pre_page($char='')
        {
                $linkhead  = $this->linkhead;
                $page = $this->page;
				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;
				$currpage  = $this->curr_page_color;
                if (empty($char))
                {
                        $char = "<";
                }

                if ($page>1)
                {
                        $pre_page = $page - 1;
                        return "[<a href=\"$linkhead"."page=$pre_page\" style=\"$havelink\">$char</a>]";
                }
                else
                {
                        return "[<span style=\"$nolink\">$char</span>]";
                }

        }
        #End function pre_page();

        function next_page($char='')
        {
                $linkhead   = $this->linkhead;
                $total_page = $this->total_page;
                $page  = $this->page;

				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;

                if (empty($char))
                {
                        $char = ">";
                }
                if ($page<$total_page)
                {
                        $next_page = $page + 1;
                        return "[<a href=\"$linkhead"."page=$next_page\" style=\"$havelink\">$char</a>]";
                }
                else
                {
                        return "[<span style=\"$nolink\">$char</span>]";
                }
        }
        #End function next_page();

        function num_bar($num='', $left='', $right='')
        {
                $num       = (empty($num))?10:$num;
                $this->num = $num;
                $mid       = floor($num/2);
                $last      = $num - 1;
                $page = $this->page;
                $totalpage = $this->total_page;
                $linkhead  = $this->linkhead;
                $left      = (empty($left))?"[":$left;
                $right     = (empty($right))?"]":$right;
                //$color     = (empty($color))?"#ff0000":$color;
                $style     = $this->curr_page_color;//by eyes
                $minpage   = (($page-$mid)<1)?1:($page-$mid);
                $maxpage   = $minpage + $last;
                if ($maxpage>$totalpage)
                {
                        $maxpage = $totalpage;
                        $minpage = $maxpage - $last;
                        $minpage = ($minpage<1)?1:$minpage;
                }

                for ($i=$minpage; $i<=$maxpage; $i++)
                {
                        $char = $i;
                        if ($i==$page)
                        {
                                $char = "$left<span style=\"$style\">$char</span>$right";
								$linkchar = $char;
                        }
                        else
                        {
                        	$linkchar = $left."<a href='$linkhead"."page=$i'>".$char."</a>".$right;
                        }

                        @$linkbar  .= $linkchar;
                }

                return @$linkbar;
        }
        #End function num_bar();

        function pre_group($char='')
        {
				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;

                $page   = $this->page;
                //$onepage     = $this->onepage;
                $linkhead    = $this->linkhead;
                $num             = $this->num;
                $mid         = floor($num/2);
                $minpage     = (($page-$mid)<1)?1:($page-$mid);
                $char        = (empty($char))?"上10页":$char;
                $pgpage = ($minpage>$num)?$minpage-$mid:1;
                if ($page>6)
                {
	                return "[<a href='$linkhead"."page=$pgpage' style=\"$havelink\">".$char."</a>]";
                }
                else
                {
                	return "[<span style=\"$nolink\">$char</span>]";
                }

        }
        #End function pre_group();

        function next_group($char='')
        {
				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;

                $page = $this->page;
                $linkhead  = $this->linkhead;
                $totalpage = $this->total_page;
                $num           = $this->num;
                $mid       = floor($num/2);
                $last      = $num;
                $minpage   = (($page-$mid)<1)?1:($page-$mid);
                $maxpage   = $minpage + $last;
                if ($maxpage>$totalpage)
                {
                        $maxpage = $totalpage;
                        $minpage = $maxpage - $last;
                        $minpage = ($minpage<1)?1:$minpage;
                }

                $char = (empty($char))?"下10页":$char;
                $ngpage = ($totalpage>$maxpage+$last)?$maxpage + $mid:$totalpage;

                if (($page+4)<$totalpage)
                {
	                return "[<a href='$linkhead"."page=$ngpage' style=\"$havelink\">".$char."</a>]";
                }
                else
                {
                	return "[<span style=\"$nolink\">$char</span>]";
                }

        }
        #End function next_group();

        /*
        first page
        add by eyes 2004.12.19
        當只有總页數爲1時,不顯示
        */
        function first_page($char='')
        {
				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;

                $linkhead  = $this->linkhead;
                //$page = $this->page;
                $totalpage = $this->total_page;
                if (empty($char))
                {
                        $char = "上一页";
                }

                if ($totalpage>1)
                {
                        $first_page = 1;
                        return "[<a href=\"$linkhead"."page=$first_page\" style=\"$havelink\">$char</a>]";
                }
                else
                {
                        return "[<span style=\"$nolink\">$char</span>]";
                }

        }//end first_page

        /*
        last page
        add by eyes 2004.12.19
        當只有總页數爲1時,不顯示
        */
        function last_page($char='')
        {
				$nolink    = $this->non_link_color;
				$havelink  = $this->link_color;

                $linkhead  = $this->linkhead;
                //$page = $this->page;
                $totalpage = $this->total_page;
                if (empty($char))
                {
                        $char = "最后一页";
                }

                if ($totalpage>1)
                {
                        //$first_page = 1;
                        return "[<a href=\"$linkhead"."page=$totalpage\" style=\"$havelink\">$char</a>]";
                }
                else
                {
                        return "[<span style=\"$nolink\">$char</span>]";
                }

        }//end last_page





    function whole_num_bar($num='', $color='')
    {
        $num_bar    = $this->num_bar($num, $color);
        $pre_group  = $this->pre_group();
        $pre_page   = $this->pre_page();
        $next_page  = $this->next_page();
        $next_group = $this->next_group();
        $first_page = $this->first_page();
        $last_page  = $this->last_page();

            return  $first_page.$pre_group.$pre_page.$num_bar.$next_page.$next_group.$last_page;
    }
        #End function whole_bar();

        function Jump_form()
        {
                $linkhead = $this->linkhead;
                $total          = $this->total_page;
                $form = <<<EOT
<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <script language="javascript">
                function jump(linkhead, total, page)
                {
                        var page = (page.value>total)?total:page.value;
                        var page = (page<1)?1:page;
                        location.href = linkhead + "page=" + page;
                        return false;

                }
        </script>
       <form name="pagebarjumpform" method="post" onSubmit="return jump('$linkhead', $total, this.form.page)"><tr>
          <td>
        <input name="page" type="text" size="1">
                <input type="button" name="Submit" value="GO" onClick="return jump('$linkhead', $total, this.form.page)">
      </td>
        </tr></form></table>
EOT;

        return $form;
        }
        #End function Jump_form();

}
#End class PageBar;


/*****
        //example

$total = 1000;
$onepage = 20;

$pb       = new PageBar($total, $onepage);
$offset   = $pb->offset();
$pagebar  = $pb->whole_num_bar();
$jumpform = $pb->Jump_form();
echo $offset."<br>".$pagebar."<br>".$jumpform;

return:
   0
[<<] [1][2][3][4][5][6][7][8][9][10][>][>>]

------------------
一些說明:
這個類並不是完美的,請其他高手指出其中的不足
$this->offset() 不是沒有用的,它返回的是mysql query的指針,如果你用于mysql查詢,它就是limit $offset //$offste = $this->offset();
$form_vars 是指表單變量 //這個還沒有應用,剛才經過測試,確認已經無錯,表單變量名用"|"隔開,如"action|searchkey"
$char / /你自己設定的上一页,下一页,上一組,下一組的字符,有默認值, 可以不設
$color //當前页的突顯顔色,也有默認值
你可以把上一页,下一页等單獨輸出,也可以結合數字條一塊輸出,當然還可以加上表單跳轉.
功能還是有所欠缺的,不過其他的我是懶得加了。
*****/

?>