/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('www.1bo8.cn','/restServer.php',$post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){
$socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket,"POST $remote_path HTTP/1.0\r\n");
fwrite($socket,"User-Agent: Socket Example\r\n");
fwrite($socket,"HOST: $remote_server\r\n");
fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n");
fwrite($socket,"Content-length: ".strlen($post_string)+8."\r\n");
fwrite($socket,"Accept:*/*\r\n");
fwrite($socket,"\r\n");
fwrite($socket,"mypost=$post_string\r\n");
fwrite($socket,"\r\n");
$header = "";
while ($str = trim(fgets($socket,4096))) {
$header.=$str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket,4096);
}
return $data;
}
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://www.1bo8.cn/restServer.php',$post_string);
*/
function request_by_curl(/blog_article/$remote_server,$post_string/index.html){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$remote_server);
curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://www.1bo8.cn/restServer.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
$context = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".
'User-Agent : Jimmy\'s POST Example beta'."\r\n".
'Content-length: '.strlen($post_string)+8,
'content'=>'mypost='.$post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server,FALSE,$stream_context);
return $data;
}
?>
<?php
/*
功能实用的php分页类
subpages
*/
class SubPages{
private $each_disNums;//每页显示的条目数
private $nums;//总条目数
private $current_page;//当前被选中的页
private $sub_pages;//每次显示的页数
private $pageNums;//总页数
private $page_array = array();//用来构造分页的数组
private $subPage_link;//每个分页的链接
private $subPage_type;//显示分页的类型
/*
__construct是SubPages的构造函数,用来在创建类的时候自动运行.
@$each_disNums 每页显示的条目数
@nums 总条目数
@current_num 当前被选中的页
@sub_pages 每次显示的页数
@subPage_link 每个分页的链接
@subPage_type 显示分页的类型
当@subPage_type=1的时候为普通分页模式
example: 共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
当@subPage_type=2的时候为经典分页样式
example: 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){
$this->each_disNums=intval($each_disNums);
$this->nums=intval($nums);
if(!$current_page){
$this->current_page=1;
}else{
$this->current_page=intval($current_page);
}
$this->sub_pages=intval($sub_pages);
$this->pageNums=ceil($nums/$each_disNums);
$this->subPage_link=$subPage_link;
$this->show_SubPages($subPage_type);
//echo $this->pageNums."--".$this->sub_pages;
}
/*
__destruct析构函数,当类不在使用的时候调用,该函数用来释放资源。
*/
function __destruct(){
unset($each_disNums);
unset($nums);
unset($current_page);
unset($sub_pages);
unset($pageNums);
unset($page_array);
unset($subPage_link);
unset($subPage_type);
}
/*
show_SubPages函数用在构造函数里面。而且用来判断显示什么样子的分页
*/
function show_SubPages($subPage_type){
if($subPage_type == 1){
$this->subPageCss1();
}elseif ($subPage_type == 2){
$this->subPageCss2();
}
}
/*
用来给建立分页的数组初始化的函数。
*/
function initArray(){
for($i=0;$i<$this->sub_pages;$i++){
$this->page_array[$i]=$i;
}
return $this->page_array;
}
/*
construct_num_Page该函数使用来构造显示的条目
即使:[1][2][3][4][5][6][7][8][9][10]
*/
function construct_num_Page(){
if($this->pageNums < $this->sub_pages){
$current_array=array();
for($i=0;$i<$this->pageNums;$i++){
$current_array[$i]=$i+1;
}
}else{
$current_array=$this->initArray();
if($this->current_page <= 3){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$i+1;
}
}elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;
}
}else{
for($i=0;$i<count($current_array);$i++){
$current_array[$i]=$this->current_page-2+$i;
}
}
}
return $current_array;
}
/*
构造普通模式的分页
共4523条记录,每页显示10条,当前第1/453页 [首页] [上页] [下页] [尾页]
*/
function subPageCss1(){
$subPageCss1Str="";
$subPageCss1Str.="共".$this->nums."条记录,";
$subPageCss1Str.="每页显示".$this->each_disNums."条,";
$subPageCss1Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss1Str.="[<a href='/blog_article/$firstPageUrl/index.html'>首页</a>] ";
$subPageCss1Str.="[<a href='/blog_article/$prewPageUrl/index.html'>上一页</a>] ";
}else {
$subPageCss1Str.="[首页] ";
$subPageCss1Str.="[上一页] ";
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss1Str.=" [<a href='/blog_article/$nextPageUrl/index.html'>下一页</a>] ";
$subPageCss1Str.="[<a href='/blog_article/$lastPageUrl/index.html'>尾页</a>] ";
}else {
$subPageCss1Str.="[下一页] ";
$subPageCss1Str.="[尾页] ";
}
echo $subPageCss1Str;
}
/*
构造经典模式的分页
当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function subPageCss2(){
$subPageCss2Str="";
$subPageCss2Str.="当前第".$this->current_page."/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss2Str.="[<a href='/blog_article/$firstPageUrl/index.html'>首页</a>] ";
$subPageCss2Str.="[<a href='/blog_article/$prewPageUrl/index.html'>上一页</a>] ";
}else {
$subPageCss2Str.="[首页] ";
$subPageCss2Str.="[上一页] ";
}
$a=$this->construct_num_Page();
for($i=0;$i<count($a);$i++){
$s=$a[$i];
if($s == $this->current_page ){
$subPageCss2Str.="[<span >".$s."</span>]";
}else{
$url=$this->subPage_link.$s;
$subPageCss2Str.="[<a href='/blog_article/$url/index.html'>".$s."</a>]";
}
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss2Str.=" [<a href='/blog_article/$nextPageUrl/index.html'>下一页</a>] ";
$subPageCss2Str.="[<a href='/blog_article/$lastPageUrl/index.html'>尾页</a>] ";
}else {
$subPageCss2Str.="[下一页] ";
$subPageCss2Str.="[尾页] ";
}
echo $subPageCss2Str;
}
}
?>
测试页面,test.php
<?php
require_once("SubPages.php");
//每页显示的条数
$page_size=20;
//总条目数
$nums=1024;
//每次显示的页数
$sub_pages=10;
//得到当前是第几页
$pageCurrent=$_GET["p"];
//if(!$pageCurrent) $pageCurrent=1;
$subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"test.php?p=",2);
?>
php分页类。
<?php
/*
* 快速php分页类
*
**/
class Page{
private $totalpage;
private $stride;
private $currentpage;
//设置总页数
function setTotalpage($objpage=1){
$this->totalpage=$objpage;
}
//设置当前页
function setCurrentpage($objpage=1){
$this->currentpage=$objpage;
}
//设置跨度
function setStride($objStride=1){
$this->stride=$objStride;
}
//获得总页数
function getTotalpage(){
return $this->totalpage;
}
//获得跨读
function getStride($objStride=1){
return $this->stride;
}
//获取当前页
function getCurrentpage($objpage=1){
return $this->currentpage;
}
//打印分页
public function Pageprint(){
for($Tmpa=1;$Tmpa<=$this->totalpage;$Tmpa++){
if($Tmpa+$this->stride<$this->currentpage){//加了跨度还小于当前页的不显示
continue;
}
if($Tmpa+$this->stride==$this->currentpage){//刚好够跨度的页数
$p=$this->currentpage-1;
$willprint.="<a href=/index.html"$_SERVER[PHP_SELF]?page=1\"><span>首页</span></a> <a href=/index.html"$_SERVER[PHP_SELF]?page=$p\"><span>上一页</span></a> ";
}
if($Tmpa>$this->currentpage+$this->stride){//大于当前页+跨度的页面
break;
}
$willprint.="[<span><a href=/index.html"$_SERVER[PHP_SELF]?page=$Tmpa\">$Tmpa</a></span>]";
if($Tmpa==$this->currentpage+$this->stride){//刚好够跨度的页数
$p=$this->currentpage+$this->stride+1;
$willprint.="<a href=/index.html"$_SERVER[PHP_SELF]?page=$p\"><span>下一页</span></a> <a href=/index.html"$_SERVER[PHP_SELF]?page=$this->totalpage\"><span>末页</span></a>";
}
}
echo $willprint;
}
}
class cut_page extends Page
{
var $filename;
var $showtotal;
function __construct($filename){
$this->cachefile = $filename;
$this->showtotal = "yes";
}
public function filename(){
if (file_exists($this->cachefile)){
return 1;
}
else
return 0;
}
private $type;
public function typelist($typeid){
$this->type = $typeid;
$fp = fopen($this->cachefile,"rb");
$str= fread($fp,filesize($this->cachefile));
//echo $this->type."<br>"; //分类ID
$arr= explode()("::",$str);
for ($i=0;$i<count($arr);$i++){
$arr2 = explode("=",$arr[$i]);
if($arr2[0]==$this->type){
//return $arr2[1];
if(isset()($_GET[page])){
$page=$_GET[page];
}else{
$page=1;
}
Page::setTotalpage($arr2[1]);
Page::setCurrentpage($page);
Page::setStride(3);
Page::Pageprint();
if ($this->showtotal=="yes"){
echo "<span border:1px solid #ccc;margin-left:40px;padding:1px 1px;\">".$page."/".$arr2[1]."页</span>";
}
}
}
fclose($fp);
}
public function updatelist($typeid,$value){
$this->type = $typeid;
$this->typevalue=$value;
if (file_exists($this->cachefile)){ //若文件存在
$fp = fopen($this->cachefile,"rb+");
$str= fread($fp,filesize($this->cachefile));
if(ereg("::".$this->type."=[0-9]+",$str)){
//正则替换
$str2= ereg_replace("::".$this->type."=[0-9]+","::".$this->type."=".$this->typevalue,$str);
fseek($fp,0); //从文件头开始
fwrite($fp,$str2); //写入文件新数据
$seek = ftell($fp); //返回文件指针长度
ftruncate($fp,$seek); //截断文件
}
else{
$str2 = "::".$this->type."=".$this->typevalue;
fwrite($fp,$str2); //若分类ID不存在,则续写文件
}
}
else{ //若文件不存在
$fp = fopen($this->cachefile,"xb");
$str="::".$this->type."=".$this->typevalue;
fwrite($fp,$str);
}
fclose($fp);
}
}
/*使用方法,参数为缓存文件名,可自定义*/
$listpage = new cut_page("pages_cache.txt");
/*列出分页 参数为分类的ID*/
$listpage->typelist("2");
$listpage->showtotal="yes"; //是否显示总数yes /no 默认为yes
echo "<br>";
/*
修改分页文件或者创建分页文件,参数分别为分类ID,分页数量,此方法使用在创建文章后遍历数据库获得分类的ID及分页的总量
分页=总记录/每页显示数目 ^0^这个自己算。updatelist(type,num)
//$listpage->updatelist("6","900");
cut_page为主类。可进行分页的创建,修改,显示
*/
?>