php分页代码

 更新时间:2016年11月25日 15:54  点击:1766
这是一款简单实用的php分页代码,同时也很好的利用的类的构造函数来实例分页的初始化,好了下面我们来看看这款代码如何吧。
 代码如下 复制代码

 class page extends mysql
 {
  public $page;
  public $page_size;
  private $table;
  private $condition;
  private $limit;
  private $href;
  private $action_value;
  private $url_value;
 //分页初始化

  function __construct($page,$page_size,$table,$condition,$limit,$href,$action_value,$url_value)
  {
   $this->page=$page;
   $this->page_size=$page_size;
   $this->table=$table;
   $this->condition=$condition;
   $this->limit=$limit;
   $this->href=$href;
   $this->action_value=$action_value;
   $this->url_value=$url_value;
   $this->get_page();
  }

 /**
  * get the page's value判断当前在第几页
  */
  function get_page()
  {
   if($this->page=="")
   {
    $this->page=1;
   }

   return $this->page;
  }

 /**
  * page main code //调用转到指定的分页代码号。
  */
  function go_page()
  {
  if($this->page!="")
  {
   $reslut=mysql::fn_select($this->table,'count(*) as `total`',$this->condition,'',$this->limit);
   $rs=mysql::fetch_array($reslut);
   $message_count=$rs['total'];  //get the messages's count number
   $page_count=ceil($message_count/$this->page_size);  //get the page's count number
   $offset=($this->page-1)*$this->page_size;  //get the first value of sql's limit

   if($message_count<=$this->page_size)
   {
    $page_code=array("","","","");
   }
   else if($this->page==1)
   {
    $page_code=array("",
        "",
        "<a name=code href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一页</a>",
        "<a name=code href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾页</a>");
   }
   else if($this->page==$page_count)
   {
       $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首页</a>",
        "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一页</a>",
        "",
        "");
   }
   else
   {
    $page_code=array("<a name='code' href=".$this->href."?page=1&action=".$this->action_value."&".$this->url_value.">首页</a>",
        "<a name='code' href=".$this->href."?page=".($this->page-1)."&action=".$this->action_value."&".$this->url_value.">上一页</a>",
        "<a name='code' href=".$this->href."?page=".($this->page+1)."&action=".$this->action_value."&".$this->url_value.">下一页</a>",
        "<a name='code' href=".$this->href."?page=".$page_count."&action=".$this->action_value."&".$this->url_value.">尾页</a>");
   }

   $page_info=array(
       "message_count"=>$message_count,
       "page_count"=>$page_count,
       "offset"=>$offset,
       "page_size"=>$this->page_size,
       "page_now"=>$this->page,
       "page_first"=>$page_code[0],
       "page_up"=>$page_code[1],
       "page_next"=>$page_code[2],
       "page_one_last"=>$page_code[3]
       );

   return $page_info;
  }
  }
 }


//php分页代码调用方法

 代码如下 复制代码

 $page = new page('',10,'`cy0871_users_info`','index.php',1,'','','userid=1');
 print_r ($page->go_page());
 $page_info = $page->go_page();
 echo $page_info['page']."<br/>";
 echo $page_info['page_size'];

 

文章这里为你提供一二款分页代码,里面有一款超简洁代码的分页程序哦,如果你正是php初学者这款分页代码很不错哦。
 代码如下 复制代码
$page_total = $num/$pagesize_wish;
  $page_total_int = (int)$page_total;
  if($page_total!=$page_total_int) $page_total = $page_total_int+1;
   for($i=1;$i<=$page_total;$i++){
    if($i==$_get['w']){
     print " <strong>".$i."</strong> ";
    }else{
     if(!$_get['w']){
      if($i==1){
       print " <strong>".$i."</strong> ";
      }else{
       print "<a href="../?w=$i"> $i </a>";
      }
     }else{
      print "<a href="../?w=$i"> $i </a>";
     }
    }
   }
  


   //分页代码二
   

 代码如下 复制代码
$pernumber=10; //每页显示的记录数
 $page=$_get['page']; //获得当前的页面值
 $count=mysql教程_query("select count(*) from user"); //获得记录总数
 $rs=mysql_fetch_array($count);
 $totalnumber=$rs[0];
 $totalpage=ceil($totalnumber/$pernumber); //计算出总页数
 if (!isset($page)) {
  $page=1;
 } //如果没有值,则赋值1
 $startcount=($page-1)*$pernumber; //分页开始,根据此方法计算出开始的记录
 $result=mysql_query("select * from user limit $startcount,$pernumber"); //根据前面的计算出开始的记录和记录数
 while ($row=mysql_fetch_array($result)) {
  echo "user_id:".$row[0]."<br>";
  echo "username:".$row[1]."<br>"; //显示数据库教程的内容
 }
 if ($page != 1) { //页数不等于1
 ?>
 <a href="fenye.php?page=<?php echo $page - 1;?>">上一页</a> <!--显示上一页-->
 <?php
 }
 for ($i=1;$i<=$totalpage;$i++) {  //循环显示出页面
 ?>
 <a href="fenye.php?page=<?php echo $i;?>"><?php echo $i ;?></a>
 <?php
 }
 if ($page<$totalpage) { //如果page小于总页数,显示下一页链接
 ?>
 <a href="fenye.php?page=<?php echo $page + 1;?>">下一页</a>
 <?php
 }
 

?>

下面写了二个php 字符串截取函数,方法很简单,string要截取的字符串,sublen长度,$start开位置哦。
 代码如下 复制代码

 function my_sub_str($string, $sublen, $start)
 {
  $pa = "/fdddd";
  preg_match_all($pa, $string, $t_string);

  if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
  return join('', array_slice($t_string[0], $start, $sublen));
 }

 /**
  * 字符串截取 不加"..."
  */

 代码如下 复制代码

 function my_sub_str_00($string, $sublen, $start)
 {
  $pa = "/sss/";
  preg_match_all($pa, $string, $t_string);

  return join('', array_slice($t_string[0], $start, $sublen));
 }

//字符截取测试

 代码如下 复制代码
$string ="www.111cn.net中国web第一站";
echo my_sub_str($string, 10, 0); //输出 www.111cn....
这种一款可以统计你网站当前在线人数的代码哦,由php+txt实现的无需数据来来支持。

  $time = gettimeofday(void);

  //文件初始化 start
  if(@filesize("time.text")<=0){

   $fd_time = fopen("time.text","w+");
   fputs($fd_time,$time[sec]);
   fclose($fd_time);

   $fd_time = fopen("ip.text","w+");
   fclose($fd_time);

  }
  //文件初始化 over


  //更新时间 start
  $tamp = file("time.text");
  $equal = ($time[sec] - $tamp[0]);
  if($equal > 60){

   $fd_time = fopen("time.text","w+");
   fputs($fd_time,"");
    fclose($fd_time);

  }
  //更新时间 over


  //检查ip start
  $fd_ip = fopen("ip.text","a+");

 //获取用户ip地址
  if ($http_server_vars["http_x_forwarded_for"])
 {
 $ip = $http_server_vars["http_x_forwarded_for"];
 }
 elseif ($http_server_vars["http_client_ip"])
 {
 $ip = $http_server_vars["http_client_ip"];
 }
 elseif ($http_server_vars["remote_addr"])
 {
 $ip = $http_server_vars["remote_addr"];
 }
 elseif (getenv("http_x_forwarded_for"))
 {
 $ip = getenv("http_x_forwarded_for");
 }
 elseif (getenv("http_client_ip"))
 {
 $ip = getenv("http_client_ip");
 }
 elseif (getenv("remote_addr"))
 {
 $ip = getenv("remote_addr");
 }
 else
 {
 $ip = "unknown";
 }


    $ip_adds = file("ip.text");
    for($i;$i<count($ip_adds);$i++){

        if($ip." "==$ip_adds[$i]){
         $ip_check = 1;
         break;
        }
    }

    if($ip_check!=1){
     fputs($fd_ip,$ip." ");
    }
    fclose($fd_ip);
  //检查ip over


  $ip_adds = count(file("ip.text"));

?>
调用 方法

<?php
/*
 * 标题:简单在线人数统计
 *
 * 作者:邓东东
 *
 * 创建于:2010-4-27下午09:42:31
 *
 * 技术支持:php100学习交流22 :108610071
 **/
?>
<?
include("online.php");
$fd_ip = file("ip.text");
?>
<title>在线人数</title>
<center><font color=blue size=6>php100论坛在线总人数为:<font color=red size=8><?=$ip_adds?>08610071</font>人<br>
用户ip:<?

echo "<select>";
for($i = 0;$i < count($ip_adds);$i++){
 echo"<option value = '$i'>".substr($fd_ip[$i],0,0-strlen(strrchr($fd_ip[$i],'.'))).".*</option> ";
}
echo "</select>";

 

 

?></font></center>

下面的代码使用两种方式来调facebook的接口,第一种县判断用户的环境是否开启了curl库,开启了这个库,就采用这种方式来获取请求。里面详细的参数讲解大家可以参考手册。

if(function_exists('curl_init'))
{
  $ch = curl_init();
  curl_setopt($ch, curlopt_url, $url_with_get);
  curl_setopt($ch, curlopt_post, 1);
  curl_setopt($ch, curlopt_postfields, $post);
  curl_setopt($ch, curlopt_returntransfer, true);
  $result = curl_exec($ch);
  curl_close($ch);
}
else
{
  $content = http_build_query($post)
  $content_length = strlen($content);
  $context =
  array('http' =>
array('method' => 'post',
'user_agent' => $user_agent,
'header' => 'content-type: ' . $content_type . " " .
'content-length: ' . $content_length,
'content' => $content));
$context_id = stream_context_create($context);
$sock = fopen($url_with_get, 'r', false, $context_id);
$result = '';
if ($sock)
  {
    while (!feof($sock))
  $result .= fgets($sock, 4096);
  fclose($sock);
}
return $result;
}
}

测试代码

 代码如下 复制代码


$url_with_get= "http://api.facebook.com/restserver.php?method=facebook.friends.get&session_key=&api_key=1232121311&v=1.0";
$post = array('sig'=>12312123234353);

[!--infotagslink--]

相关文章

  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • vue.js 表格分页ajax 异步加载数据

    Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24