php 常用函数收藏(二)
程序员文章站
2024-04-04 09:46:41
...
/**
* 读取缓存,默认为文件缓存,不加载缓存配置。
* @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 $to = $page-1;
$from = 2;
} elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
$multipage .= ''.$num.L('page_item').'';
if($curr_page>0) {
$multipage .= ' '.L('previous').'';
if($curr_page==1) {
$multipage .= ' 1';
} elseif($curr_page>6 && $more) {
$multipage .= ' 1..';
} else {
$multipage .= ' 1';
}
}
for($i = $from; $i if($i != $curr_page) {
$multipage .= ' '.$i.'';
} else {
$multipage .= ' '.$i.'';
}
}
if($curr_page if($curr_page $multipage .= ' ..'.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
} elseif($curr_page==$pages) {
$multipage .= ' '.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
return $multipage;
}
摘自 chaojie2009的专栏
* 读取缓存,默认为文件缓存,不加载缓存配置。
* @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 $to = $page-1;
$from = 2;
} elseif($to >= $pages) {
$from = $pages-($page-2);
$to = $pages-1;
}
$more = 1;
}
$multipage .= ''.$num.L('page_item').'';
if($curr_page>0) {
$multipage .= ' '.L('previous').'';
if($curr_page==1) {
$multipage .= ' 1';
} elseif($curr_page>6 && $more) {
$multipage .= ' 1..';
} else {
$multipage .= ' 1';
}
}
for($i = $from; $i if($i != $curr_page) {
$multipage .= ' '.$i.'';
} else {
$multipage .= ' '.$i.'';
}
}
if($curr_page if($curr_page $multipage .= ' ..'.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
} elseif($curr_page==$pages) {
$multipage .= ' '.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
return $multipage;
}
摘自 chaojie2009的专栏