15158846557 在线咨询 在线咨询
15158846557 在线咨询
所在位置: 首页 > 营销资讯 > 网站运营 > 用户动态页面的实现——php基于CI框架的学习(一)

用户动态页面的实现——php基于CI框架的学习(一)

时间:2023-07-22 23:54:02 | 来源:网站运营

时间:2023-07-22 23:54:02 来源:网站运营

用户动态页面的实现——php基于CI框架的学习(一):最近在艰难晦涩地学习php,万事开头难,所以笔者决定从最简单的页面开始学习

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

<?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”业务模型

function __construct() { parent::__construct(); //调用doing_model模型 $this->load->model("doing_model"); $this->load->model("topic_model");}然后接着看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];这一段主要的作用是确定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');以上就是笔者艰涩的学习过程,希望笔者这些浅显的理解能对你有所帮助。如有错误理解,也希望各位读者能悉心指出,谢谢。

关键词:学习,动态,实现,用户

74
73
25
news

版权所有© 亿企邦 1997-2025 保留一切法律许可权利。

为了最佳展示效果,本站不支持IE9及以下版本的浏览器,建议您使用谷歌Chrome浏览器。 点击下载Chrome浏览器
关闭