/**
* 讀取緩存,默認為文件緩存,不加載緩存配置。
* @param string $name 緩存名稱
* @param $filepath 數據路徑(模塊名稱) caches/cache_$filepath/
* @param string $config 配置名稱
*/
function getcacheinfo($name, $filepath='', $type='file', $config='') {
pc_base::load_sys_class('cache_factory');
if($config) {
$cacheconfig = pc_base::load_config('cache');
$cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
} else {
$cache = cache_factory::get_instance()->get_cache($type);
}
return $cache->cacheinfo($name, '', '', $filepath);
}
/**
* 生成sql語句,如果傳入$in_cloumn 生成格式為 IN('a', 'b', 'c')
* @param $data 條件數組或者字符串
* @param $front 連接符
* @param $in_column 字段名稱
* @return string
*/
function to_sqls($data, $front = ' AND ', $in_column = false) {
if($in_column && is_array($data)) {
$ids = '\''.implode('\',\'', $data).'\'';
$sql = "$in_column IN ($ids)";
return $sql;
} else {
if ($front == '') {
$front = ' AND ';
}
if(is_array($data) && count($data) > 0) {
$sql = '';
foreach ($data as $key => $val) {
$sql .= $sql ? " $front `$key` = '$val' " : " `$key` = '$val' ";
}
return $sql;
} else {
return $data;
}
}
}
/**
* 分頁函數
*
* @param $num 信息總數
* @param $curr_page 當前分頁
* @param $perpage 每頁顯示數
* @param $urlrule URL規則
* @param $array 需要傳遞的數組,用於增加額外的方法
* @return 分頁
*/
function pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
if(defined('URLRULE') && $urlrule == '') {
$urlrule = URLRULE;
$array = $GLOBALS['URL_ARRAY'];
} elseif($urlrule == '') {
$urlrule = url_par('page={$page}');
}
$multipage = '';
if($num > $perpage) {
$page = $setpages+1;
$offset = ceil($setpages/2-1);
$pages = ceil($num / $perpage);
if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
$from = $curr_page - $offset;
$to = $curr_page + $offset;
$more = 0;
if($page >= $pages) {
$from = 2;
$to = $pages-1;
} else {
if($from <= 1) {
$to = $page-1;
$from = 2;
} elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
$multipage .= '<a class="a1">'.$num.L('page_item').'</a>';
if($curr_page>0) {
$multipage .= ' <a href="'.pageurl($urlrule, $curr_page-1, $array).'" class="a1">'.L('previous').'</a>';
if($curr_page==1) {
$multipage .= ' <span>1</span>';
} elseif($curr_page>6 && $more) {
$multipage .= ' <a href="'.pageurl($urlrule, 1, $array).'">1</a>..';
} else {
$multipage .= ' <a href="'.pageurl($urlrule, 1, $array).'">1</a>';
}
}
for($i = $from; $i <= $to; $i++) {
if($i != $curr_page) {
$multipage .= ' <a href="'.pageurl($urlrule, $i, $array).'">'.$i.'</a>';
} else {
$multipage .= ' <span>'.$i.'</span>';
}
}
if($curr_page<$pages) {
if($curr_page<$pages-5 && $more) {
$multipage .= ' ..<a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
} else {
$multipage .= ' <a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
}
} elseif($curr_page==$pages) {
$multipage .= ' <span>'.$pages.'</span> <a href="'.pageurl($urlrule, $curr_page, $array).'" class="a1">'.L('next').'</a>';
} else {
$multipage .= ' <a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
}
}
return $multipage;
}
摘自 chaojie2009的專欄