Home >Backend Development >PHP Tutorial >PHP简单分页的实现

PHP简单分页的实现

WBOY
WBOYOriginal
2016-06-20 12:30:45990browse

程序的关键点是页码的获取

<?php    function pagef($every,$table){    include('../conf/config.php');    echo "<link rel='stylesheet' type='text/css' href='../css/testpg.css'>";    $url=$_SERVER["PHP_SELF"];    $pageNow =isset($_GET['pageNow'])?$_GET['pageNow']:1;    $start=($pageNow-1)*$every;    $allsql = "select * from {$table};";    // die();    // $sql = "select * from test;";    $rest = $conn->query($allsql);    $field = $rest->fetch_fields();    $fieldNum = count($field);   //字段数量    //获取数据量    $rowNum = $rest->num_rows;    // echo "记录条数:".$rowNum;    $allpage=ceil($rowNum/$every);    // echo "----每页分$every"."条时有$allpage"."页<br/>";    // $pageNow=($pageNow-1)*$every;    echo "<table class='table' border=1 cellspacing=0>";          // echo "<h3 class='info'>测试信息表   <a href='addCompany.html' class='link'>添加</a></h3>";                // echo "<h1>$pageNow"."页</h1>";                echo "<tr>";                for ($i=0; $i < $fieldNum; $i++) {                    echo "<td class='td'>".$field[$i]->name."</td>";                }          echo "</tr>";          //显示分页          $sql="select * from $table limit $start,$every;";          // echo $sql;          $newRest = $conn->query($sql);          while($row = $newRest->fetch_row()){               echo "<tr>";               for ($j=0; $j <$fieldNum ; $j++) {                   echo "<td class='td' id='td'>".$row[$j]."</td>";               }               echo "</tr>";          }    echo "</table>";    echo "<div id='divclass'>";    echo "<div id='center'>";    echo "<span class='fy'>$pageNow/$allpage</span>";    echo "<a href='{$url}?pageNow=1' class='fy'>首页</a>";    if($pageNow==1){         echo "<a href='{$url}?pageNow=1' class='fy'>上一页</a>";    }else{        $pagePre=$pageNow-1;        echo "<a href='{$url}?pageNow={$pagePre}' class='fy'>上一页</a>";    }    // echo "<a href='{$url}?pageNow={$pageNow-1}' class='fy'>上一页</a>";    // echo "<a href='{$url}?pageNow={$pageNow+1}' class='fy'>下一页</a>";    if($pageNow==$allpage){       echo "<a href='{$url}?pageNow={$allpage}' class='fy'>下一页</a>";    }else{       $pageNext=$pageNow+1;      //  echo "<h1>$pageNow</h1>";       echo "<a href='{$url}?pageNow={$pageNext}' class='fy'>下一页</a>";    }    echo "<a href='{$url}?pageNow={$allpage}' class='fy' >尾页</a>";    echo "<form action='{$url}' method='get' >";    echo "<select name='pageNow' class='option'>";    echo "<option value='1'>选择分页</option>";          for($i=1;$i<=$allpage;$i++){              echo "<option value='{$i}'>$i</option>";          }    echo "<input type='submit' value='跳转' class='button'>";    echo "</div>";  echo "</div>";$conn->close();}//实际使用一个测试数据表echo pagef(10,'test');?>

//css文件

*{padding: 0px;  margin: 0px;}.center{text-align: left;        color: red;        text-indent: 500px;}#info{color: DodgerBlue;}.table{       margin-left: auto;       margin-right: auto;       width: auto;       text-align: center;       color: royalblue;       border: 0;       }.td{width: 150px;    /*text-indent: 10px;*/    text-align: left;    font-size: 14px;}#td{color: #555;}tr:hover{background-color: DodgerBlue;}.info{text-align: center;      color: red;}.link{color: blue;      text-decoration: none;      text-align: center;      font-size: 12px;}.h3{text-align: center;    }#divclass{width: 100%;          height: 30px;          margin-top: 10px;          font-size: 18px;          /*background: coral;*/          }#center{width: 27%;        /*display: inline-block;*/        height: 30px;        margin-left: auto;        margin-right: auto;        /*background: orange;*/      }.fy{    /*background: DodgerBlue;*/    height: 30px;    line-height: 30px;    display: inline-block;    float: left;    color: DodgerBlue;    padding-left: 5px;    text-decoration: none;    text-align: center;   }.option{width: 80px;        height: 30px;        background: white;        float: left;        border: 2px solid DodgerBlue;        display: inline-block;}.button{width: 56px;        float: left;        color: white;        background: DodgerBlue;        height: 30px;        border: 0px;        border-radius: 0px;        display: inline-block;}


Statement:
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