zoukankan      html  css  js  c++  java
  • php+mysql分页类

    分页方法文件名 pagestyle.php

    <?php
    
    function page($page,$total,$phpfile,$pagesize=3,$pagelen=3){
    $pagecode = "";
    $page = intval($page);
    $total = intval($total);
    if(!$total) return array();
    $pages = ceil($total/$pagesize);
    
    if($page<1) $page = 1;
    if($page>$pages) $page = $pages;
    
    $offset = $pagesize*($page-1);
    
    $init = 1;
    $max = $pages;
    $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
    $pageoffset = ($pagelen-1)/2;
    
    
    if($page!=1){
    $pagecode.=" <a href=\"{$phpfile}&page=1\"><IMG align=absMiddle src='/kor/img/btn_prev2.gif' width=11 height=11></a> ";
    $pagecode.=" <a href=\"{$phpfile}&page=".($page-1)."\"><IMG align=absMiddle src='/kor/img/btn_prev.gif' width=11 height=11></a> ";
    }
    
    if($pages>$pagelen){
    
    if($page<=$pageoffset){
    $init=1;
    $max = $pagelen;
    }else{
    
    if($page+$pageoffset>=$pages+1){
    $init = $pages-$pagelen+1;
    }else{
    
    $init = $page-$pageoffset;
    $max = $page+$pageoffset;
    }
    }
    }
    
    for($i=$init;$i<=$max;$i++){
    if($i==$page){
    $pagecode.='<STRONG>'.$i.'</STRONG>';
    } else {
    $pagecode.=" <a href=\"{$phpfile}&page={$i}\">[$i]</a> ";
    }
    }
    if($page!=$pages){
    $pagecode.=" <a href=\"{$phpfile}&page=".($page+1)."\"><IMG align=absMiddle src='/kor/img/btn_next2.gif' width=11 height=11></a> ";
    $pagecode.=" <a href=\"{$phpfile}&page={$pages}\"><IMG align=absMiddle src='/kor/img/btn_next.gif' width=11 height=11></a> ";
    }
    $pagecode.='</div>';
    return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
    }
    /**********
    $page = $_GET['page'];
    $total = 101;
    $phpfile = "1.php";
    $pagesize = 10;
    
    $code = page($page,$total,$phpfile,$pagesize,$pagelen=6);
    
    echo $code['pagecode'];
    **************/
    ?>

     调用页面代码member.php

    <?php
      $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM member WHERE 1";
      $sql = $sql." ORDER BY `mb_no` DESC";
      if (!$page) {
        $page = 1;
      }
      $page_row = ($page-1)*10;
      $sql = $sql." LIMIT ".$page_row." , 10";
      $query=mysql_query($sql);
      $sql2 = "select found_rows() as no";//获得查询出来的记录总数
      $query2=mysql_query($sql2);
      $row2=mysql_fetch_array($query2);
      while($row=mysql_fetch_array($query)){
        echo "……";
        ……
        ……
        等等输出代码,在这里就不做演示了
      }
    
      $page = $_GET['page'];//获得当前页数
      $total = $row2['no'];//记录总条数
      $phpfile = "member.php";//链接的文件名
      $pagesize = 10;//每页记录数
      $pagelen = 7;//显示7个页数
      
      $code = page($page,$total,$phpfile,$pagesize,$pagelen);
      echo $code['pagecode'];
    ?>
  • 相关阅读:
    [LeetCode] Wildcard Matching, Solution
    [LeetCode] Add Binary 解题报告
    [LeetCode] Validate Binary Search Tree 解题报告
    [LeetCode] ZigZag Conversion 解题报告
    [LeetCode] Best Time to Buy and Sell Stock II Solution
    [LeetCode] Anagrams 解题报告
    [LeetCode] Word Search 解题报告
    【转载】git/github初级运用自如
    关于实训的那点事儿
    【转载】解决git Push时请求username和password,而不是sshkey验证
  • 原文地址:https://www.cnblogs.com/xcxc/p/3131860.html
Copyright © 2011-2022 走看看