时间:2023-09-25 20:06:01 | 来源:网站运营
时间:2023-09-25 20:06:01 来源:网站运营
专家动态页面的实现——php基于CI框架的学习(二):以下是本次学习的页面class Expert extends CI_Controller{}
在Expert类里定义了几个参数以及说明其使用了哪些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'; //排序
可以在页面中一一对应if ($cid != 'all') { $category = $this->category [$cid]; //得到分类信息 $navtitle = $category ['name'] . "专家列表"; $cfield = 'cid' . $category ['grade'];//获取分类的等级}
第二行语句有点难以理解,选中后面的category查看引用,可以发现当前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;
同时再选中$category右键查看快速引用,会找到下面几行代码$this->load->database ();$category = $this->category = $this->cache->load ( 'category', 'id', 'displayorder' );
访问缓存数据,通过load()获取数据库里,名字为“category”的数据表里的id和displayorder字段的数据,以下是load函数的源代码function fromcache($cachename,$cachetime = 3){}
总之,由上方可以得出全局变量$category中读取的是数据库某条数据,源代码$category = $this->category [$cid]; //得到分类信息
意为:Expert新定义的$category 为 db.category.id = cid 的整条数据库的信息//上接源代码,即$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'] ); //获取子分类
此段代码包含的函数:function list_by_cid_pid($cid, $pid) { $cid=intval($cid); $pid=intval($pid); $sublist = array (); //把cid,pid值等于'all'的分类设置为平级的分类 if ($cid == 'all') { $cid = 0; } if ($pid == 'all') { $pid = 0; } $where=" and onlybackground!=1 "; //在数据表里寻找与函数参数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;}
我们可以访问相关的数据库来//判断网页是否是付费内容$orderwhere = '';switch ($status) { case 'all' : //全部 $orderwhere = ''; break; case '1' : //付费 $orderwhere = ' and mypay>0 '; break; case '2' : //免费 $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 ) ) );令当前页面的page为$this->uri->segment ( 5 )返回的整数值,如果不存在该地址就返回1,此网页返回1,因为最多只有四个分页//冗长的,本页面最后一个判断语句$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 () );
该语句中调用了以下方法//展示专家列表,get_list获取user数据表所有专家用户id//并判断他是否认证,最后一次登陆时间,关注人数,被关注次数,个人擅长分类$expertlist = $this->expert_model->get_list ( 1, $startindex, $pagesize, $cid, $status );//通过page函数,以分类的id号,是否免费重新建立两个分页$departstr = page ( $rownum, $pagesize, $page, "expert/default/$cid/$status" );//使当前页面,每一页面最多可处理15条数据$questionlist = $this->expert_model->get_solves ( 0, 15 );//代码的页面实现include template ( 'expert' );
关键词:学习,动态,实现,专家