然后我們就在項目文件里找到同名文件開始艱難晦澀地學(xué)習(xí)

<?phpdefined ( &#39;BASEPATH&#39;" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 用戶動態(tài)頁面的實現(xiàn)——php基于CI框架的學(xué)習(xí)(一)

用戶動態(tài)頁面的實現(xiàn)——php基于CI框架的學(xué)習(xí)(一)

時間:2023-07-22 23:54:02 | 來源:網(wǎng)站運營

時間:2023-07-22 23:54:02 來源:網(wǎng)站運營

用戶動態(tài)頁面的實現(xiàn)——php基于CI框架的學(xué)習(xí)(一):最近在艱難晦澀地學(xué)習(xí)php,萬事開頭難,所以筆者決定從最簡單的頁面開始學(xué)習(xí)

然后我們就在項目文件里找到同名文件開始艱難晦澀地學(xué)習(xí)

<?phpdefined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );class Doing extends CI_Controller { function __construct() { parent::__construct(); //調(diào)用doing_model模型 $this->load->model("doing_model"); $this->load->model("topic_model"); } function index() { $navtitle = "問答動態(tài)"; $type = 'atentto'; //獲取第三分段的參數(shù),無參數(shù)就是默認(rèn)界面 $recivetype = $this->uri->segment ( 3 ); if ($recivetype) { $type = $recivetype; } if (!$this->user['uid']) { $type = 'all'; } $navtitletable = array( 'all' => '問答動態(tài)', 'my' => '我的動態(tài)', 'atentto' => '關(guān)注的動態(tài)' //default ); $navtitle = $navtitletable[$type];? //分頁設(shè)置 $page = max(1, intval($this->uri->segment ( 4 ))); $pagesize = $this->setting['list_default']; $startindex = ($page - 1) * $pagesize;? $doinglist = $this->doing_model->list_by_type($type, $this->user['uid'], $startindex, $pagesize); $rownum = $this->doing_model->rownum_by_type($type, $this->user['uid']); $departstr = page($rownum, $pagesize, $page, "doing/default/$type"); if ($type == 'atentto') { $recommendsize = $rownum ? 3 : 6; $recommandusers = $this->doing_model->recommend_user($recommendsize); } $userarticle=$this->topic_model->get_user_articles(0,5); include template('doing'); }}?>代碼不是很長,但是這背后引申出好幾個文件,還是很需要一番時間來學(xué)習(xí)的。先一段一段地分析語句吧。從下面這段代碼里我們可以知道,這個頁面的架構(gòu)調(diào)用了model里面的“doing-model”業(yè)務(wù)模型

function __construct() { parent::__construct(); //調(diào)用doing_model模型 $this->load->model("doing_model"); $this->load->model("topic_model");}然后接著看index函數(shù)里的語句

$navtitle = "問答動態(tài)";$type = 'atentto';?//獲取第三分段的參數(shù),無參數(shù)就是默認(rèn)界面$recivetype = $this->uri->segment ( 3 );if ($recivetype) { $type = $recivetype;}if (!$this->user['uid']) { $type = 'all';}$navtitletable = array( 'all' => '問答動態(tài)', 'my' => '我的動態(tài)', 'atentto' => '關(guān)注的動態(tài)' //default);$navtitle = $navtitletable[$type];這一段主要的作用是確定type的屬性,由上面三張網(wǎng)頁圖可知,不同type情況下呈現(xiàn)的動態(tài)的數(shù)量是不同的,type關(guān)系到下面動態(tài)數(shù)據(jù)的篩選。在確定type屬性這里我們用到的核心語句是

$this->uri->segment ( );這行語句來自CI框架URI類中的一個函數(shù)

主要目的就是從URI中獲得我們想要的一個字段,從index后面的“/”從1開始計數(shù),所以$this->uri->segment ( 3 )就是index后面的第三段

像default界面沒有3段,所以返回null值,type還是一開始默認(rèn)設(shè)置的“atentto”,然后就是頁面分頁的設(shè)置,這一部分是所有項目的公共部分,與我們今天討論的問題無關(guān),所以我們繼續(xù)往下看

//type對應(yīng)動態(tài)類型$doinglist = $this->doing_model->list_by_type($type, $this->user['uid'], $startindex, $pagesize);$rownum = $this->doing_model->rownum_by_type($type, $this->user['uid']);我們可以發(fā)現(xiàn),這里有兩個變量分別調(diào)用了doing_model里面的兩個函數(shù),所以我們就在doing_model文件里搜索函數(shù)名,就能分別得到兩個函數(shù),list_by_type函數(shù)很長,在這里只截取一部分,不妨礙理解

function list_by_type($searchtype = 'all', $uid = 0, $start = 0, $limit = 20) { $doinglist = array (); $sql = ""; $uid = intval ( $uid ); switch ($searchtype) { case 'all' : $sql .= "select * from " . $this->db->dbprefix . "doing where action in(1,2,3,6,9,11)"; break; case 'my' : $sql .= "select * from " . $this->db->dbprefix . "doing where authorid=$uid "; break; case 'atentto' : $sql .= "select d.* from " . $this->db->dbprefix . "doing as d," . $this->db->dbprefix . "user_attention as u where d.authorid=u.uid and u.followerid=$uid and action in(1,2,3,6,9,11)"; break; 可以發(fā)現(xiàn),不同的type使得我們在數(shù)據(jù)庫里檢索數(shù)據(jù)的條件也會不同,因而獲得的結(jié)果也不同,所以出現(xiàn)了上圖三種情況的動態(tài)頁面。當(dāng)然,我們也可以在mysql里面找到同名數(shù)據(jù)表,對應(yīng)上面代碼的查詢條件,加深我們對于數(shù)據(jù)庫里各個數(shù)據(jù)表含義、數(shù)據(jù)字段的理解。

//list_by_type函數(shù)續(xù)?$sql .= " ORDER BY createtime DESC LIMIT $start,$limit";$query = $this->db->query ( $sql );foreach ( $query->result_array () as $doing ) { $doing ['doing_time'] = tdate ( $doing ['createtime'] );//顯示幾天前的操作 $doing ['user'] = $this->get_by_uid ( $doing ['authorid'] ); $doing ['avatar'] = get_avatar_dir ( $doing ['authorid'] ); $doing ['actiondesc'] = $this->actiontable [$doing ['action']]; $doing ['followerlist'] = $this->get_follower ( $doing ['questionid'] ); if ($doing ['refer_authorid']) { $doing ['refer_avatar'] = get_avatar_dir ( $doing ['refer_authorid'] ); } switch ($doing ['action']) { case '1' : // 提出了問題 $doing ['question'] = $this->getquestionbyqid ( $doing ['questionid'] ); $doing ['category'] = $this->get_cat_bycid ( $doing ['question'] ['cid'] ); $doing ['categoryname'] = $doing ['category'] ['name']; $doing ['cid'] = $doing ['category'] ['id']; $doing ['title'] = $doing ['question'] ['title']; $doing ['hidden'] = $doing ['question'] ['hidden']; $doing ['views'] = $doing ['question'] ['views']; $doing ['answers'] = $doing ['question'] ['answers']; $doing ['attentions'] = $doing ['question'] ['attentions']; $doing ['content'] = $doing ['question'] ['title']; $doing ['image'] = getfirstimg ( $doing ['question'] ['description'] ); $doing ['description'] = cutstr ( checkwordsglobal ( strip_tags ( $doing ['question'] ['description'] ) ), 240, '...' ); $doing ['url'] = urlmap ( 'question/view/' . $doing ['questionid'], 2 ); break; case '2' : /* other code */}利用CI框架的$this->db->query ( $sql )語句,訪問我們前面查詢好的數(shù)據(jù)集的數(shù)據(jù),再進(jìn)行遍歷,通過doing-model模型設(shè)定的多個其他的函數(shù),提取出數(shù)據(jù)庫里的我們需要的數(shù)據(jù)及數(shù)據(jù)格式,然后根據(jù)各條數(shù)據(jù)的“action”對應(yīng)的值,匹配動態(tài)不同的操作,最后輸出結(jié)果,達(dá)到我們書寫動態(tài)信息的目的。

list_by_type函數(shù)查找動態(tài)對應(yīng)的用戶的數(shù)據(jù),rownum_by_type查找的是動態(tài)對應(yīng)的問題的數(shù)據(jù)

//最后的部分//頁面封裝$departstr = page($rownum, $pagesize, $page, "doing/default/$type");if ($type == 'atentto') { $recommendsize = $rownum ? 3 : 6; $recommandusers = $this->doing_model->recommend_user($recommendsize);}?//側(cè)邊欄的實現(xiàn)$userarticle=$this->topic_model->get_user_articles(0,5);?//template為抽象模板,用于通過filename調(diào)用,大概是實現(xiàn)靜態(tài)網(wǎng)頁的模板include template('doing');以上就是筆者艱澀的學(xué)習(xí)過程,希望筆者這些淺顯的理解能對你有所幫助。如有錯誤理解,也希望各位讀者能悉心指出,謝謝。

關(guān)鍵詞:學(xué)習(xí),動態(tài),實現(xiàn),用戶

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉