Home > Article > Backend Development > PHP common function collection (2)_PHP tutorial
/**
* Read cache, the default is file cache, no cache configuration is loaded.
* @param string $name cache name
* @param $filepath data path (module name) caches/cache_$filepath/
* @param string $config configuration name
*/
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);
}
/**
* Generate sql statement. If $in_cloumn is passed in, the generated format is IN('a', 'b', 'c')
* @param $data conditional array or string
* @param $front connector
* @param $in_column field name
* @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;
}
}
}
/**
*Paging function
*
* @param $num total number of messages
* @param $curr_page current page
* @param $perpage Number of displays per page
* @param $urlrule URL rule
* @param $array The array that needs to be passed, used to add additional methods
* @return Pagination
*/
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 .= ''.$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 <= $to; $i++) {
if($i != $curr_page) {
$multipage .= ' '.$i.'';
} else {
$multipage .= ' '.$i.'';
}
}
if($curr_page<$pages) {
if($curr_page<$pages-5 && $more) {
$multipage .= ' ..'.$pages.' '.L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
} elseif($curr_page==$pages) {
$multipage .= ' '.$pages.' ' .L('next').'';
} else {
$multipage .= ' '.$pages.' '.L('next').'';
}
}
Return $multipage;
}
Excerpted from chaojie2009’s column