1.感觉是不一样的 无限分类

1.拿到所有分类

$model = $this->findModel($id);
$allCategory = Category::find()->asArray()->all();
$arrayCategoryIdName = ArrayHelper::map($allCategory, 'id', 'name');

ArrayHelper::map映射函数使用:
array (size=7)
  1 => string '手机' (length=6)
  2 => string '电脑' (length=6)
  3 => string '苹果' (length=6)
  4 => string '三星' (length=6)
  5 => string '台式机' (length=9)
  6 => string '笔记本' (length=9)
  7 => string '上网本' (length=9)

2.取得父id

$rootCategoryId = Category::getRootCatalogId($model->category_id, $allCategory);

static public function getRootCalalogId($id = 0, $array = [])
{
    if(0 == $id)
   return 0;
   foreach ((array)$array as $v) {
            if ($v['id'] == $id) {
                $parentId = $v['parent_id'];
                if(0 == $parentId)
                    return $id;
                else
                    return self::getRootCatalogId($parentId, $array);
            }
        } 
}

3.得到一个父类的所有子类

static public function getArraySubCatalogId($parentId = 0, $array = [])
    {
        $result[] = $parentId;
        foreach ((array)$array as $v) {
            if ($v['parent_id'] == $parentId) {
                $tempResult = self::getArraySubCatalogId($v['id'], $array);
                if ($tempResult) {
                    $result = array_merge($result, $tempResult);
                }
            }
        }
        return $result;
    }
 // 同类商品  和 同大类商品
        $sameCategoryProducts = Product::find()->where(['category_id' => $model->category_id])->orderBy(['sales' => SORT_DESC])->limit(3)->all();

        $sameRootCategoryProducts = Product::find()->where(['category_id' => $arraySameRootCategory])->orderBy(['sales' => SORT_DESC])->limit(Yii::$app->params['productHotCount'])->all();

4.记录浏览日志

// 记录浏览日志
        $historyProducts = [];
        $cookies = Yii::$app->request->cookies;
        if ($cookies->has('productHistory')) {
            $arrHistory = explode(',', $cookies->getValue('productHistory'));

            foreach ($arrHistory as $v) {
                $product = Product::findOne($v);
                if ($product) {
                    array_push($historyProducts, $product);
                }
            }

            array_unshift($arrHistory, $id);
            $arrHistory = array_unique($arrHistory);
            while (count($arrHistory) > Yii::$app->params['productHistoryCount']) {
                array_pop($arrHistory);
            }
            Yii::$app->response->cookies->remove('productHistory');
            Yii::$app->response->cookies->add(new Cookie([
                'name' => 'productHistory',
                'value' => implode(',', $arrHistory),
                'expire' => time() + 3600 * 24 * 30,
            ]));
        } else {
            Yii::$app->response->cookies->add(new Cookie([
                'name' => 'productHistory',
                'value' => $id,
                'expire' => time() + 3600 * 24 * 30,
            ]));
        }

 

posted on 2015-12-21 21:47  阿卡贝拉  阅读(235)  评论(0编辑  收藏  举报