專為新手寫的結合smarty的類第3/3頁
更新時間:2006年12月02日 00:00:00 作者:
復制代碼 代碼如下:
<?php
// 分頁類,使用時調用類下面的函數(shù)即可
class Page_class
{
//帖子總數(shù)
private $count = 0;
//每頁顯示多少行
private $rows;
//每頁顯示多少個跳轉頁號
private $link_num;
//共有多少頁
private $pages = 0;
//當前頁
private $current_page;
//開始記錄
private $start = 0;
//上一大頁
private $prve = 0;
//下一大頁
private $next = 0;
//數(shù)字鏈接
private $links = array();
//返回值
public $return_rs = array();
public function __construct( $count, $current_page = 1, $rows = 10, $link_num = 7 )
{
//獲取傳入的帖子總數(shù)
$this->count = intval( $count );
//獲取當前頁
$this->current_page = intval( $current_page );
//顯示多少行
$this->rows = intval( $rows );
//每頁顯示多少個跳轉頁號
$this->link_num = $link_num;
//調用計算頁數(shù)的方法
$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()
{
}
//計算頁數(shù)
private function count_page()
{
//計算共有多少頁
@$this->pages = ceil( $this->count / $this->rows );
//如果當前頁大于最大頁數(shù),則使其等于最大頁數(shù);如果當前頁小于1,則使其等于1
$this->current_page > $this->pages ? $this->current_page = $this->pages : true ;
$this->current_page < 1 ? $this->current_page = 1 : true;
//計算查詢的開始記錄數(shù)
$this->start = ( $this->current_page - 1 ) * $this->rows;
}
//返回頁面跳轉頁號的函數(shù)
private function return_links()
{
//用當前頁除以顯示頁數(shù)得到當前是第幾“大頁”
$start_s = floor( $this->current_page / $this->link_num );
//如果當前頁號正好整除顯示頁數(shù),則應該對$start_s減一,因為設想一下,如果當前是第7頁
//顯示頁數(shù)也是7,則$start_s=1,也就是說已經到了第二“大頁”了,而實際上它應該還是在
//第一“大頁”
( $this->current_page % $this->link_num ) == 0 ? $start_s-- : true;
//計算當前“大頁”開始頁號,算法是(當前“大頁”*顯示頁數(shù))+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;
//開始循環(huán)計算當前大頁中的小頁號
for ( $i=0; $i < $this->link_num; $i++ )
{
//如果下一個頁號已經超出了總頁數(shù),則說明應該停止了
if ( $start_page + $i > $this->pages )
{
break;
}
//將頁號記錄在$this->links_arr數(shù)組中
$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
// 文本操作函數(shù)
// 修改
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
// 上傳函數(shù)
function m_up_file( $files, $up_url, $type, $max_size = 2097152 )
{
$i = 0;
if ( !is_array( $files ) )
{
die( '參數(shù)傳遞錯誤' );
}
$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 );
}
?>
相關文章
使用PHP socke 向指定頁面提交數(shù)據(jù)
一直以為有了ajax別人網站的數(shù)據(jù)就可以拿過來用,這也是我這幾天想的一個方像,但是用了firefox測試之后,現(xiàn)在不能,2008-07-07PHP中使用xmlreader讀取xml數(shù)據(jù)示例
這篇文章主要介紹了PHP中使用xmlreader讀取xml數(shù)據(jù)示例,本文示例相對簡單,只包含了一個讀取功能,需要的朋友可以參考下2014-12-12php自定義函數(shù)call_user_func和call_user_func_array詳解
看UCenter的時候有一個函數(shù)call_user_func,百思不得其解,因為我以為是自己定義的函數(shù),結果到處都找不到,后來百度了一下才知道call_user_func是內置函數(shù)2011-07-07PHP通過get方法獲得form表單數(shù)據(jù)方法總結
這篇文章我們給大家介紹了PHP如何通過get的方式來得到獲取form表單數(shù)據(jù)的方法,有需要的朋友們參考下。2018-09-09