分页查询

    xiaoxiao2021-03-25  108

    分页查询封装好的类引用,就好写多了,主页面最终效果

    (1)首先就是要显现是内容

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <table width= "50%"  cellpadding= "0"  cellspacing= "0"  border= "1" >              <tr>                  <td>地区代号</td>                  <td>地区名称</td>                  <td>父级代号</td>              </tr>        <?php                  $db  =  new  MySQLi( "localhost" , "root" , "123" , "test3" );                                    $sql  =  " select * from chinastates " ;   //正常显示的表,都练好几遍了,不解释                       $result  =  $db ->query( $sql );          $attr  =  $result ->fetch_all();          foreach ( $attr  as  $v )          {              echo  "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td></tr>" ;          }              ?>          </table>           

     

    (2)分页中的参数要有总数,所以要求总数

    1 2 3 4 5 $sz  =  " select count(*) from chinastates" ;    //求总数                   $rz  =  $db ->query( $sz ); $az  =  $rz ->fetch_all(); $zt  =  $az [0][0];<br> echo  $zt ;

    可以输出,看一下  

    (3)引入封装好的分页的类,并且造新对象

    1 2 //调用封装好的分页类 include ( "./test16_fenye.class.php" );        1 2 3 $fenye  =  new  Page( $zt ,10);   //造新对象,参数就是写总条数,显示的条数                   $sql  =  " select * from chinastates " . $fenye ->limit;    //记得写完语句要加空格再拼语句        

    这样就显示了十条数据:

    (4)最后显示按钮就可以了,照常的引用分页就可以:显示上一页,下一页,跳转等

    1 2 3 <?php    echo  $fenye ->fpage();    //调用分页信息并输出,想显示什么样就可以在括号中添加索引号 ?> 

    1 2 3 <?php    echo  $fenye ->fpage(5,6,7);    //调用分页信息并输出,想显示什么样就可以在括号中添加索引号 ?> 

    (5)分页查询,查询关键字,并且使结果能够分页显示:传值给本页面

    1 2 3 4 5 <form action= "test16_fenye.php"  method= "get" >         关键字:<input type= "text"  name= "key"  value= "<?php echo $key ?>" />   //value值是让输入的值还存在         <input type= "submit"  value= "查询"  /> </form>         

    写查询条件:以前都讲过

    1 2 3 4 5 6 7 $tj  =  " 1=1 " ; $key  =  "" ; if (! empty ( $_GET [ "key" ])) {      $key  =  $_GET [ "key" ];      $tj  =  " areaname like '%{$key}%' " ; }

    要在后面查总数和表的语句上加上写的查询条件

    1 2 $sz  =  " select count(*) from chinastates where " . $tj ;   //求条数和 $sql  =  " select * from chinastates where " . $tj  . $fenye ->limit;    //分页

    转载请注明原文地址: https://ju.6miu.com/read-15051.html

    最新回复(0)