Home >Backend Development >PHP Tutorial >php limit page turning (pagination) code
PHP limit page turning code, friends in need can refer to it.
The database operation part is omitted and only the paging code is written. $display=10;//每页记录数 if (isset($_GET["p"])) { $num_pages=$_GET["p"];//当前页码 }else{ $sql="select count(*) from f_user"; $rs=@mysql_query($sql) or die(mysql_error()); $row=mysql_fetch_array($rs); $num_records=$row[0];//记录总数 if($num_records>$display) { $num_pages=ceil($num_records/$display); }else{ $num_pages=1; } } if (isset($_GET["s"])) { $start=$_GET["s"]; }else{ $start=0; } $sql="select * from f_user order by id asc limit $start, $display"; $rs=@mysql_query($sql) or die(mysql_error()); echo '<table>'; echo '<tr>'; echo '<td colspan="5" class="tt">用户管理</td>'; echo '</tr>'; echo '<tr>'; echo '<td class="ttm">ID</td>'; echo '<td class="ttm">账号</td>'; echo '<td class="ttm">用户组</td>'; echo '<td class="ttm">注册时间</td>'; echo '<td class="ttm">管理</td>'; echo '</tr>'; while ($rows=mysql_fetch_array($rs)) { echo '<tr>'; echo '<td>'.$rows["id"].'</td>'; echo '<td>'.$rows["u_name"].'</td>'; echo '<td>'.$rows["u_uls"].'</td>'; echo '<td>'.$rows["u_joindate"].'</td>'; echo '<td>编辑 删除</td>'; echo '</tr>'; } mysql_free_result($rs); mysql_close(); echo '<tr>'; echo '<td colspan="5" class="pages">'; if($num_pages>1) { $current_page=($start/$display)+1; if($current_page!=1) { echo '上一页'; } for($i=1; $i<=$num_pages; $i++) { if($i != $current_page) { echo ' '.$i.' '; }else{ echo $i.' '; } } }else{ $current_page=1; } if($current_page !=$num_pages) { echo '下一页'; } echo '</td>'; echo '</tr>'; echo '</table>'; |