Home > Backend Development > PHP Tutorial > php分页种代码

php分页种代码

WBOY
Release: 2016-06-13 13:04:45
Original
1066 people have browsed it

php分页类代码

<?php /**
 * 简单分页类
 *
 */
class Pagination
{

	/**
	 * 分页显示样式一
	 *
	 * @param int $allItemTotal 所有记录数量
	 * @param int $currPageNum 当前页数量
	 * @param int $pageSize  每页需要显示记录的数量
	 * @param string $pageName  当前页面的地址, 如果为空则由系统自动获取,缺省为空
	 * @param array $getParamList  页面中需要传递的URL参数数组, 数组中key代表变量民,value代表变量值
	 * @return string  返回最后解析出分页HTML代码, 可以直接使用
	 * @example 
	 * 	echo cps_split_page(100, 2, 10, 'page.php', array('uid'=>1001, 'gid'=>2008));
	 * 	
	 *  输出: [上一页]  1>10 [下一页]
	 */
	public static function split1($allItemTotal, $currPageNum, $pageSize, $pageName='',  $getParamList = array()){
		if ($allItemTotal == 0) return "";
	
		//页面名称
		if ($pageName==''){
			$url = $_SERVER['PHP_SELF']."?page=";
		} else {
			$url = $pageName."?page=";
		}
		
		//参数
		$urlParamStr = "";
		foreach ($getParamList as $key => $val) {
			$urlParamStr .= "&". $key ."=". $val;
		}
		//计算总页数
		$pagesNum = ceil($allItemTotal/$pageSize);
		
		//第一页显示
		$firstPage = ($currPageNum <<" : "<a href=". $url ."1". $urlParamStr ." title='第1页'>1<<</a>";
		
		//最后一页显示
		$lastPage = ($currPageNum >= $pagesNum)? ">>". $currPageNum : "<a href=".%20%24url%20.%20%24pagesNum%20.%20%24urlParamStr." title='第". $pagesNum ."页'>>>". $pagesNum ."</a>";
		
		//上一页显示
		$prePage  = ($currPageNum [上一页]";
		
		//下一页显示
		$nextPage = ($currPageNum >= $pagesNum) ? "下页" : "<a href=".%20%24url%20.%20(%24currPageNum+1)%20.%20%24urlParamStr%20." title="下一页">[下一页]</a>";
		
		//按页显示
		$listNums = "";
		for ($i=($currPageNum-4); $i $pagesNum) continue;
			if ($i == $currPageNum) $listNums.= "[".$i."] ";
			else $listNums.= " <a href=".%20%24url%20.%20%24i%20.%20%24urlParamStr%20." title='第". $i ."页'>[". $i ."]</a> ";
		}
		
		$returnUrl = $prePage ."  ". $firstPage ." ". $listNums ." ". $lastPage ." ". $nextPage;
		
		return $returnUrl;
	}


	/**
	 * 分页显示样式二
	 * 
	 * @param int $allItemTotal 所有记录数量
	 * @param int $currPageNum 当前页数量
	 * @param int $pageSize  每页需要显示记录的数量
	 * @param string $pageName  当前页面的地址, 如果为空则由系统自动获取,缺省为空
	 * @param array $getParamList  页面中需要传递的URL参数数组, 数组中key代表变量民,value代表变量值
	 * @return string  返回最后解析出分页HTML代码, 可以直接使用
	 * @example 
	 *   echo pageStyle1(50, 2, 10, 's.php', array('id'=>1, 'name'=>'user'));
	 *
	 *   输出:上一页   1  2  3  4  5   下一页   [2] [GO]
	 */
	public static function split2($allItemTotal, $currPageNum, $pageSize, $pageName='',  $getParamList = array()){
		if ($allItemTotal == 0) return "";
	
		//页面名称
		if ($pageName==''){
			$url = $_SERVER['PHP_SELF']."?page=";
		} else {
			$url = $pageName."?page=";
		}
		
		//参数
		$urlParamStr = "";
		foreach ($getParamList as $key => $val) {
			$urlParamStr .= "&". $key ."=". $val;
		}
		//计算总页数
		$pagesNum = ceil($allItemTotal/$pageSize);
		
		//上一页显示
		$prePage  = ($currPageNum 上一页";
		
		//下一页显示
		$nextPage = ($currPageNum >= $pagesNum) ? "下一页" : "<a href=".%20%24url%20.%20(%24currPageNum+1)%20.%20%24urlParamStr%20." title="下一页">下一页</a>";
		
		//按页显示
		$listNums = "";
		for ($i=($currPageNum-4); $i $pagesNum) continue;
			if ($i == $currPageNum) $listNums.= " ".$i." ";
			else $listNums.= " <a href=".%20%24url%20.%20%24i%20.%20%24urlParamStr%20." title='第". $i ."页'>". $i ."</a> ";
		}
		
		$returnUrl = $prePage ."  ". $listNums ."  ". $nextPage;
		$gotoForm = '&nbsp  <input type="text" size="2" id="page_input" value="'. $currPageNum .'"><input type="button" value="Go" onclick="location.href=\''. $url .'\'+document.getElementById(\'page_input\').value+\''. $urlParamStr .'\'">';
		
		return $returnUrl . $gotoForm;
	}


	/**
	 * 分页显示样式三
	 * 
	 * @param int $allItemTotal 所有记录数量
	 * @param int $currPageNum 当前页数量
	 * @param int $pageSize  每页需要显示记录的数量
	 * @param string $pageName  当前页面的地址, 如果为空则由系统自动获取,缺省为空
	 * @param array $getParamList  页面中需要传递的URL参数数组, 数组中key代表变量民,value代表变量值
	 * @return string  返回最后解析出分页HTML代码, 可以直接使用
	 * @example 
	 *   echo pageStyle1(50, 2, 10, 's.php', array('id'=>1, 'name'=>'user'));
	 *
	 *   输出:上一页  下一页
	 */	
	public static function split3($allItemTotal, $currPageNum, $pageSize, $pageName='',  $getParamList = array()){
		if ($allItemTotal == 0) return "";
	
		//页面名称
		if ($pageName==''){
			$url = $_SERVER['PHP_SELF']."?page=";
		} else {
			$url = $pageName."?page=";
		}
		
		//参数
		$urlParamStr = "";
		foreach ($getParamList as $key => $val) {
			$urlParamStr .= "&". $key ."=". $val;
		}
		//计算总页数
		$pagesNum = ceil($allItemTotal/$pageSize);
		
		//上一页显示
		$prePage  = ($currPageNum 上一页";
		
		//下一页显示
		$nextPage = ($currPageNum >= $pagesNum) ? "下一页" : "<a href=".%20%24url%20.%20(%24currPageNum+1)%20.%20%24urlParamStr%20." title="下一页">下一页</a>";
		
		$returnUrl = $prePage ."  ". $nextPage;		
		return $returnUrl;
	}


    /**
     * 分页显示4
     * 
     * @param int $allItemTotal 所有记录数量
     * @param int $currPageNum 当前页数量
     * @param int $pageSize  每页需要显示记录的数量
     * @param string $pageName  当前页面的地址, 如果为空则由系统自动获取,缺省为空
     * @param array $getParamList  页面中需要传递的URL参数数组, 数组中key代表变量民,value代表变量值
     * @return string  返回最后解析出分页HTML代码, 可以直接使用
     * @example 
     *   echo pageStyle1(50, 2, 10, 's.php', array('id'=>1, 'name'=>'user'));
     *
     *   输出:第2/50页 上一页 1 2 3 4 5 下一页  跳到 [  ] 页 [GO]
     */
     public static function split4($allItemTotal, $currPageNum, $pageSize, $pageName='',  $getParamList = array()){
        if ($allItemTotal == 0) return "";
    
        //页面名称
        if ($pageName==''){
            $url = $_SERVER['PHP_SELF']."?page=";
			$formUrl = $_SERVER['PHP_SELF']."?";
        } else {
            $url = $pageName."?page=";
			$formUrl = $pageName."?";
        }
        
        //参数
        $urlParamStr = "";
        foreach ($getParamList as $key => $val) {
            $urlParamStr .= "&". $key ."=". $val;
        }
		$formUrl .= $urlParamStr ."&page=";

        //计算总页数
        $pagesNum = ceil($allItemTotal/$pageSize);
        
        //上一页显示
        $prePage  = ($currPageNum 上一页";
        
        //下一页显示
        $nextPage = ($currPageNum >= $pagesNum) ? "下一页" : "<a href="%22.%20%24url%20.%20(%24currPageNum+1)%20.%20%24urlParamStr%20.%22" title="下一页" class="page_next">下一页</a>";
        
        //按页显示
        $listNums = "<select name="page_select" id="page_select">\n";
        for ($i=1; $i $pagesNum) continue;
            if ($i == $currPageNum) $listNums .= "<option selected>{$i}</option>\n";
            else $listNums .= "<option>{$i}</option>\n";
        }
        $listNums .= "</select>\n";
        
        $returnUrl =  $prePage .' '. $nextPage . ' 共有'.$pagesNum.'页  跳到 '.$listNums ." 页 ";
        $script =
        function _pageSelect(url){
            var o = document.getElementById("page_select");
            var v = o.options[o.selectedIndex].text;
            window.location.replace(url+v);
        }            
        
            
EOF;
        $gotoForm = ' <a href="javascript:_pageSelect(%5C''.%24formUrl.'%5C');" onclick="//_pageSelect(\''.$formUrl.'\')">GO</a>';
        
        return $script . $returnUrl . $gotoForm;
    }


    /**
     * 分页显示5
     * 
     * @param int $allItemTotal 所有记录数量
     * @param int $currPageNum 当前页数量
     * @param int $pageSize  每页需要显示记录的数量
     * @param string $pageName  当前页面的地址, 如果为空则由系统自动获取,缺省为空
     * @param array $getParamList  页面中需要传递的URL参数数组, 数组中key代表变量民,value代表变量值
     * @return string  返回最后解析出分页HTML代码, 可以直接使用
     * @example 
     *   echo pageStyle1(50, 2, 10, 's.php', array('id'=>1, 'name'=>'user'));
     *
     *   输出:第2/50页 上一页 1 2 3 4 5 下一页  跳到 [  ] 页 [GO]
     */
     public static function split5($allItemTotal, $currPageNum, $pageSize, $pageName='',  $getParamList = array()){
        if ($allItemTotal == 0) return "";
    
        //页面名称
        if ($pageName==''){
            $url = $_SERVER['PHP_SELF']."?page=";
			$formUrl = $_SERVER['PHP_SELF']."?";
        } else {
            $url = $pageName."?page=";
			$formUrl = $pageName."?";
        }
        
        //参数
        $urlParamStr = "";
        foreach ($getParamList as $key => $val) {
            $urlParamStr .= "&". $key ."=". $val;
        }
		$formUrl .= $urlParamStr ."&page=";

        //计算总页数
        $pagesNum = ceil($allItemTotal/$pageSize);
        
        //上一页显示
        $prePage  = ($currPageNum 上一页";
        
        //下一页显示
        $nextPage = ($currPageNum >= $pagesNum) ? "下一页" : "<a href="%22.%20%24url%20.%20(%24currPageNum+1)%20.%20%24urlParamStr%20.%22" title="下一页" class="page_next">下一页</a>";
        
        //按页显示
        $listNums = "";
        for ($i=($currPageNum-1); $i $pagesNum) continue;
            if ($i == $currPageNum) $listNums.= " <span class="page_cur">".$i."</span>";
            else $listNums.= " <a href="%22.%20%24url%20.%20%24i%20.%20%24urlParamStr%20.%22" title='第". $i ."页' class="page_other">". $i ."</a>";
        }
        
        $returnUrl = '<span class="page_text">第'.$currPageNum.'/'.$pagesNum.'页</span> '. $prePage ." ". $listNums ." ". $nextPage;
        $gotoForm = ' <span class="page_jump">跳到 <input type="text" class="page_enter" style="width:20px;" id="page_input" value="'. $currPageNum .'"> 页 <input type="button" value="Go" class="page_submit" onclick="location.href=\''. $url .'\'+document.getElementById(\'page_input\').value+\''. $urlParamStr .'\'">';
        
        return $returnUrl . $gotoForm;
    }


}

/*
例子:
$allItemTotal = 1000;
$currPageNum = isset($_GET['page']) ? $_GET['page'] : 1;
$pageSize = 5;
$pageName='';
$getParamList = array('classid'=>44);

$pagination = new Pagination();
echo $pagination->split1($allItemTotal, $currPageNum, $pageSize, $pageName, $getParamList)."<br>";
echo $pagination->split2($allItemTotal, $currPageNum, $pageSize, $pageName, $getParamList)."<br>";
echo $pagination->split3($allItemTotal, $currPageNum, $pageSize, $pageName, $getParamList)."<br>";
echo $pagination->split4($allItemTotal, $currPageNum, $pageSize, $pageName, $getParamList)."<br>";
echo $pagination->split5($allItemTotal, $currPageNum, $pageSize, $pageName, $getParamList)."<br>";
*/
?></span>
Copy after login
Related labels:
gt
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template