zoukankan      html  css  js  c++  java
  • tp3.2中怎么访问分类及子分类下面的文章

    在项目开发过程中,我们可能会遇到在进入文章分类时需要遍历文章分类及文章子分类下面的文章的情况,具体解决步骤如下:

    一、为便于理解,这里列出用到的表及字段

    文章分类表(article_cate)

    文章表(article)

    其中 article 表的 cate_id 和 article_cate 表的 id 关联,article_cate 表中的 pid 是 id 的父级代号。

    从表中可以看出,在 id 为 69 的文章分类下的 id 为 70 的子分类 下的 id 为 111 的 分类下 有一篇 cate_id 为111的文章

    二、

    (1)关系图如下

    (2)要达到的效果就是

    在控制器中写一个方法:能取到 与 文章分类有关联的所有分类 id , 及 id 为 69 下的所有 子 id 及 子 id 下的 二级子 id ,以此类推。代码如下:

    复制代码
     1  public function getChild($pid = 0){
     2         global $article_cate;
     3         $where = [
     4             'pid' => $pid
     5         ];
     6 
     7         $datas = D('articleCate')->where($where)->select();
     8         if(!empty($datas)){
     9             foreach($datas as $data){
    10                 $article_cate[] = $data['id'];
    11                 $this->getChild($data['id']);
    12             }
    13         }
    14         return $article_cate;
    15     }
    16     public function getC(){
    17         global $article_cate;
    18         $article_cate = [];
    19 
    20     }
    复制代码

    调用:

    在 方法中传入 69 这个参数,就会返回与 id 为69 有关联的 id

    因为是定义了全局变量,所以在调用的时候还要清一下全局变量,具体调用代码如下:

    复制代码
    1  global $article_cate;
    2         $article_cate = [];
    3         $arr = $this->getChild(69);
    4         $str = implode(',',$arr);
    5         $str = $str.',69';
    6         $mapzb2bt['cate_id'] = array('in',$str);
    7         $zb2list = $arc->where($mapzb2bt)->order('a_id desc')->limit(6)->select();
    8         $this->assign('zb2list',$zb2list);
    复制代码

    其中拼了一个 69 是为了不进查询 id 为 69 下的所有子分类文章,还能查询 id 为69 下的分类文章

  • 相关阅读:
    css浏览器兼容问题集锦
    【转】H264编码原理以及I帧B帧P帧
    Makefile Shell 脚本;sed命令
    oProfile 学习
    C++ 局部变量的析构
    【转】C++ 单例模式
    C++ operator 知识点 2
    C++ operator 知识点
    218多校第九场 HDU 6424 (数学)
    2018多校第九场 HDU 6416 (DP+前缀和优化)
  • 原文地址:https://www.cnblogs.com/shenzikun1314/p/7281090.html
Copyright © 2011-2022 走看看