PHP分页
<?php $db=new mysqli(); $db->connect('localhost','root','root','test'); $sql="select count(*) from user"; $res=$db->query($sql); $row=$res->fetch_row(); $pg_size=3; $row_total=$row[0]; $pg_total=ceil($row_total/$pg_size); $pg_now=@$_GET['pg_now']?$_GET['pg_now']:1; $sql="select * from user limit ".($pg_now-1)*$pg_size.",".$pg_size; $res=$db->query($sql); $rows=$res->fetch_all(MYSQLI_ASSOC); ?> <center> <table width="400" border="1"> <tr bgcolor="#ccc"><th>ID</th><th>name</th><th>pass</th></tr> <?php foreach($rows as $v){ echo "<tr>"; echo "<td>{$v['id']}</td>"; echo "<td>{$v['name']}</td>"; echo "<td>{$v['pass']}</td>"; echo "</tr>"; } ?> </table> <?php echo "<br>当前页:".$pg_now.'/'.$pg_total.' '; if($pg_now==1){ echo "上一页"; }else{ $pre_pg=$pg_now-1; echo "<a href='paginate.php?pg_now={$pre_pg}'>上一页</a> "; } for($i=1;$i<=$pg_total;$i++){ echo " <a href='paginate.php?pg_now={$i}'>{$i}</a>"; } if($pg_now==$pg_total){ echo " 下一页"; }else{ $next_pg=$pg_now+1; echo " <a href='paginate.php?pg_now={$next_pg}'>下一页</a> "; } echo "<br><br>"; ?> <form action="paginate.php" method="get"> 跳转到:<input type="text" name="pg_now"> <input type="submit" value="GO"> </form> </center>
效果图:
说明:
当当前页为第一页或最后一页的时候,上一页或下一页不能点击,实现代码:
if($pg_now==1){ echo "上一页"; }else{ $pre_pg=$pg_now-1; echo "<a href='paginate.php?pg_now={$pre_pg}'>上一页</a> "; }
if($pg_now==$pg_total){ echo " 下一页"; }else{ $next_pg=$pg_now+1; echo " <a href='paginate.php?pg_now={$next_pg}'>下一页</a> "; }
问题:
页码数过多怎么控制?