本文简单介绍了怎样在phpcms制作的网站中调用全站的文章点击数,且自动按序排行.
首先我们需要打开phpcms的modules文件夹、content文件夹、classes文件夹中的content_tag.class.php文件
然后找个位置或在函数结尾加入如下新的自定义的allhits函数通过前台模中action="allhits"来调用.
代码如下:
/*
* sitehits站点点击排行
*/
function allhits($data){
if(emptyempty($data['siteid'])){
return false;
}
$siteid = intval($data['siteid']);
$this->hits_db = pc_base::load_model('hits_model');
$catco = getcache('category_content','commons');
$catid = '';
//获取站点下所有栏目ID
foreach($catco as $key=>$val){
if($val==$siteid){
$catid .= $comma.$key;
$comma=',';
}
}
//获取点击排行
$hqnum = $this->hits_db->select('catid in('.$catid.')','hitsid',$data['limit'],$data['order']);
$hitsarr = array();
$hitdb = pc_base::load_model('sitemodel_model');
$this->db_config = pc_base::load_config('database');
$tablepre = $this->db_config['default']['tablepre'];
foreach($hqnum as $hqnum_r){
preg_match_all('/-(d+)-/',$hqnum_r['hitsid'],$modelid);
$id = substr($hqnum_r['hitsid'],(strpos($hqnum_r['hitsid'],'-',2)+1));
$tbname = $hitdb->get_one(array('modelid'=>$modelid[1][0]),'tablename');
$this->db->table_name = $tablepre.$tbname['tablename'];
$hitsarr[] = array_merge($hitsarr,$this->db->get_one(array('id'=>$id)));
}
return $hitsarr;
}
保存修改好的文件,然后在需要调用全站点击排行的模板中加入以下调用:
代码如下:
{pc:content action="allhits" siteid="1" num="9" order="views DESC"}
{loop $data $r}
<a href="{$r[url]}" target="_blank">{$r[title]}</a>
{/loop}
{/pc}
然后就可以很便捷的随意在模板页面中调用全站点击排行的数据列表了.