欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP简单分页的实现

程序员文章站 2022-06-17 15:11:01
...
程序的关键点是页码的获取

";    $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"."页
"; // $pageNow=($pageNow-1)*$every; echo "
"; // echo "

测试信息表 添加

"; // echo "

$pageNow"."页

"; echo ""; for ($i=0; $i ".$field[$i]->name.""; } echo ""; //显示分页 $sql="select * from $table limit $start,$every;"; // echo $sql; $newRest = $conn->query($sql); while($row = $newRest->fetch_row()){ echo ""; for ($j=0; $j ".$row[$j].""; } echo ""; } echo "
"; echo "
"; echo "
"; echo "$pageNow/$allpage"; echo "首页"; if($pageNow==1){ echo "上一页"; }else{ $pagePre=$pageNow-1; echo "上一页"; } // echo "上一页"; // echo "下一页"; if($pageNow==$allpage){ echo "下一页"; }else{ $pageNext=$pageNow+1; // echo "

$pageNow

"; echo "下一页"; } echo "尾页"; echo "
"; echo ""; echo "
"; echo "
";$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;}