一、做phpcms-v9二次開發時,我們經常需要用到如下代碼,所以有必須在這里注釋說明一下
defined('IN_PHPCMS') or exit('No permission resources.');
//第一步:獲取模型緩存路徑
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
pc_base::load_app_func('util','content');
class index {
private $db;
function __construct() {
//第二步:獲取與數據庫相關的配置信息,如:數據表前綴、數據庫名、數據庫用戶名、數據庫密碼、數據庫編碼、數據庫類型
$this->db = pc_base::load_model('content_model');
$this->_userid = param::get_cookie('_userid');
$this->_username = param::get_cookie('_username');
$this->_groupid = param::get_cookie('_groupid');
}
//首頁
public function init() {
if(isset($_GET['siteid'])) {
$siteid = intval($_GET['siteid']);
} else {
$siteid = 1;//模型情況下siteid為1
}
$siteid = $GLOBALS['siteid'] = max($siteid,1);
define('SITEID', $siteid);
$_userid = $this->_userid;
$_username = $this->_username;
$_groupid = $this->_groupid;
//SEO
$SEO = seo($siteid);
$sitelist = getcache('sitelist','commons');
$default_style = $sitelist[$siteid]['default_style'];
$CATEGORYS = getcache('category_content_'.$siteid,'commons');
include template('content','index',$default_style);
}
//內容頁
public function show() {
$catid = intval($_GET['catid']);
$id = intval($_GET['id']);
if(!$catid || !$id) showmessage(L('information_does_not_exist'),'blank');
$_userid = $this->_userid;
$_username = $this->_username;
$_groupid = $this->_groupid;
$page = intval($_GET['page']);
$page = max($page,1);
$siteids = getcache('category_content','commons');//獲取各欄目所對應的站點id
$siteid = $siteids[$catid];//獲取當前欄目所對應的站點id
$CATEGORYS = getcache('category_content_'.$siteid,'commons');//獲取當前站點下所有欄目的配置信息
if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0) showmessage(L('information_does_not_exist'),'blank');
$this->category = $CAT = $CATEGORYS[$catid];//獲取當前站點下當前欄目的配置信息
$this->category_setting = $CAT['setting'] = string2array($this->category['setting']);
$siteid = $GLOBALS['siteid'] = $CAT['siteid'];//獲取當前站點下當前欄目所對應的站點id值
$MODEL = getcache('model','commons');//獲取各個模型的配置信息
$modelid = $CAT['modelid'];//獲取當前站點下當前欄目所屬的模型id,找到了模型,也就找到了要查詢的模型表(數據表)
//設置模型表:通過緩存文件獲取modelid,然后再通過模型modelid獲取對應的模型表及對應的模型附表
$tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];
//設置模型附表
$r = $this->db->get_one(array('id'=>$id));
if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
//下面代碼獲取的是當前模型附表中數據
$this->db->table_name = $tablename.'_data';
//下面代碼獲取的是當前模型附表中數據
$r2 = $this->db->get_one(array('id'=>$id));
//將模型表數據和模型附表中數據合并在一起
$rs = $r2 ? array_merge($r,$r2) : $r;
//再次重新賦值,以數據庫為準
$catid = $CATEGORYS[$r['catid']]['catid'];
$modelid = $CATEGORYS[$catid]['modelid'];
require_once CACHE_MODEL_PATH.'content_output.class.php';
$content_output = new content_output($modelid,$catid,$CATEGORYS);
//將查詢出來的數據經模型處理函數處理后放入$data數據中
$data = $content_output->get($rs);
//注意:這行代碼非常之重要,主要用來將數組中各個元素轉化為變量,數據元素的鍵名為變量名,數據元素的值為變量值
extract($data);