一、做phpcms-v9二次開發(fā)時(shí),我們經(jīng)常需要用到如下代碼,所以有必須在這里注釋說明一下
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() {
//第二步:獲取與數(shù)據(jù)庫相關(guān)的配置信息,如:數(shù)據(jù)表前綴、數(shù)據(jù)庫名、數(shù)據(jù)庫用戶名、數(shù)據(jù)庫密碼、數(shù)據(jù)庫編碼、數(shù)據(jù)庫類型
$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);
}
//內(nèi)容頁
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');//獲取各欄目所對(duì)應(yīng)的站點(diǎn)id
$siteid = $siteids[$catid];//獲取當(dāng)前欄目所對(duì)應(yīng)的站點(diǎn)id
$CATEGORYS = getcache('category_content_'.$siteid,'commons');//獲取當(dāng)前站點(diǎn)下所有欄目的配置信息
if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0) showmessage(L('information_does_not_exist'),'blank');
$this->category = $CAT = $CATEGORYS[$catid];//獲取當(dāng)前站點(diǎn)下當(dāng)前欄目的配置信息
$this->category_setting = $CAT['setting'] = string2array($this->category['setting']);
$siteid = $GLOBALS['siteid'] = $CAT['siteid'];//獲取當(dāng)前站點(diǎn)下當(dāng)前欄目所對(duì)應(yīng)的站點(diǎn)id值
$MODEL = getcache('model','commons');//獲取各個(gè)模型的配置信息
$modelid = $CAT['modelid'];//獲取當(dāng)前站點(diǎn)下當(dāng)前欄目所屬的模型id,找到了模型,也就找到了要查詢的模型表(數(shù)據(jù)表)
//設(shè)置模型表:通過緩存文件獲取modelid,然后再通過模型modelid獲取對(duì)應(yīng)的模型表及對(duì)應(yīng)的模型附表
$tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];
//設(shè)置模型附表
$r = $this->db->get_one(array('id'=>$id));
if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
//下面代碼獲取的是當(dāng)前模型附表中數(shù)據(jù)
$this->db->table_name = $tablename.'_data';
//下面代碼獲取的是當(dāng)前模型附表中數(shù)據(jù)
$r2 = $this->db->get_one(array('id'=>$id));
//將模型表數(shù)據(jù)和模型附表中數(shù)據(jù)合并在一起
$rs = $r2 ? array_merge($r,$r2) : $r;
//再次重新賦值,以數(shù)據(jù)庫為準(zhǔn)
$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);
//將查詢出來的數(shù)據(jù)經(jīng)模型處理函數(shù)處理后放入$data數(shù)據(jù)中
$data = $content_output->get($rs);
//注意:這行代碼非常之重要,主要用來將數(shù)組中各個(gè)元素轉(zhuǎn)化為變量,數(shù)據(jù)元素的鍵名為變量名,數(shù)據(jù)元素的值為變量值
extract($data);