zoukankan      html  css  js  c++  java
  • 一个简单的php分页逻辑

    php分页

    <?php
    include 'backend/conn.php';
    
    $html = '<ul>';     //输出的html
    $pageDataNum=3;    //每页显示10行
    $pageCount=0;   //页数 
    $currentIndex=1;//当前第几页
    $dataNum = 0;  //数据条数
    $curentDataIndex = 0; //当前第几行数据
    $pre = 0;             //上一页页数
    $nex = 0;              //下一页页数
    $preImageName = '';      //是否到页码开始的尽头
    $nexImageName = '';      //是否到页码结尾的尽头
    
    $page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
    $page = daddslashes($page);
    
    $sql = "select count(1) as countNum from GsSpecialArticle";
    $result = mysql_query($sql) or die(mysql_error());
    if($row = mysql_fetch_object($result)){
        $dataNum=$row->countNum;
    }
    $pageCount = ceil($dataNum/$pageDataNum);
    $curentDataIndex = ($page-1) * $pageDataNum;
    $curentDataIndex = $curentDataIndex > $dataNum ? ($curentDataIndex-$pageDataNum) : $curentDataIndex;
    
    $sql = "select * from GsSpecialArticle order by CreateTime desc limit ".$curentDataIndex.','.$pageDataNum.';';
    $result = mysql_query($sql) or die(mysql_error());
    while($row = mysql_fetch_object($result)){
        $html.='<li>'.substr($row->CreateTime,0,10).'</span>'.'<span class="title"><a href="01maincontent.html?id=14&subId=17&specialId='.$row->Id.'">'.$row->ArticleTitle.'</span></a></li>';
    }
    
    $pre = $page - 1;
    $pre = $pre < 1 ? 1 : $pre;
    $nex = $page + 1;
    $nex = $nex > $pageCount ? $pageCount : $nex;
    
    
    $html .= '</ul><ul id="pages">';
    
    if($page == 1){
        $html .= '<li><span><image alt="" src="images/page_frist1.png"></image></span>
        <span><image alt="" src="images/page_prev1.png"></image></span></span>';
        for($i=1;$i<=$pageCount;$i++){
            if(abs($i-$page)<7){
                if($i == $page){
                    $html .= '<span style="color:red;">'.$i.'</span>';
                }
                else{
                    $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
                }
            }
        }
        $html .= '<span><a href="#anchor" onclick="ChangePage('.$nex.')"><image alt="" src="images/page_next.png"></image></a></span>
        <span><a href="#anchor" onclick="ChangePage('.$pageCount.')"><image alt="" src="images/page_last.png"></image></a></span></li>';
    }
    else if($page == $pageCount){
        //echo $page.'</br>';
        //echo $pageCount.'</br>';
        $html .= '<li><span><a href="#anchor" onclick="ChangePage(1)"><image alt="" src="images/page_frist.png"></image></a></span>
        <span><a href="#anchor" onclick="ChangePage('.$pre.')"><image alt="" src="images/page_prev'.$preImageName.'.png"></image></a></span>';
        for($i=1;$i<=$pageCount;$i++){
            if(abs($i-$page)<7){
                if($i == $page){
                    $html .= '<span style="color:red;">'.$i.'</span>';
                }
                else{
                    $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
                }
            }
        }    
        $html .= '<span><image alt="" src="images/page_next1.png"></image></span><span><image alt="" src="images/page_last1.png"></image></span></li>';
    }
    else{
        $html .= '<li><span><a href="#anchor" onclick="ChangePage(1)"><image alt="" src="images/page_frist.png"></image></a></span>
        <span><a href="#anchor" onclick="ChangePage('.$pre.')"><image alt="" src="images/page_prev.png"></image></a></span>';
        for($i=1; $i<=$pageCount ; $i++){
            if(abs($i-$page)<4){
                if($i == $page){
                    $html .= '<span style="color:red;">'.$i.'</span>';
                }
                else{
                    $html .= '<span><a href="#anchor" onclick="ChangePage('.$i.')">'.$i.'</image></a></span>';
                }
            }
        }
        $html .= '<span><a href="#anchor" onclick="ChangePage('.$nex.')"><image alt="" src="images/page_next.png"></image></a></span>
        <span><a href="#anchor" onclick="ChangePage('.$pageCount.')"><image alt="" src="images/page_last.png"></image></a></span></li>';
    }
    
    $html .= '</ul>';
    
    echo $html;
    ?>
    function ChangePage(page){
        window.location.href = "01maincontent.html?id=14&subId=17&page="+page;
    }
  • 相关阅读:
    第三章 传奇的开始--Delphi(附读书笔记)
    南沙才是根本,进军西太平洋就是一个伪命题
    Qt之自定义插件(for Qt Designer)
    人类本来就是在无奈中前进的
    亚投行国家分工非常明确,一路一带是欧亚大融合之路,欢呼吧!
    冒泡排序
    webkit中DOM 事件有多少
    在TMemo上画一条线(超级简单,举一反三)
    判断系统64位(使用GetNativeSystemInfo函数,XP时代就有这个函数了)
    项目的大小衡量标准,以及项目演进的方法(填空架子,持续集成,边开发边测试效果)
  • 原文地址:https://www.cnblogs.com/tylerdonet/p/3643996.html
Copyright © 2011-2022 走看看