• Yii常见导航无限分类的取出方法(带缓存)


    1.

    试图页面代码:

    <?php foreach((array)$this->_catalog as $key=>$row):?>
    <?php if($row['parent_id'] == 0):?>

    //导航一般只取出父id为0的。

    <dd>
    <h3><a href="<?php echo $this->createUrl('post/index',array(
    'catalog'=>$row['catalog_name_alias'])) ?>">
    <?php echo $row['catalog_name']?><i></i></a></h3>
    <ul>
    <?php foreach((array)Catalog::lite($row['id']) as $key=>$val):?>
    <li><a href="<?php echo $this->_navLink($val)?>"><?php echo $val['catalog_name']?></a></li>
    <?php endforeach?>
    </ul>
    </dd>
    <?php endif?>
    <?php endforeach?>

    2.基类控制器的init方法里面(注意:继续父类初始化也要特别说明parent::init)

    $this->_catalog = Catalog::get(0, XXcache::system('_catalog'));

    //从缓存里面取出id为0,

    3.XXcache.php:

    <?php

    class XXcache{

    public static function system( $id, $expirse = 600, $fields = '', $params = array() ) {
    $value = Yii::app()->cache->get( $id );
    if ( $value === false ) {
    return self::_refresh( $id, $expirse, $fields, $params );
    } else {
    return $value;
    }
    }

    public function _refresh( $id, $expirse, $fields, $params ) {
    try {
    switch ( $id ) {
    case '_link':
    $data = (array) self::_base( 'Link', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
    self::set( $id, $data, $expirse );
    break;
    case '_pca':
    $data = (array) self::_base( 'Pca', $fields, array ( 'condition' => "status_is='Y'" , 'order' => 'id ASC' ) );
    self::set( $id, $data, $expirse );
    break;
    case '_userGroup':
    $data = (array) self::_base( 'UserGroup', $fields );
    self::set( $id, $data, $expirse );
    break;
    case '_ad':
    $data = (array) self::_base( 'Ad', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
    self::set( $id, $data, $expirse );
    break;
    case '_config':
    $data = (array) self::_config( $params );

    self::set( $id, $data, $expirse );
    break;
    case '_catalog':
    $data = (array) self::_base( 'Catalog', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
    self::set( $id, $data, $expirse );
    break;
    default:
    throw new Exception( '数据不在接受范围' );
    break;
    }

    return $data;
    } catch ( Exception $error ) {
    exit( $error->getMessage() );
    }
    }

    protected function _base( $id = '', $fields = '', $condition = '' ) {
    $mod = ucfirst( $id );
    $model = new $mod();
    $dataGet = $model->findAll( $condition );
    foreach ( (array) $dataGet as $key => $row ) {
    foreach ( (array) self::_attributes( $fields, $model ) as $attr ) {
    $returnData[$key][$attr] = $row->$attr;
    }
    }
    return $returnData;
    }

    protected function _attributes( $fields, $model = '' ) {
    if ( empty( $fields ) || trim( $fields ) == '*' ) {
    return $model->attributeNames();
    } else {
    $fields = str_replace( ',', ',', $fields );
    return explode( ',', $fields );
    }
    }

    }

    4.catalog.php

    /**
    * 取分类
    */
    static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = '---') {

    $str_repeat = '';
    if ($level) {
    for($j = 0; $j < $level; $j ++) {
    $str_repeat .= $repeat;
    }
    }
    $newarray = array ();
    $temparray = array ();
    foreach ( ( array ) $array as $v ) {
    if ($v ['parent_id'] == $parentid) {
    $newarray [] = array ('id' => $v ['id'], 'catalog_name' => $v ['catalog_name'],
    'catalog_name_alias' => $v ['catalog_name_alias'],
    'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'],
    'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'],
    'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'],
    'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] ,
    'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],
    'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],
    'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],
    'template_show' => $v ['template_show'],'create_time' => $v ['create_time'],
    'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );

    $temparray = self::get ( $v ['id'], $array, ($level + $add) );
    if ($temparray) {
    $newarray = array_merge ( $newarray, $temparray );
    }
    }
    }
    return $newarray;
    }



    /**
    * 获取下级子类,普通模式
    *
    * @param $parentId
    * @param array $array
    * @return array
    */
    static public function lite ($parentId, array $array = array(), $params = array())
    {
    if(empty($parentId))
    return ;
    $eachArr = empty($array)? XXcache::system('_catalog', 86400): $array;
    foreach ((array)$eachArr as $row) {
    if ($row['parent_id'] == $parentId)
    $arr[] = $row;
    }
    return $arr;
    }

  • 相关阅读:
    虎虎的小尾巴:作为交易员,从最初的新人到如今实现稳定盈利,一路走来你有过什么记忆非常深刻的亏损教训和体会? 2018-08-09
    虎虎的小尾巴:期货市场的资金吸引力?
    虎虎的小尾巴
    虎虎的小尾巴:高质量的期货研究报告去哪里找?
    虎虎的小尾巴:如何看待2017.8.7PTA1801一分钟内闪崩下跌6%?
    虎虎的小尾巴:交易逻辑重于一切 2017-08-13
    虎虎的小尾巴:期货数据入门
    虎虎的小尾巴:期货交易在使用基本面分析操作过程中要注意哪些问题?怎么控制风险? 2017-03-11
    Android开发-- 使用ADT23 的一些问题
    Git 学习笔记--3.EGit使用手册
  • 原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3360935.html
Copyright © 2020-2023  润新知