首页 » 网站推广 » phpci框架技巧_用户动态页面的实现php基于CI框架的进修一

phpci框架技巧_用户动态页面的实现php基于CI框架的进修一

访客 2024-10-26 0

扫一扫用手机浏览

文章目录 [+]

然后我们就在项目文件里找到同名文件开始困难晦涩地学习

<?phpdefined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );class Doing extends CI_Controller { function __construct() { parent::__construct(); //调用doing_model模型 $this->load->model("doing_model"); $this->load->model("topic_model"); } function index() { $navtitle = "问答动态"; $type = 'atentto'; //获取第三分段的参数,无参数便是默认界面 $recivetype = $this->uri->segment ( 3 ); if ($recivetype) { $type = $recivetype; } if (!$this->user['uid']) { $type = 'all'; } $navtitletable = array( 'all' => '问答动态', 'my' => '我的动态', 'atentto' => '关注的动态' //default ); $navtitle = $navtitletable[$type]; //分页设置 $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'); }}?>

代码不是很长,但是这背后引申出好几个文件,还是很须要一番韶光来学习的。
先一段一段地剖析语句吧。
从下面这段代码里我们可以知道,这个页面的架构调用了model里面的“doing-model”业务模型

phpci框架技巧_用户动态页面的实现php基于CI框架的进修一 phpci框架技巧_用户动态页面的实现php基于CI框架的进修一 网站推广

function __construct() { parent::__construct(); //调用doing_model模型 $this->load->model("doing_model"); $this->load->model("topic_model");}

然后接着看index函数里的语句

phpci框架技巧_用户动态页面的实现php基于CI框架的进修一 phpci框架技巧_用户动态页面的实现php基于CI框架的进修一 网站推广
(图片来自网络侵删)

$navtitle = "问答动态";$type = 'atentto';//获取第三分段的参数,无参数便是默认界面$recivetype = $this->uri->segment ( 3 );if ($recivetype) { $type = $recivetype;}if (!$this->user['uid']) { $type = 'all';}$navtitletable = array( 'all' => '问答动态', 'my' => '我的动态', 'atentto' => '关注的动态' //default);$navtitle = $navtitletable[$type];

这一段紧张的浸染是确定type的属性,由上面三张网页图可知,不同type情形下呈现的动态的数量是不同的,type关系到下面动态数据的筛选。
在确定type属性这里我们用到的核心语句是

$this->uri->segment ( );

这行语句来自CI框架URI类中的一个函数

紧张目的便是从URI中得到我们想要的一个字段,从index后面的“/”从1开始计数,以是$this->uri->segment ( 3 )便是index后面的第三段

像default界面没有3段,以是返回null值,type还是一开始默认设置的“atentto”,然后便是页面分页的设置,这一部分是所有项目的公共部分,与我们本日谈论的问题无关,以是我们连续往下看

//type对应动态类型$doinglist = $this->doing_model->list_by_type($type, $this->user['uid'], $startindex, $pagesize);$rownum = $this->doing_model->rownum_by_type($type, $this->user['uid']);

我们可以创造,这里有两个变量分别调用了doing_model里面的两个函数,以是我们就在doing_model文件里搜索函数名,就能分别得到两个函数,list_by_type函数很长,在这里只截取一部分,不妨碍理解

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; }

可以创造,不同的type使得我们在数据库里检索数据的条件也会不同,因而得到的结果也不同,以是涌现了上图三种情形的动态页面。
当然,我们也可以在mysql里面找到同名数据表,对应上面代码的查询条件,加深我们对付数据库里各个数据表含义、数据字段的理解。

//list_by_type函数续​$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 )语句,访问我们前面查询好的数据集的数据,再进行遍历,通过doing-model模型设定的多个其他的函数,提取出数据库里的我们须要的数据及数据格式,然后根据各条数据的“action”对应的值,匹配动态不同的操作,末了输出结果,达到我们书写动态信息的目的。

list_by_type函数查找动态对应的用户的数据,rownum_by_type查找的是动态对应的问题的数据

//末了的部分//页面封装$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);​//template为抽象模板,用于通过filename调用,大概是实现静态网页的模板include template('doing');

以上便是笔者艰涩的学习过程,希望笔者这些浅近的理解能对你有所帮助。
如有缺点理解,也希望各位读者能悉心指出,感激。

标签:

相关文章

TDS程序,智慧城市建设中的关键纽带

随着我国城市化进程的加快,智慧城市建设已成为我国经济社会发展的重要方向。在城市管理、公共服务、产业升级等方面,智慧城市发挥着越来越...

网站推广 2024-12-05 阅读0 评论0

《非梦少女》,青春逐梦,绽放光芒

青春是一首悠扬的乐曲,奏响着梦想的旋律;青春是一片绚烂的画卷,描绘着逐梦的脚步。《非梦少女》这部动画作品,以青春为主题,讲述了四位...

网站推广 2024-12-05 阅读0 评论0

万事达八折代码,消费新体验,优惠无限

随着我国经济的快速发展,人们的消费观念逐渐转变,越来越多的人开始追求高品质、高性价比的生活。在这个过程中,信用卡作为现代消费的重要...

网站推广 2024-12-05 阅读0 评论0