php mysql 分页函数

 更新时间:2016年11月25日 15:53  点击:1349
一款超简单的php mysql 分页,也是很实例的一款函数,他可以自动获取用户增加的参数,而不需要用户来增加,很自能化的一款自能分页程序。

+-----------------------------------------------------------------------
| 文件概要:分页显示函数
| 文件名称:page.php
| 创建时间:2010-5-20
+-----------------------------------------------------------------------
*/
 function _pageft($totle, $displaypg = 20, $url = '') {

  global $page, $firstcount, $pagenav, $_server;

  $globals["displaypg"] = $displaypg;

  if (!$page)
   $page = 1;
  if (!$url) {
   $url = $_server["request_uri"];
  }

  //url分析:
  $parse_url = parse_url($url);
  $url_query = $parse_url["query"]; //单独取出url的查询字串
  if ($url_query) {
   $url_query = preg_replace("/(^|&)page=$page/", "", $url_query);
   $url = str_replace($parse_url["query"], $url_query, $url);
   if ($url_query)
    $url .= "&page";
   else
    $url .= "page";
  } else {
   $url .= "?page";
  }
  $lastpg = ceil($totle / $displaypg); //最后页,也是总页数
  $page = min($lastpg, $page);
  $prepg = $page -1; //上一页
  $nextpg = ($page == $lastpg ? 0 : $page +1); //下一页
  $firstcount = ($page -1) * $displaypg;

  //开始分页导航条代码:
  $pagenav = "显示第 <b>" . ($totle ? ($firstcount +1) : 0) . "</b>-<b>" . min($firstcount + $displaypg, $totle) . "</b> 条记录&nbsp;&nbsp;共 <font color='red'>$totle</font> 条留言";

  //如果只有一页则跳出函数:
  if ($lastpg <= 1)
   return false;

  $pagenav .= " <a href='$url=1'>首页</a> ";
  if ($prepg)
   $pagenav .= " <a href='$url=$prepg'>前页</a> ";
  else
   $pagenav .= " 前页 ";
  if ($nextpg)
   $pagenav .= " <a href='$url=$nextpg'>后页</a> ";
  else
   $pagenav .= " 后页 ";
  $pagenav .= " <a href='$url=$lastpg'>尾页</a> ";

  //下拉跳转列表,循环列出所有页码:
  $pagenav .= " 到第 <select class='border:1px solid #98ccfa;' name='topage' size='1' onchange='window.location="$url="+this.value'>n";
  for ($i = 1; $i <= $lastpg; $i++) {
   if ($i == $page)
    $pagenav .= "<option value='$i' selected>$i</option>n";
   else
    $pagenav .= "<option value='$i'>$i</option>n";
  }
  $pagenav .= "</select> 页&nbsp;&nbsp;&nbsp;共 <font color='red'>$lastpg</font> 页";
 }
?>

本文章提供两种mysql分页代码,分页原是都很简单就是$total = @(int)($sum / $pagesize);$p = (int)$_GET[\'page\'];再到数据库中支limti X,X就成了。
 代码如下 复制代码
function fenyezhixin($table,$pagesize="",$paixu="",$do="",$wwhere=""){
    if(($table == "news_fabu") or ($table == "product_fabu") or ($table == "message") or ($table == "down_fabu") or ($table == "kucun")){
    $query = @mysql_query("select * from $table $wwhere");
    $pagesize = $pagesize;
    $sum = mysql_num_rows($query);
    if($sum == 0){
        $pagesize = 1;
    }
 if($pagesize=="0"){
     $sum = "1";
 }
    if (@($sum % $pagesize) == 0){
     $total = @(int)($sum / $pagesize);
 }else{
     $total = @(int)($sum / $pagesize) + 1;
 }
    if($total == 0){$total=1;}
    if (isset($_get['page'])){
        $p = (int)$_get['page'];
    }else{
        $p = 1;
    }
    $start = $pagesize * ($p - 1);
    $query = @mysql_query("select * from $table $wwhere order by $paixu limit $start,$pagesize") or die ("数据查询失败2!");
    if ($do == 1){
        $queryarray = array($query,$total,$sum,$p);
        return $queryarray;
    }
    if($do == 2){
        $parray = array($total,$sum,$p);
        return $parray;
    }
 }else{
     $query = @mysql_query("select * from $table $wwhere order by $paixu limit $pagesize") or die ("数据查询失败1!");
     if ($do == 1){
            $queryarray = array($query,$total,$sum,$p);
            return $queryarray;
     }
     if($do == 2){
            $parray = array($total,$sum,$p);
            return $parray;
     }
 }
}
//返回分页条  
function fenyedaohang($total="",$sum="",$p="",$menut=""){
 $w = substr($menut,strrpos($menut,"&")+1,2);
 $wr = substr($menut,strrpos($menut,"=")+1,strlen($menut));
 $pindao = $_server["script_name"];$pinstrlen = strrpos($pindao,"/"); $pindao = substr($pindao,$pinstrlen+1,strlen($pindao));
    if($w == "pr"){
     $queryr = mysql_query("select feiye.feiye_what from feiye where feiye.feiye_page = '$pindao'");
  $rows = mysql_fetch_row($queryr); $rrows = $rows[0];
  if(empty($rrows)){
      mysql_query("insert into `feiye` (`feiye_page`, `feiye_what`) values ('$pindao', '$wr')");
   echo "<meta http-equiv='refresh' content='0'>";
  }else{
      if($wr != $rrows){
        mysql_query("update `feiye` set `feiye_what`='$wr' where (`feiye_page`='$pindao')");
     echo "<meta http-equiv='refresh' content='0'>";
   }
  }
 }
 if($w == "ne"){
     $queryr = mysql_query("select feiye.feiye_what from feiye where feiye.feiye_page = '$pindao'");
  $rows = mysql_fetch_row($queryr); $rrows = $rows[0];
  if(empty($rrows)){
      mysql_query("insert into `feiye` (`feiye_page`, `feiye_what`) values ('$pindao', '$wr')");
   echo "<meta http-equiv='refresh' content='0'>";
  }else{
      if($wr != $rrows){
        mysql_query("update `feiye` set `feiye_what`='$wr' where (`feiye_page`='$pindao')");
     echo "<meta http-equiv='refresh' content='0'>";
   } 
  }
 }
    echo "共"."$total"."页&nbsp;"."记录"."$sum"."条&nbsp;当前"."$p"."/"."$total"."页&nbsp;&nbsp;";
    if($total == 1){
        echo "<font  class="page">首页</font>";
    }else{
        echo "<a href='?page=1&menu=$menut' class="page">首页</a>"."&nbsp;";
    }
    if ($p > 1){
        $prev = $p - 1;
        echo "<a href='?page=$prev&menu=$menut' class="page">上一页</a>"."&nbsp;";
    }else{
        echo "<font class="page">上一页</font>"."&nbsp;";
    }
    $page = $_get["page"];
    $pagesum = $page+5;
    if($total >= 11){
        if($pagesum <=11 ){
            $pagesum = 11;
        }
    }
    if($pagesum >= $total){
        $pagesum = $total;
    }
    $pagestart = $page - 5;
    if($pagestart <= 0){
        $pagestart = 1;
    }
    if($total >= 11 and ($total-4) <= $page){
        $pagestart = $total-10;
    }
    for($i=$pagestart;$i<=$pagesum;$i++){
        if($i == $p){
            echo "<font color=cccccc>&nbsp;$i&nbsp;</font>";
        }else{
            echo "<a href='?page=$i&menu=$menut' class="page" >$i</a>";
        }
    }
    if ($p < $total){
        $next = $p + 1;
        echo "&nbsp;<a href='?page=$next&menu=$menut' class="page" >下一页</a>"."&nbsp;";
    }else{
        echo "<font class="page" >下一页</font>"."&nbsp;";
    }
    if($total == 1){
        echo "<font  class="page">尾页</font>";
    }else{
        echo "<a href='?page=$total&menu=$menut' class="page">尾页</a>";
    }
}

 在php中有很多方法来把目录所有文件列出的代码,用e
*/

$list = scandir(".");
    $zipname = "";
    foreach($list as $file)
    {
      if($file=="."||$file=="..")continue;
      $b=substr($file,-3);
      if($b==".gz"||$b==".fz")
      { $zipname = $file; break; }
    }
 
//代码二

$d=dir(".");
echo $d->path.$e;
while(false !== ($e= $d->read())) {
    echo "<a href=$e target=_blank >$e</a>"."<br>";
    }
$d->close();


//最简单的方法

$dirs    = array();
foreach(glob("test/*") as $d)
{
    if(is_dir($d))
    {
        $dirs[]    = $d;
    }
}
print_r($dirs);

//方法四

glob("test/*", glob_onlydir) ;

//方法五

function clean_dir($path)        {
        if (!is_dir($path))        {
                if (is_file($path))        {
                        unlink($path);
                }
                return;
        }
        $p=opendir($path);
        while ($f=readdir($p))        {
                if ($f=="." || $f=="..") continue;
                clean_dir($path.$f);
        }
        rmdir($path);
        return;
}

本文章提供一款完整的php目录 文件在线解压缩程序,他可打包指定目录并且把目录下所有目录与文件名都打包好,按rar的方式打包,目录结构不变,同时也提供解压功能。

$fz = new fmzip;
$fz->setzipname("打包文件名");

#打包/压缩
$fz->setsource("待打包目录");
$fz->compress($silent,$compress);

#解包/解压
$fz->settarget("待解包目录");
$fz->uncompress($silent);
*/
class fmzip
{

var $source; //压缩源
var $target; //解压目的文件夹
var $zipname;//压缩文件名
var $handle; //打开压缩文件的句柄
var $silent; //是否输出
var $count_dir; //计数器_文件夹
var $count_file;//计数器_文件
var $dirlist;

function setsource($source)//设置压缩源
{
  if(!file_exists($source))
    die("source <$source> does not exist.");
  $this->source = $source;
}

function settarget($target)//设置解压目的文件夹
{
  if(!file_exists($target))
    $this->makedir($target);
  chdir(dirname($_server['script_filename']));
  if(substr($target,-1)=="/")
    $target = substr($target,0,strlen($target)-1);
  if(!file_exists($target))
  {
    die("target <$target> does not exist.");
  }
  $this->target = $target;
}

function setzipname($zipname)//设置压缩文件名
{
  if(empty($zipname)) $zipname = "fmzip.fz";
  $this->zipname = $zipname;
}

function compress($silent = false, $compress = true) //压缩
{
  $this->silent = $silent;
  if($silent===false)echo "<pre>compressing...rn";
  if(is_file("$this->zipname"))unlink("$this->zipname");
  $this->handle = fopen($this->zipname,"w");//创建压缩文件
  if($this->handle == null) die("error creating $this->zipname");//打开失败
  $this->count_dir = 0; $this->count_file = 0; //初始化计数器
  $this->merge($this->source);//压缩
  fwrite($this->handle,"-1");//结束标志
  fclose($this->handle);//关闭文件
  echo "rndirectory: $this->count_dir";
  echo "rnfile: $this->count_filern";
  if(function_exists("gzcompress") && $compress==true)
  {
    file_put_contents("$this->zipname.gz",gzcompress(file_get_contents("$this->zipname")));
    unlink("$this->zipname");
  }
  if($silent===false)
  {
    echo $this->listfile();
    echo "</pre>";
  }
}

function listfile()
{
  if(file_exists("$this->zipname.gz"))
    return "<a href="$this->zipname.gz" target="_blank">download $this->zipname.gz</a>";
  if(file_exists("$this->zipname"))
    return "<a href="$this->zipname" target="_blank">download $this->zipname</a>";
}

function merge($dir)//合并文件、文件夹(递归)
{
/* 说明:不处理link。 */
 if(is_dir($dir))//如果压缩源是文件夹
 {
  $list = scandir($dir);//扫描文件列表
  natcasesort($list);
  foreach($list as $file)//先处理文件夹
  {
    $full = "$dir/$file";
    if(!is_dir($full)||$file=="."||$file=="..")continue;//只处理文件夹
    $this->count_dir++;
    if($this->silent===false)
      echo "[dir] $fullrn"; //输出提示
    fwrite($this->handle,$this->file_info($full));//写入文件夹信息
    $this->merge($full);//递归合并下级文件夹
  }//文件夹处理完毕;
  foreach($list as $file)//处理文件
  {
    $full = "$dir/$file";
    if(!is_file($full)||$file=="."||$file=="..")continue; //只处理文件
    $this->count_file++;
    if($this->silent===false)
      echo "[file] $fullrn";//输出提示
    fwrite($this->handle,$this->file_info($full));//写入文件信息
  }//文件处理完毕
 }
 else
 {
   $this->count_file++;
   if($this->silent===false)echo "[file] $fullrn";//输出提示
   fwrite($this->handle,$this->file_info($file));//写入文件信息
 }
}//end function merge

function file_info($file)
{
  $perm = substr(sprintf('%o',fileperms($file)), -3); //权限
  $filename = str_replace($this->source,"",$file);
  if(is_file($file))//文件
  {
    $size = filesize($file); //文件大小
    return "1rn$filenamern$permrn$sizern".file_get_contents($file)."rn";// .文件内容
  }
  if(is_dir($file))//目录
    return "0rn$filenamern$permrn";
}//end function file_info

function uncompress($silent = false)
{
  $this->silent = $silent;
  if($silent===false)echo "<pre>uncompressing...rn";
  if(substr($this->zipname,-3)==".gz")
    $this->zipname = substr($this->zipname,0,strlen($this->zipname)-3);
  if(file_exists("$this->zipname.gz"))
  {
    if(!function_exists(gzuncompress))
      die("function gzuncompress is not supported. unable to continue.");
    file_put_contents($this->zipname,gzuncompress(file_get_contents("$this->zipname.gz")));
  }
  $this->handle = fopen($this->zipname,"r");
  if($this->handle == null) die("error reading $this->zipname");//打开失败
  $count = 0;
  while(1)
  {
    $count ++;
    $type = $this->read_line(); //读取类型
    if($type === "-1")break;//处理完毕,退出
    $filename = $this->target.$this->read_line();//读取文件名
    $permission = $this->read_line();//读取权限
/* <文件夹> [0]n[file_name]n[perm]n */
    if($type === "0")//目录
    {
      if($this->silent === false)//输出提示
        echo "[dir] $filename  [$permission]rn";
      $this->makedir($filename);//创建文件夹
      chdir(dirname($_server['script_filename']));
      chmod($filename,$permission);
      $this->dirlist[$filename] = 1;
      continue;
    }
/* <文件> [1]n[file_name]n[perm]n[size]n[contents]n */
    if($type === "1")//文件
    {
      $this->count_file++;
      $size = $this->read_line(); //读取文件大小
      if($this->silent === false)//输出提示
        echo "[file] $filename  [$permission] [size = $size]rn";
      if($size!=0)
      {
        $fp = fopen($filename,"w");
        $contents = fread($this->handle,$size);
        fwrite($fp,$contents);
        fclose($fp);
        chmod($filename,$permission);
      }
      $this->read_line();//内容后的一个回车
      continue;
    }
  }
  $this->count_dir = count($this->dirlist);
  if($silent===false)
    echo "ndirectory: $this->count_dir";
    echo "nfile: $this->count_filen</pre>n";
  fclose($this->handle);
  if(file_exists("$this->zipname.gz"))unlink("$this->zipname");
}//end function uncompress;

function read_line()
{
  $a = fgets($this->handle);
  $a = str_replace("rn","",$a);
  $a = str_replace("n","",$a);
  return $a;
}

function makedir($full)
{
  list($a,$b) = split("/",$full,2);
  if($a == "") return;
  if(file_exists($a)&&!is_dir($a))die("can't create dir $a");
  if(!file_exists($a))@mkdir($a);
  chdir($a);
  if($b!=="")
    $this->makedir($b);
  chdir("..");
}//end function makedir

} //end class fmzip

/*

使用方法:

#必须
include("包含这个class的php文件");
$fz = new fmzip;
$fz->setzipname("打包文件名");

#打包/压缩
$fz->setsource("待打包目录");
$fz->compress($silent,$compress);

#解包/解压
$fz->settarget("待解包目录");
$fz->uncompress($silent);

$silent : true|false (不加引号!) 是否产生输出 默认为true,不产生
$compress : true|false (不加引号!) 是否压缩 默认为true,压缩

这里把数据库中的常用的内容保存到一个标准的php格式的文件当中,这样使用起来也方便了很多,下面代码。

$res=mysql教程_query("select k1,k2  from ".table('keywords')." ") ;
$str="<?php rn ";
while($rs=mysql_fetch_array($res))
{
 
 $str .="$keyword['".$rs[0]."']='".$rs[1]."';rn";
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//方法二

$f=file_get_contents("w1.txt");
$f=str_replace("rn","<br>",$f);//替换换行符
$arr=explode("<br>",$f);
$str="<?php rn ";
foreach($arr as $t)
{
 $rs=explode("|",$t);
 //不能包含?等特殊符号
 $str .="$keyword['".str_replace("?","",$rs[0])."']='".str_replace("?","",$rs[1])."';rn";
 
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//conn.php文件
$conn=mysql_connect($mysql_host,$mysql_user,$mysql_password) or die('连接服务器出错');
mysql_select_db($mysql_db) or die("选择数据库出错");
mysql_query("set names 'gbk'");
function table($t)
{
 global $mysql_table_prefix;
 return $mysql_table_prefix.$t;
}
?>

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 详解Mysql中的JSON系列操作函数

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • 深入研究mysql中的varchar和limit(容易被忽略的知识)

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • mysql的3种分表方案

    一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28