時(shí)間:2023-09-25 20:06:01 | 來源:網(wǎng)站運(yùn)營
時(shí)間:2023-09-25 20:06:01 來源:網(wǎng)站運(yùn)營
專家動(dòng)態(tài)頁面的實(shí)現(xiàn)——php基于CI框架的學(xué)習(xí)(二):以下是本次學(xué)習(xí)的頁面class Expert extends CI_Controller{}
在Expert類里定義了幾個(gè)參數(shù)以及說明其使用了哪些modelfunction __construct() { $this->whitelist = "index"; parent::__construct ( ); $this->load->model ( 'category_model' ); $this->load->model ( "expert_model" );}
然后我們看function index() 里的代碼,一行行看下來$navtitle = "問題專家";$cid = intval ( $this->uri->segment ( 3 ) ) ? $this->uri->segment ( 3 ) : 'all'; //分類id$status = null!== $this->uri->segment ( 4 ) ? $this->uri->segment ( 4 ) : 'all'; //排序
可以在頁面中一一對應(yīng)if ($cid != 'all') { $category = $this->category [$cid]; //得到分類信息 $navtitle = $category ['name'] . "專家列表"; $cfield = 'cid' . $category ['grade'];//獲取分類的等級(jí)}
第二行語句有點(diǎn)難以理解,選中后面的category查看引用,可以發(fā)現(xiàn)當(dāng)前category是來自System/core目錄下文件controller.php的全局變量。private static $instance;var $cache;var $currentuid = array ();var $setting = array ();var $category = array ();var $usergroup = array ();var $whitelist;var $time;var $ip;
同時(shí)再選中$category右鍵查看快速引用,會(huì)找到下面幾行代碼$this->load->database ();$category = $this->category = $this->cache->load ( 'category', 'id', 'displayorder' );
訪問緩存數(shù)據(jù),通過load()獲取數(shù)據(jù)庫里,名字為“category”的數(shù)據(jù)表里的id和displayorder字段的數(shù)據(jù),以下是load函數(shù)的源代碼function fromcache($cachename,$cachetime = 3){}
總之,由上方可以得出全局變量$category中讀取的是數(shù)據(jù)庫某條數(shù)據(jù),源代碼$category = $this->category [$cid]; //得到分類信息
意為:Expert新定義的$category 為 db.category.id = cid 的整條數(shù)據(jù)庫的信息//上接源代碼,即$cid == 'all'的情況else { $category ['name'] = ''; $category ['id'] = 'all'; $cfield = ''; $category ['pid'] = 0;}if ($cid != 'all') $category = $this->category_model->get ( $cid );$sublist = $this->category_model->list_by_cid_pid ( $cid, $category ['pid'] ); //獲取子分類
此段代碼包含的函數(shù):function list_by_cid_pid($cid, $pid) { $cid=intval($cid); $pid=intval($pid); $sublist = array (); //把cid,pid值等于'all'的分類設(shè)置為平級(jí)的分類 if ($cid == 'all') { $cid = 0; } if ($pid == 'all') { $pid = 0; } $where=" and onlybackground!=1 "; //在數(shù)據(jù)表里尋找與函數(shù)參數(shù)pid相同的分類id //使pid分類為id的子分類 $query = $this->db->query ( "select * from " . $this->db->dbprefix . "category where pid=$cid and isuseask=1 $where order by displayorder asc,id asc" ); //給子分類添加封面縮略圖和大圖 foreach ( $query->result_array () as $category ) { $category ['image'] = get_cid_dir ( $category ['id'], 'big' ); $category ['bigimage'] = get_cid_dir ( $category ['id'], 'big' ); $sublist [] = $category; } return $sublist;}
我們可以訪問相關(guān)的數(shù)據(jù)庫來//判斷網(wǎng)頁是否是付費(fèi)內(nèi)容$orderwhere = '';switch ($status) { case 'all' : //全部 $orderwhere = ''; break; case '1' : //付費(fèi) $orderwhere = ' and mypay>0 '; break; case '2' : //免費(fèi) $orderwhere = " and mypay=0 "; break; default: $orderwhere = ''; break;}$page = max ( 1, intval ( $this->uri->segment ( 5 ) ) );$pagesize = $this->setting ['list_default'];$startindex = ($page - 1) * $pagesize;
$page = max ( 1, intval ( $this->uri->segment ( 5 ) ) );令當(dāng)前頁面的page為$this->uri->segment ( 5 )返回的整數(shù)值,如果不存在該地址就返回1,此網(wǎng)頁返回1,因?yàn)樽疃嘀挥兴膫€(gè)分頁//冗長的,本頁面最后一個(gè)判斷語句$rownum = $cid == 'all' ? returnarraynum ( $this->db->query ( getwheresql ( 'user', " expert=1 " . $orderwhere , $this->db->dbprefix ) )->row_array () ) : returnarraynum ( $this->db->query ( getwheresql ( 'user', " expert=1 " . $orderwhere . "and uid IN (SELECT uid FROM " . $this->db->dbprefix . "user_category WHERE cid=$cid)" , $this->db->dbprefix ) )->row_array () );
該語句中調(diào)用了以下方法//展示專家列表,get_list獲取user數(shù)據(jù)表所有專家用戶id//并判斷他是否認(rèn)證,最后一次登陸時(shí)間,關(guān)注人數(shù),被關(guān)注次數(shù),個(gè)人擅長分類$expertlist = $this->expert_model->get_list ( 1, $startindex, $pagesize, $cid, $status );//通過page函數(shù),以分類的id號(hào),是否免費(fèi)重新建立兩個(gè)分頁$departstr = page ( $rownum, $pagesize, $page, "expert/default/$cid/$status" );//使當(dāng)前頁面,每一頁面最多可處理15條數(shù)據(jù)$questionlist = $this->expert_model->get_solves ( 0, 15 );//代碼的頁面實(shí)現(xiàn)include template ( 'expert' );
關(guān)鍵詞:學(xué)習(xí),動(dòng)態(tài),實(shí)現(xiàn),專家
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。