使用thinkphp开发功能模块:
C:控制器
M:模型
V:视图
友情链接模块
因为自己开发的功能,所以暂时没有注释,如果有看不明白的可以留言一起讨论。
控制
public function index(){ import('ORG.Util.Page');// 导入分页类 $M = M('Friendlink'); $keyword = I('keyword'); $status = I('status'); $group_id = I('group_id'); $statusArr = array(0=>'隐藏',1=>'显示','-1'=>'未审核'); if($status!=''){ $map['status'] = $status; } if(!empty($group_id)){ $map['group_id'] = $group_id; } if(!empty($keyword)&&$keyword!='请输入搜索关键词'){ $map['name'] = array('like','%'.$keyword.'%'); } $count = $M->where($map)->count(); $Page = new Page($count,20); $show = $Page->show(); $list = $M->where($map)->order('sort asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select(); foreach ($list as $key => $val) { $list[$key]['statusName'] = $statusArr[$val['status']]; } $this->assign('list',$list); $this->assign('page',$show); $this->display(); } public function show(){ $info = D('Friendlink')->getInfo(); $this->assign('info',$info); $this->display(); } public function add(){ $M = M('Friendlink'); if(IS_POST){ $id = I('id'); if(empty($id)){ $result = D('Friendlink')->add(); }else{ $result = D('Friendlink')->save(); } echo json_encode($result); }else{ $info = D('Friendlink')->getInfo(); $this->assign('info',$info); $this->display(); } } public function check(){ $result = D('Friendlink')->check(); echo json_encode($result); } public function uncheck(){ $result = D('Friendlink')->uncheck(); echo json_encode($result); } public function del(){ $result = D('Friendlink')->del(); echo json_encode($result); } public function status(){ $M = M('Friendlink'); $id = I('id'); $status = I('status'); $M-> where('id='.$id)->setField('status',$status); } //前置后置 public function _after_index(){ D('Friendlink')->updateCache(); }
模型
<?php class FriendlylinkModel extends Model { public function friendlylink() { $Friendlylink = M('Friendlylink'); $where['status'] = 1; $list = $Friendlylink->where($where)->select(); return $list; } public function getNavList($pid=0){ $Nav = M('Nav'); $where['pid'] = $pid; $where['status'] = 1; $navList = $Nav->where('pid='.$pid)->order('sort')->select(); foreach($navList as $key=>$val){ $subNavNum = $Nav->where('pid='.$val['id'])->count(); if( !empty($subNavNum) ){ $navList[$key]['subNav'] = $this->getSubNav($val['id']); } } return $navList; } private function getSubNav($pid){ $Nav = M('Nav'); $where['pid'] = $pid; $where['status'] = 1; $list = $Nav->where('pid='.$pid)->order('sort')->select(); foreach($list as $key=>$val){ $subNavNum = $Nav->where('pid='.$val['id'])->count(); if( !empty($subNavNum) ){ $list[$key]['subNav'] = $this->getSubNav($val['id']); } } return $list; } } ?>
视图模板在这里就不说了,因为每个人所使用的后台模板都一台一样。