🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>整理时间:2018年9月17日 By w.y.b @Meizi Office [TOC] ## 知识点: 1、视频列表 2、视频列表分页 3、分页与筛选地址组合 4、列表多条件筛选 ## 一、视频列表 ### (一)属性数据表 ``` CREATE TABLE `属性表名` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `attrtype` varchar(30) NOT NULL COMMENT '属性分类', `attrname` varchar(50) NOT NULL COMMENT '属性名称', `value` smallint(5) NOT NULL DEFAULT '0' COMMENT '属性值', `listorder` smallint(5) NOT NULL DEFAULT '0' COMMENT '排序', `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态 1:启用,0:禁用', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; ``` ### (二)后台处理 ``` // 属性 $video_list = Db::name('属性表名')->where('attrtype','video')->select(); $this->assign('video_list',$video_list); $level_list = Db::name('属性表名')->where('attrtype','level')->select(); $this->assign('level_list',$level_list); ``` ### (三)模板渲染 ``` <div class="feifan123-net"> <span>类型</span> <div class="list_nav j-list-nav" id="j-nav-type0"> <ul> <li class="active"><a href="#"> 全部 </a></li> {volist name="videoattr_list" id="vo"} <li><a href="#"> {$vo.attrname} </a></li> {/volist} </ul> </div> </div> <div class="feifan123-net"> <span>行业</span> <div class="list_nav j-list-nav" id="j-nav-type2"> <ul> <li class="active"><a href="#"> 全部 </a></li> {volist name="level_list" id="vo"} <li><a href="#"> {$vo.attrname} </a></li> {/volist} </ul> </div> </div> ``` ## 二、视频列表分页 ### (一)参考网址 >分页 https://v3.bootcss.com/components/#pagination 为您的网站或应用提供带有展示页码的分页组件,或者可以使用简单的翻页组件。 >最新基于ThinkPHP5.0+BootStrap框架开发的自适应WAP手机端响应式界面博客系统PHP源码 https://blog.csdn.net/wazyb8/article/details/79401940 >Bootstrap 分页 http://www.runoob.com/bootstrap/bootstrap-pagination.html >【thinkphp5】 分页样式修改 https://www.cnblogs.com/richerdyoung/p/7058952.html >ThinkPHP5.0分页手册 https://www.kancloud.cn/manual/thinkphp5/154294 >thinkphp5传参分页功能无效?解决方案 https://blog.csdn.net/houbin99999/article/details/72768240 ### (二)分页具体实现 #### 1、后台处理 ``` public function category(){ //分类ID $id = input('id'); $this->assign('id',$id); // 接收前台属性传参 $attr['channel'] = (int)input('get.channel'); $attr['level'] = (int)input('get.level'); $this->assign('attr',$attr); // 分类属性 $videoattr_list = Db::name('属性表')->where('attrtype','video')->select(); $this->assign('videoattr_list',$videoattr_list); // 行业属性 $level_list = Db::name('属性表')->where('attrtype','level')->select(); $this->assign('level_list',$level_list); // 视频列表 $video_list = Db::name('视频表')->order('id desc')->paginate(10,false,[ 'type' => 'bootstrap', 'var_page' => 'page', ]); $page = $video_list->render(); $this->assign('video_list',$video_list); $this->assign('page',$page); // 自动调用模型视图 $template = getModInfoById($id,'category_template'); return view('../application/index/view/default/index/' . $template); } ``` #### 2、前端渲染 >类型属性 ``` <div class="feifan123-net"> <span>类型</span> <div class="list_nav j-list-nav" id="j-nav-type0"> <ul> <li {if condition="empty($attr['channel'])"} class="active" {/if} ><a href="?channel=0&level={$attr.level}"> 全部 </a></li> {volist name="videoattr_list" id="vo"} <li {if condition="$vo.value eq $attr['channel']"} class="active" {/if}><a href="?channel={$vo.value}&level={$attr.level}"> {$vo.attrname} </a></li> {/volist} </ul> </div> </div> ``` >行业属性 ``` <div class="feifan123-net"> <span>行业</span> <div class="list_nav j-list-nav" id="j-nav-type2"> <ul> <li {if condition="empty($attr['level'])"} class="active" {/if} ><a href="?channel={$attr.channel}&level=0"> 全部 </a></li> {volist name="level_list" id="vo"} <li {if condition="$vo.value eq $attr['level']"} class="active" {/if}><a href="?channel={$attr.channel}&level={$vo.value}"> {$vo.attrname} </a></li> {/volist} </ul> </div> </div> ``` >排序属性 ``` <div class="feifan123-net bb0"> <span>排序</span> <div class="list_nav j-list-nav"> <ul> <li class="active"><a href="#">最新</a></li> <li><a href="#">最热</a></li> </ul> </div> </div> ``` ## 三、分页与筛选地址组合 ### (一)分页参数组合 #### 1、接收前台属性传参 ``` $attr['channel'] = (int)input('get.channel'); $attr['level'] = (int)input('get.level'); $attr['page'] = (int)input('get.page'); $this->assign('attr',$attr); ``` #### 2、组合参数 ``` $url = '/index/index/category?rand=' . rand(5, 12); if($attr['channel']){ $url .= '&channel=' . $attr['channel']; $query['channel'] = $attr['channel']; } if($attr['level']){ $url .= '&level=' . $attr['level']; $query['level'] = $attr['level']; } if($attr['page']){ $url .= '&page=' . $attr['page']; $query['page'] = $attr['page']; } ``` #### 3、分页组件调用 ``` $video_list = Db::name('视频表名')->order('id desc')->paginate(10,false,[ 'type' => 'bootstrap', 'var_page' => 'page', 'query' => $query, // $query 是数组 ]); $page = $video_list->render(); $this->assign('page',$page); ``` ### (二)改进组合 #### 1、自定义索引列表 >思路:改进TP5查询方法,将数据中的ID作为键,原数据作为值,重新建立数组。 ``` // 指定表名 public function table($table){ $this->where = array(); $this->field = '*'; $this->order = ''; $this->table = $table; return $this; } // 指定查询字段 public function field($field = '*'){ $this->field = $field; return $this; } // 排序 public function order($order){ $this->order = $order; return $this; } // 指定查询条件 public function where($where = array()){ $this->where = $where; return $this; } // 自定义索引列表 public function cates($index){ $query = Db::name($this->table)->field($this->field)->where($this->where); $this->order && $query = $query->order($this->order); $lists = $query->select(); if(!$lists){ return false; } $results = []; foreach ($lists as $key => $value) { $results[$value[$index]] = $value; } return $results; } ``` #### 2、函数array_keys() 函数返回包含数组中所有键名的一个新数组。 如果提供了第二个参数,则只返回键值为该值的键名。 参考网址:http://www.w3school.com.cn/php/func_array_keys.asp 片段代码: `<?php if(!in_array($data['label_channel'],array_keys($channel_list))){echo 'class="selected"';}?>` 片段代码: ``` $data['label_channel'] = (int)input('get.label_channel'); $channel_list = $this->db->table('video_label')->where(array('flag'=>'channel'))->cates('id'); $this->assign('channel_list',$channel_list); ``` ``` <h3>频道:</h3> <ul class="mod_category_item"> <li <?php if(!in_array($data['label_channel'],array_keys($channel_list))){echo 'class="selected"';}?>> <a href="/index.php/index/index/cate?label_channel={$data.label_channel}&label_charge={$data.label_charge}&label_area={$data.label_area}">全部</a> </li> {volist name="channel_list" id="channel"} <li {if condition="$data.label_channel eq $channel.id"}class="selected"{/if}> <a href="/index.php/index/index/cate?label_channel={$data.label_channel}&label_charge={$data.label_charge}&label_area={$data.label_area}">{$channel.title}</a> </li> {/volist} <li class="close-mod_btn"> <a data-searchpingback-elem="link" data-searchpingback-param="ptype=11" href="javascript:;" j-delegate="action"><em class="vm-inline">收起</em><i class="site-icons ico-explain-t"></i></a> </li> </ul> <div class="openBtn"> <a class="openBtn-txt" href="javascript:;" j-delegate="action"><em class="vm-inline">更多</em><i class="site-icons ico-explain-b"></i></a> </div> </div> ``` ## 四、列表多条件筛选 ### (一)数据表设计 #### 1、属性表 ``` CREATE TABLE `属性表名` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `attrtype` varchar(30) NOT NULL COMMENT '属性分类', `attrname` varchar(50) NOT NULL COMMENT '属性名称', `value` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '属性值', `listorder` smallint(5) NOT NULL DEFAULT '0' COMMENT '排序值', `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态 1:启用,0:禁用', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; ``` #### 2、视频表 ``` CREATE TABLE `视频表名` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', `catid` int(11) NOT NULL DEFAULT '0' COMMENT '分类ID', `title` varchar(100) NOT NULL DEFAULT '' COMMENT '标题', `keywords` varchar(100) NOT NULL DEFAULT '' COMMENT '关键词', `tags` varchar(255) NOT NULL DEFAULT '' COMMENT 'TAGS', `description` varchar(255) NOT NULL DEFAULT '' COMMENT '摘要', `thumb` varchar(100) NOT NULL DEFAULT '' COMMENT '缩略图', `username` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名', `inputtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '发布时间', `url` varchar(100) NOT NULL DEFAULT '' COMMENT 'URL', `status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '状态', `views` int(11) NOT NULL DEFAULT '0' COMMENT '浏览量', `listorder` int(11) NOT NULL DEFAULT '0' COMMENT '排序', `content` text NOT NULL, `video` varchar(255) NOT NULL DEFAULT '', `channel` tinyint(5) unsigned NOT NULL DEFAULT '1' COMMENT '分类属性', `level` tinyint(5) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; ``` ### (二)组合参数 ``` // 分页参数组合(数组) $query['channel'] = $attr['channel']; $query['level'] = $attr['level']; $query['page'] = $attr['page']; 模板渲染 <li {if condition="empty($attr['channel'])"} class="active" {/if} ><a href="?channel=0&level={$attr.level}"> 全部 </a></li> {volist name="videoattr_list" id="vo"} <li {if condition="$vo.value eq $attr['channel']"} class="active" {/if}><a href="?channel={$vo.value}&level={$attr.level}"> {$vo.attrname} </a></li> {/volist} ``` ### (三)筛选条件 ``` // 分页参数组合(数组) $query['channel'] = $attr['channel']; $query['level'] = $attr['level']; $query['page'] = $attr['page']; // 组合条件 $where['status'] = 1; if(!empty($attr['channel'])){ $where['channel'] = $attr['channel']; } if(!empty($attr['level'])){ $where['level'] = $attr['level']; } // 视频列表 $video_list = Db::name('视频表名')->where($where)->order('id desc')->paginate(10,false,[ 'type' => 'bootstrap', 'var_page' => 'page', 'query' => $query, ]); ``` ### (四)片段代码 ``` public function category(){ //分类ID $id = input('id'); $this->assign('id',$id); // 接收前台属性传参 $attr['channel'] = (int)input('get.channel'); $attr['level'] = (int)input('get.level'); $attr['page'] = (int)input('get.page'); $this->assign('attr',$attr); // 分类属性 $videoattr_list = Db::name('属性表名`')->where('attrtype','video')->select(); $this->assign('videoattr_list',$videoattr_list); // 行业属性 $level_list = Db::name('属性表名')->where('attrtype','level')->select(); $this->assign('level_list',$level_list); // 分页参数组合(数组) $query['channel'] = $attr['channel']; $query['level'] = $attr['level']; $query['page'] = $attr['page']; // 组合条件 $where['status'] = 1; if(!empty($attr['channel'])){ $where['channel'] = $attr['channel']; } if(!empty($attr['level'])){ $where['level'] = $attr['level']; } // 视频列表 $video_list = Db::name('视频表名')->where($where)->order('id desc')->paginate(10,false,[ 'type' => 'bootstrap', 'var_page' => 'page', 'query' => $query, ]); $page = $video_list->render();//p($video_list);die; $this->assign('video_list',$video_list); $this->assign('page',$page); // 自动调用模型视图 $template = getModInfoById($id,'category_template'); return view('../application/index/view/default/index/' . $template); } ```