php 分页实现

$url=$_SERVER[‘REQUEST_URL’]; //获得当前请求的URL

$url=parse_url($url);//将获得的URL解析成有固定键值的数组

$url=$url[‘path’];//获得路径

 

$pageSize=10;//设置一页显示的条数

if(isset($_GET[‘page’]))

$currentPage=$_GET[‘page’];

else

   $currentPage=1;

 

$conn=@mysql_connnect(“localhost”,”root”,”root”)or die (“数据库连接失败”);

mysql_select_db(“test”,$conn);

mysql_query(“set names ‘utf-8’”);

$sql=”select * from policyinfo”;

$query=mysql_query($sql);

$totalSize=mysql_num_rows($query);

$totalPage=ceil($totalSize/$pageSize);//向上取整函数

$from=($currentPage-1)* $pageSize;

$sql=”select * from policyinfo limit $from, $pageSize”;

$query=mysql_query($sql);

While($row=mysql_fetch_array($query))

{

显示数据

}

//下一页 上一页 首页 尾页按钮

<table style="width: 100%;">

<tr>

<td align="right">

    <div style="line-height: 33px; float: right">

<?php

        if ($currentPage==1)

           {

                          ?>

                             <input type="button" name="FirstPage" value="首页" id="FirstPage" disabled>

 

                                <input type="button" name="PreviousPage" value="上一页"

                                   id="PreviousPage" disabled>

                        <?php

                             }

                             else {

                         ?>

 

                                <input type="button" name="FirstPage" value="首页" id="FirstPage"

                                  onclick="javascript:window.location.href='<?php echo $url."?page=1";?>'">

 

                                <input type="button" name="PreviousPage" value="上一页"

                                   id="PreviousPage" class="btbuttom"

                                  onclick="javascript:window.location.href='<?php echo $url."?page=".($currentPage-1);?>'">

                            <?php

                             }

                             if ($currentPage>=$totalPage)

                             {

                          ?>

                                  <input type="button" name="NextPage" value="下一页" id="NextPage" disabled>

 

                                <input type="button" name="LastPage" value="尾页" id="LastPage" disabled>

                        <?php

                             }

                             else {

                         ?>

                                   <input type="button" name="NextPage" value="下一页" id="NextPage"

                                       onclick="javascript:window.location.href='<?php echo $url."?page=".($currentPage+1);?>'">

   

   

                                   <input type="button" name="LastPage" value="尾页" id="LastPage"

                                       onclick="javascript:window.location.href='<?php echo $url."?page=".$totalPage;?>'">

                         <?php

                             }

                         ?>    

                         </div>

                     </td>

 

                  </tr>

                                          </table>

posted on 2014-02-26 15:34  炜祎  阅读(129)  评论(0编辑  收藏  举报