1. 程式人生 > >Phpcms V9.5.4呼叫最新文章列表和多欄目呼叫

Phpcms V9.5.4呼叫最新文章列表和多欄目呼叫

PHPCMS呼叫預設表最新文章有一下兩種方法:

1. get標籤形式呼叫

2. 修改標籤程式碼實現最新文章呼叫

下面我們看看具體是實現方法:

第一種方法 用GET標籤呼叫

這種方法不用修改PHPCMS的核心程式碼,就可以達到模型表的最新資料呼叫,具體程式碼片段如下:

{pc:get sql="SELECT * from v9_news where status=99 and catid in(21,22,23,24,25) order by id DESC" start=="7" num="7" return="data"}
{loop $data $r}
<li><a href="{$r[url]}" title="{$r[title]}" target="_blank">{str_cut($r['title'],40)}({date('m-d',$r[inputtime])})</span></a></li>
{/loop}
{/pc}

第二種方法 修改標籤程式碼

PHPCMS的標籤預設只能呼叫單個主欄目下面的子欄目的文章,如果不填寫欄目則無法呼叫資料,填寫多個欄目ID只調取首個欄目ID的文章,為什麼這麼說呢?看看程式碼就知道原因所在了。

	/**
	 * 列表頁標籤
	 * @param $data
	 */
	public function lists($data) {
		$catid = intval($data['catid']);
		if(!$this->set_modelid($catid)) return false;
		if(isset($data['where'])) {
			$sql = $data['where'];
		} else {
			$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
			if($this->category[$catid]['child']) {
				$catids_str = $this->category[$catid]['arrchildid'];
				$pos = strpos($catids_str,',')+1;
				$catids_str = substr($catids_str, $pos);
				$sql = "status=99 AND catid IN ($catids_str)".$thumb;
			} else {
				$sql = "status=99 AND catid='$catid'".$thumb;
			}
		}
		$order = $data['order'];

		$return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');
						
		//呼叫副表的資料
		if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {
			$ids = array();
			foreach ($return as $v) {
				if (isset($v['id']) && !empty($v['id'])) {
					$ids[] = $v['id'];
				} else {
					continue;
				}
			}
			if (!empty($ids)) {
				$this->db->table_name = $this->db->table_name.'_data';
				$ids = implode('\',\'', $ids);
				$r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');
				if (!empty($r)) {
					foreach ($r as $k=>$v) {
						if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);
					}
				}
			}
		}
		return $return;
	}

修改為新增所以欄目ID來查詢全表資料。這樣和GET方法幾乎沒什麼區別,只是標籤形式不同而已

	/**
	 * 列表頁標籤
	 * @param $data
	 */
	public function lists($data) {
		$catid = $data['catid'];
		$cid = intval( $catid );
		if(!$this->set_modelid($cid)) return false;
		if(isset($data['where'])) {
			$sql = $data['where'];
		} else {
			$thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
			if($cat_array=explode(',',$catid) ){  
				$catids_str = $catid;  
				foreach($cat_array as $k => $v){  
					if($this->category[$v]['child']) {  
						$catids_str .= ','.$this->category[$v]['arrchildid'];  
					}  
				}  
				$sql = "status=99 AND catid IN ($catids_str)".$thumb;  
			}else {
				$sql = "status=99 AND catid='$catid'".$thumb;
			}
		}
		$order = $data['order'];

		$return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');
						
		//呼叫副表的資料
		if (isset($data['moreinfo']) && intval($data['moreinfo']) == 1) {
			$ids = array();
			foreach ($return as $v) {
				if (isset($v['id']) && !empty($v['id'])) {
					$ids[] = $v['id'];
				} else {
					continue;
				}
			}
			if (!empty($ids)) {
				$this->db->table_name = $this->db->table_name.'_data';
				$ids = implode('\',\'', $ids);
				$r = $this->db->select("`id` IN ('$ids')", '*', '', '', '', 'id');
				if (!empty($r)) {
					foreach ($r as $k=>$v) {
						if (isset($return[$k])) $return[$k] = array_merge($v, $return[$k]);
					}
				}
			}
		}
		return $return;
	}

標籤呼叫形式,預設第一個欄目ID為模型表:
{pc:content action="lists" catid="1,2,3,4,5" order="listorder DESC,inputtime DESC,id DESC" moreinfo="1"  start="7" num="7"}
 {loop $data $k $v} 
  <li><a href="{$v[url]}" title="{$v[title]}" target="_blank"{title_style($v[style])}>{str_cut($v[title], 100)}</a></li>
 {/loop}
{/pc}