【phpcms-v9】獲取當前欄目下周點擊量最高的三篇帶縮略圖的文章
1.content_tag.class.php文件中hits()函數改造為:
- /**
- * 排行榜標簽:len-控制顯示條數 thumb-顯示帶縮略圖
- * @param $data
- */
- public function hits($data) {
- $catid = intval($data['catid']); //欄目id,pc標簽的catid屬性
- if(!$this->set_modelid($catid)) return false; //根據欄目id->對應的模型id->模型表,如:news
- $data['limit'] = (isset($data['limitt'])&& (!emptyempty($data['limitt'])))?$data['limitt']:"";//weiyanhui添加
- $this->hits_db = pc_base::load_model('hits_model'); //hits:點擊量表
- $sql = $desc = $ids = '';
- $array = $ids_array = array();
- $order = $data['order']; //pc標簽中order屬性
- $hitsid = 'c-'.$this->modelid.'-%'; //hits點擊量表中hitsid字段的組成:c-模型id-文章id
- $sql = "hitsid LIKE '$hitsid'"; //拼接sql
- if(isset($data['day'])) { //pc標簽中day屬性:表示調用多少天內的排行
- $updatetime = SYS_TIME-intval($data['day'])*86400; //表示調用多少天內的排行
- $sql .= " AND updatetime>'$updatetime'"; //拼接sql
- }
- if($this->category[$catid]['child']) { //當前欄目是否有子欄目
- $catids_str = $this->category[$catid]['arrchildid']; //當前欄目下所有子欄目id,包括當前欄目自身的id
- $pos = strpos($catids_str,',')+1;
- $catids_str = substr($catids_str, $pos); //當前欄目下所有子欄目id,不包括當前欄目自身id
- $sql .= " AND catid IN ($catids_str)"; //拼接sql
- } else {
- $sql .= " AND catid='$catid'"; //如果當前欄目不存在子欄目
- }
- $hits = array();
- $result = $this->hits_db->select($sql, '*', $data['limit'], $order);//查詢v9_hits點擊量表
- foreach ($result as $r) { //$result:v9_hits表中返回的數據
- $pos = strpos($r['hitsid'],'-',2) + 1; //格式:c-模型id-文章id,如,c-1-1 ,查找第二個 "-"的位置
- $ids_array[] = $id = substr($r['hitsid'],$pos); //文章的id, $ids_array[]:文章id數組
- $hits[$id] = $r; //每篇文章的點擊量信息 ,格式:$hits[文章id]= 文章點擊記錄信息
- }
- $ids = implode(',', $ids_array); //將文章id數組以逗號拼接成一個字符串
- if($ids) {
- $sql = "status=99 AND id IN ($ids)"; //拼接sql
- } else {
- $sql = '';
- }
- $this->db->table_name = $this->tablename; //內容主表:v9_news
- $result = $this->db->select($sql, '*', $data['limit'],'','','id'); //查詢內容主表:v9_news
- foreach ($ids_array as $id) { //$ids_array:hits表中查詢出來的文章id數組,id是排好順序的
- if($data['thumb']!=""){ //只查詢帶縮略圖的文章,weiyanhui添加
- if($result[$id]['title']!='' && $result[$id]['thumb']!='') {
- $array[$id] = $result[$id];
- $array[$id] = array_merge($array[$id], $hits[$id]); //將內容主表v9_news中數據與點擊量表v9_hits中數據合并,合并橋梁為id
- }
- }else{ //有縮略圖和無縮略圖的文章都查詢
- if($result[$id]['title']!='') {
- $array[$id] = $result[$id];
- $array[$id] = array_merge($array[$id], $hits[$id]); //將內容主表v9_news中數據與點擊量表v9_hits中數據合并,合并橋梁為id
- }
- }
- }
- //weiyanhui開始
- if(isset($data['len'])){//截取長度
- //獲取數組中的前len條數據
- $array=array_slice($array,0, intval($data['len']));
- } //開源軟件:Vevb.com
- //weiyanhui結束
- //echo "<br/>";
- //print_r($array);
- return $array;
- }
2.模板中調用:
- <div class="b_bianjituijian_c">
- <!-- 當前欄目下周點擊率最高的三篇文章 -->
- {pc:content action="hits" catid="$catid" thumb="1" len="3" order="weekviews DESC"}
- {loop $data $r}
- <dl class="b_bianjituijian_cf">
- <dt><a href="{$r['url']}"><img src="{$r['thumb']}" width="60" height="60" alt="{$r['title']}"/></a></dt>
- <dd>
- <h3><a href="{$r['url']}">{str_cut1($r[title],5,'...')}</a></h3>
- <p>
- {str_cut($r[description],70,'...')}
- <a href="{$r['url']}">【詳細】</a>
- </p>
- </dd>
- </dl>
- <span class="border-bx"></span>
- {/loop}
- {/pc}
- </div>
新聞熱點
疑難解答