一直超简单的php分页代码-支持smarty模板分页
<?php
class My_Lib_page{
//当前页
var $page = 1;
//每页显示条数
var $size = 20;
//总记录数
var $count;
//每栏显示条数
var $lun = 10;
//参数绑订
var $param;
//跳转
var $run;
//name=admin&pwd=2132&sex=1
//$_GET['name'] = admin;
private function setParam(){
$param = $this->param;
unset($param['page']);
if(is_array($param) && !empty($param)){
foreach($param as $key=>$value){
$data[] = $key."/".$value;
}
$this->url = implode("/",$data);
$this->param = $this->url;
$this->url = "/".$this->url;
}else{
if(!empty($param)){
$this->url = $param;
$this->param = $this->url;
$this->url = "/".$this->url;
}
}
}
function pageDate(){
$this->setParam();
$num = ceil($this->lun/2);
$this->page = intval($this->page);
$this->page = $this->page <= 0 ? 1 : $this->page;
$cpage['countPage'] = ceil($this->count/$this->size); //总页数
$cpage['page'] = $this->page > $cpage['countPage'] ? $cpage['countPage']:$this->page ;
//当前页
//显示总数
$cpage['lun'] = $cpage['countPage'] < $this->lun ? $cpage['countPage'] : $this->lun;
//开始页码
$cpage['start'] = $this->page > $num-1 ? $this->page-$num+1 : 1;
$cpage['start'] = ($cpage['start']+$cpage['lun'])>=$cpage['countPage'] ? $cpage['countPage']-$cpage['lun']+1 : $cpage['start'];
//参数显示
$cpage['url'] = $this->url;
//控制跳转
$cpage['run'] = $this->run ? 1 : 0;
$cpage['param'] = is_array($this->param) ? '': "?".$this->param;
return $cpage;
}
}
/*include "../inc/smarty.php";
$cp = new page;
empty($_REQUEST['page']) || $cp->page = $_REQUEST['page'];
$cp->count = 70000;
$cp->param = $_GET;
$cp->size = 100;
$cp->lun = 7;
$cp->run = true;
$data = $cp->pageDate();
$smarty->assign("page",$data);
$smarty->display("cpage.html");
*/
?>
<?php
class My_Lib_Functions {
# ********************************************
# 对象转数组
# ********************************************
function object2array ($obj) {
foreach ($obj as $k => $v) {
$arr[$k] = $v;
unset($v);
}
return $arr;
}
// ********************************************
// 获取限定区间内的图片宽高
//$mx :图像在页面上显示的宽度
//$my :图像在页面上显示的高度
//此处缩略图按比例缩放的原理为x/y = $mx / $my;
// **********************************************
function getPicXY($picurl, $mx = 0, $my = 0){
if (!$size = @GetImageSize($picurl)) return false; //GetImageSize用来测定任何图象大小并返回图象的尺寸以及文件类型和一个用于普通HTML文件中<IMG>标记中的height/width文本字符串
if ($mx == 0 && $my == 0) return $size; //当$mx与$my参数的值为零时,返回图象的大小
if ($mx * $size[1] / $size[0] > $my){ //判断当$mx乘以图象本身的高度再除以图象的宽度大于在页面上显示的图象宽度时
$newXY[0] = $my * $size[0] / $size[1]; //设置产生新图像的宽度为$mx的值乘以图象本身的宽度再除以图象本身的高度
$newXY[1] = $my; //设置产生的新图像的高度为页面图象显示的高度
}else{ //判断当$mx乘以图象本身的高度再除以图象的宽度不大于在页面上显示的图象宽度时
$newXY[0] = $mx; //设置产生的新图像的宽度为页面图象显示的宽度
$newXY[1] = $mx * $size[1] / $size[0]; //设置产生新图像的宽度为$mx的值乘以图象本身的高度再除以图象本身的宽度
}
if ($size[0] <= $my and $size[1] <= $mx){ //判断当图像本身的宽度小于等于页面上显示图像的宽度且图像本身的高度小于等于页面上显示图像的高度
$newXY[0] = $size[0]; //设置图像新产生的宽度为图像本身的宽度
$newXY[1] = $size[1]; //设置图像新年产的高度为图像本身的高度
}
return $newXY; //返回图像的新比例
}
function getNewImageWH2 ($imageUrl, $maxWidth = 0, $maxHeight = 0, &$newWidth = 0, &$newHeight = 0){
if (!$imageSize = @GetImageSize($imageUrl)) return false;
if ($maxWidth == 0 || $maxHeight == 0) {
$newWidth = $imageSize[0];
$newHeight = $imageSize[1];
} else {
$newWidth = $maxWidth;
$newHeight = $newWidth * $imageSize[1] / $imageSize[0];
if ($newHeight < $maxHeight) {
$newHeight = $maxHeight;
$newWidth = $newHeight * $imageSize[0] / $imageSize[1];
}
$imageSize[0] = $newWidth;
$imageSize[1] = $newHeight;
}
return $imageSize;
}
}
?>
<?php
class My_Lib_pages{
private $each_disNums;//每页显示的条目数
private $nums;//总条目数
var $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);
$nums = $nums==0 ? 1: $nums;
$this->nums=intval($nums);
$this->pageNums=ceil($nums/$each_disNums);
$this->current_page=intval($current_page);
$this->current_page= $this->current_page<=0 ? 1: $this->current_page;
$this->current_page= $this->current_page > $this->pageNums ? 1 : $this->current_page;
$this->sub_pages=intval($sub_pages);
$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='$firstPageUrl'>首页</a>] ";
$subPageCss1Str.="[<a href='$prewPageUrl'>上一页</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='$nextPageUrl'>下一页</a>] ";
$subPageCss1Str.="[<a href='$lastPageUrl'>尾页</a>] ";
}else {
$subPageCss1Str.="[下一页] ";
$subPageCss1Str.="[尾页] ";
}
return $subPageCss1Str;
}
/*
构造经典模式的分页
当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/
function subPageCss2(){
$subPageCss2Str="";
$subPageCss2Str.="当前第<span style=color:red;>".$this->current_page."</span>/".$this->pageNums."页 ";
if($this->current_page > 1){
$firstPageUrl=$this->subPage_link."1";
$prewPageUrl=$this->subPage_link.($this->current_page-1);
$subPageCss2Str.="<a href='$firstPageUrl'>首页</a> ";
$subPageCss2Str.="<a href='$prewPageUrl'><<</a>";
}else {
$subPageCss2Str.="<span class=disabled>首页</span>";
$subPageCss2Str.="<span class=disabled><<</span>";
}
$a=$this->construct_num_Page();
for($i=0;$i<count($a);$i++){
$s=$a[$i];
if($s == $this->current_page ){
$subPageCss2Str.="<span style='color:red;font-weight:bold;'>".$s."</span>";
}else{
$url=$this->subPage_link.$s;
$subPageCss2Str.="<span class=current><a href='$url'>".$s."</a></span>";
}
}
if($this->current_page < $this->pageNums){
$lastPageUrl=$this->subPage_link.$this->pageNums;
$nextPageUrl=$this->subPage_link.($this->current_page+1);
$subPageCss2Str.=" <a href='$nextPageUrl'>>></a> ";
$subPageCss2Str.="<a href='$lastPageUrl'>尾页</a> ";
}else {
$subPageCss2Str.="<span class=disabled>>></span>";
$subPageCss2Str.="<span class=disabled>尾页</span>";
}
return $subPageCss2Str;
}
}
?>
<?php
class My_Lib_upfile{
var $upfile, $upfile_name, $upfile_size;
# $upfile 临时文件名 $_FILES['tmp_name'] ,$upfile_name 文件名 $_FILES['name'] ,$upfile_size 文件大小$_FILES['size'];
var $new_upfile_name; # 上传后的文件名称 ;
var $fleth, $fileExtent; # 文件扩展名(类型) ;
var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/;
var $filename; # 文件(带路径) ;
var $filepath; #相对路径用来删除文件;
var $maxSize, $File_type; # 允许上传文件的大小 允许上传文件的类型 ;
var $BuildFile,$newFile,$File_width,$File_height,$rate;
function upfileclass($upfile,$upfile_name,$upfile_size){
$this->upfile = $upfile;
$this->upfile_name = $upfile_name;
$this->upfile_size = $upfile_size;
$this->new_upfile_name = $this->CreateNewFilename($this->upfile_name);
$this->f1 = "public/upload/images";
$this->f2 = $this->f1."/".date('Y')."-".date('m');
$this->f3 = $this->f2."/".date('d');
$this->filename = $this->f3 . "/" . $this->new_upfile_name;
$this->maxSize = 5000*1024; # 文件大小 5000KB
$this->File_type = "gif/jpg/jpeg/png/bmp"; # 允许上传的文件类型
}
# 创建新文件名 (原文件名)
function CreateNewFilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileExtent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀;
$tmps教程tr = date('Ymd').rand(0,time()) . "." .$this->fileExtent; # 创建新文件名;
return $tmpstr;
}
# 检测文件类型是否正确
function chk_fileExtent(){
$iwTrue = 0;
$fle = explode("/",$this->File_type);
for($i=0; $i < count($fle); $i++){
if($this->fileExtent == $fle[$i]){
$iwTrue = (int) $iwTrue + 1;
}
}
if( $iwTrue == 0 ){
$this->msg("文件不符合 ".$this->File_type." 格式!");
}
}
# 提示错误信息并终止操作
function msg($Error){
echo "<script language="javascript教程"> ";
echo " alert('".$Error."'); ";
echo " window.history.back(); ";
echo "</script> ";
die();
}
# 保存文件
function savefile(){
$this->chk_fileExtent();
$this->chk_fileSize();
$this->CreateFolder( "./".$this->f1 );
$this->CreateFolder( "./".$this->f2 );
$this->CreateFolder( "./".$this->f3 );
return $this->chk_savefile();
}
# 检测上传结果是否成功
function chk_savefile(){
$copymsg = copy($this->upfile,"./".$this->filename);
if( $copymsg ){
return $this->filename;
}
else{
$this->msg("文件上传失败! 请重新上传! ");
}
}
# 创建文件夹
function CreateFolder($foldername){
if( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}
# 检测文件大小
function chk_fileSize(){
if( $this->upfile_size > $this->maxSize ){
$this->msg("目标文件不能大于". $this->maxSize/1024 ." KB");
}
}
# 删除文件($filePath 文件相对路径)
function Deletefile($filePath){
if( !is_file($filePath) ){
return false;
}
else{
$ending = @unlink($filePath);
return $ending;
}
}
}
?>
/*
函数:生成缩略图
MakeBuild("images/a.jpg","news/b.jpg","100");
参数:
echo $BuildFile; 原图 带路径
echo $newFile; 生成的缩略图 带路径
echo $File_width; 缩略图宽度值
echo $File_height; 缩略图高度值 (默认为宽度的比例值)
echo $rate; 缩略图象品质;
*/
function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=1000) {
if(!is_file($BuildFile)){
$this->msg("文件 ".$BuildFile." 不是一个有效的图形文件! 系统无法生成该文件的缩略图!");
return false;
}
$data = GetImageSize($BuildFile);
switch($data[2]){
case 1:
$im = @ImageCreateFromGIF($BuildFile);
break;
case 2:
$im = @ImageCreateFromJPEG($BuildFile);
break;
case 3:
$im = @ImageCreateFromPNG($BuildFile);
break;
}
if(!$im){
return false;
}
else{
$srcW = ImageSX($im); # 取得原图宽度;
$srcH = ImageSY($im); # 取得原图高度;
$dstX = 0;
$dstY = 0;
if($File_height==0){
$File_height = $File_width/$srcW*$srcH;
}
if ($srcW*$File_height>$srcH*$File_width){
$fFile_height = round($srcH*$File_width/$srcW);
$dstY = floor(($File_height-$fFile_height)/2);
$fFile_width = $File_width;
}
else {
$fFile_width = round($srcW*$File_height/$srcH);
$dstX = floor(($File_width-$fFile_width)/2);
$fFile_height = $File_height;
}
$ni = ImageCreateTrueColor($File_width,$File_height);
$dstX = ($dstX<0)?0:$dstX;
$dstY = ($dstX<0)?0:$dstY;
$dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
$dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
ImageJpeg($ni,$newFile,$rate); # 生成缩略图;
imagedestroy($im); # imagedestroy(resource) 释放image关联的内存
}
}
相关文章
- 我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
- 最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
- 有一种方法,可以不打开网站而直接查看到这个网站的源代码.. 这样可以有效地防止误入恶意网站... 在浏览器地址栏输入: view-source:http://...2016-09-20
- <?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实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
- 本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
- 本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
- php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
- php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p> </p> <p> </p> <form name="form1...2016-11-25
- 公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
- 其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
- 本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
- 一、日期减去天数等于第二个日期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
- 微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
- Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
- 本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
- 小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
php导出csv格式数据并将数字转换成文本的思路以及代码分享
php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07- ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25