专为新手写的结合smarty的类
更新时间:2006年12月02日 00:00:00 作者:
复制代码 代码如下:
<?php
// 分页类,使用时调用类下面的函数即可
class Page_class
{
//帖子总数
private $count = 0;
//每页显示多少行
private $rows;
//每页显示多少个跳转页号
private $link_num;
//共有多少页
private $pages = 0;
//当前页
private $current_page;
//开始记录
private $start = 0;
//上一大页
private $prve = 0;
//下一大页
private $next = 0;
//数字链接
private $links = array();
//返回值
public $return_rs = array();
public function __construct( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
//获取传入的帖子总数
$this->count = intval( $count );
//获取当前页
$this->current_page = intval( $current_page );
//显示多少行
$this->rows = intval( $rows );
//每页显示多少个跳转页号
$this->link_num = $link_num;
//调用计算页数的方法
$this->count_page();
//调用返回跳转页号的方法
$this->return_links();
//返回值
$this->return_rs = array(
'rows' => $this->rows,
'prve' => $this->prve,
'next' => $this->next,
'pages' => $this->pages,
'start' => $this->start,
'count' => $this->count,
'links' => $this->links,
'current_page' => $this->current_page
);
}
public function __destruct()
{
}
//计算页数
private function count_page()
{
//计算共有多少页
@$this->pages = ceil( $this->count / $this->rows );
//如果当前页大于最大页数,则使其等于最大页数;如果当前页小于1,则使其等于1
$this->current_page > $this->pages ? $this->current_page = $this->pages : true ;
$this->current_page < 1 ? $this->current_page = 1 : true;
//计算查询的开始记录数
$this->start = ( $this->current_page - 1 ) * $this->rows;
}
//返回页面跳转页号的函数
private function return_links()
{
//用当前页除以显示页数得到当前是第几“大页”
$start_s = floor( $this->current_page / $this->link_num );
//如果当前页号正好整除显示页数,则应该对$start_s减一,因为设想一下,如果当前是第7页
//显示页数也是7,则$start_s=1,也就是说已经到了第二“大页”了,而实际上它应该还是在
//第一“大页”
( $this->current_page % $this->link_num ) == 0 ? $start_s-- : true;
//计算当前“大页”开始页号,算法是(当前“大页”*显示页数)+1;例如0*7+1=1,1*7+1=8,2*7+1=15
$start_page = ( $start_s * $this->link_num ) + 1;
//上一大页
$this->prve = $start_page - 1;
//下一大页
$this->next = $start_page + $this->link_num;
//开始循环计算当前大页中的小页号
for ( $i=0; $i < $this->link_num; $i++ )
{
//如果下一个页号已经超出了总页数,则说明应该停止了
if ( $start_page + $i > $this->pages )
{
break;
}
//将页号记录在$this->links_arr数组中
$this->links[] = $start_page + $i;
}
}
}
function m_page( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
$page = new Page_class( $count, $current_page, $rows, $link_num );
return $page->return_rs;
}
?>
复制代码 代码如下:
<?php
// 文本操作函数
// 修改
function m_txt_replace( $pattern, $text, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$replace = "{$pattern_start}{$text}{$pattern_end}";
$new_content = str_replace( $match[0], $replace, $content );
return $new_content;
}
// 追加
function m_txt_add( $pattern, $text, $content )
{
$pattern = "<!--{$pattern}-->";
@$ok = preg_match( "{{$pattern}}Ssi", $content );
if ( $ok != true )
{
return false;
}
$add = "{$pattern}\n{$text}";
$new_content = str_replace( $pattern, $add, $content );
return $new_content;
}
// 删除
function m_txt_delete( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
$new_content = str_replace( $match[0], '', $content );
return $new_content;
}
//获取
function m_txt_get( $pattern, $content )
{
$pattern_start = "<!--$pattern-->";
$pattern_end = "<!--/$pattern-->";
@$ok = preg_match( "{{$pattern_start}.*{$pattern_end}}Ssi", $content, $match );
if ( $ok != true )
{
return false;
}
return $match[0];
}
?>
复制代码 代码如下:
<?php
// 上传函数
function m_up_file( $files, $up_url, $type, $max_size = 2097152 )
{
$i = 0;
if ( !is_array( $files ) )
{
die( '参数传递错误' );
}
$type_pattern = is_array( $type ) ? '\.(' . implode( ')|(', $type ) . ')' : "\.({$type})";
foreach ( $files as $key => $arr )
{
$ok = false;
if( $arr['error'] == 0 )
{
if ( !is_uploaded_file( $arr['tmp_name'] ) )
{
$err_msg .= "文件:<b>{$arr['name']}</b>不可上传<br>";
continue;
}
elseif ( $_FILES['up_file']['size'] > $max_size )
{
$err_msg .= "文件:<b>{$arr['name']}</b>上传失败,原因是:文件超过限定大小<br>";
continue;
}
elseif ( !preg_match( "!{$type_pattern}!Si", $arr['name'] ) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传失败,原因是:格式不正确<br>";
continue;
}
else
{
$txt = substr( str_shuffle( 'abcdefghijklmnopqrstuvwxyz' ), -4 );
$hz = strtolower( strstr( $arr['name'], '.' ) );
$new_name = date( 'YmdHis' ) . $txt . $hz;
if ( !is_array( $up_url ) )
{
!preg_match( '!\/$!', $up_url ) ? $up_url .= '/' : true;
$new_url = $up_url . $new_name;
}
else
{
$key = str_replace( '.', '', $hz );
$up_url = array_change_key_case( $up_url, CASE_LOWER );
!preg_match( '!\/$!', $up_url[$key] ) ? $up_url[$key] .= '/' : true;
$new_url = $up_url[$key] . $new_name;
}
@$ok = move_uploaded_file( $arr['tmp_name'], THIS_DIR . $new_url );
}
}
if ( $ok == true )
{
$rs[$i]['url'] = $new_url;
$rs[$i]['name'] = $arr['name'];
$rs[$i]['type'] = strtoupper( str_replace( '.', '', $hz ) );
$i++;
}
elseif( !empty($arr['name']) )
{
$err_msg .= "文件<b>{$arr['name']}</b>上传出错<br>";
continue;
}
}
return array( 'arr' => $rs, 'err_msg' => $err_msg, 'num' => $i );
}
?>
相关文章
详解WordPress中调用评论模板和循环输出评论的PHP函数
这篇文章主要介绍了WordPress中调用评论模板和循环输出评论的PHP函数,分别是comments_template函数与wp_list_comments函数的使用,需要的朋友可以参考下2016-01-01
最新评论