商城购物,最近浏览过的商品的开发

最近浏览过的商品

    public function actionView($id) {
        $model = $this->loadModel($id);

        /* 记录浏览历史 */

        if (isset(Yii::app()->request->cookies['history'])) {

            $history = explode(',', Yii::app()->request->cookies['history']->value);
            array_unshift($history, $id);
            $history = array_unique($history);

            while (count($history) > 5) {
                array_pop($history);
            }

            $cookie = new CHttpCookie('history', implode(',', $history));
            $cookie->expire = F::gmtime() + 3600 * 24 * 30;
            Yii::app()->request->cookies['history'] = $cookie;
        } else {
            $cookie = new CHttpCookie('history', $id);                     //value = $id;
            $cookie->expire = F::gmtime() + 3600 * 24 * 30;
            Yii::app()->request->cookies['history'] = $cookie;
        }

        /* 更新点击次数 */
        $model->click_count = $model->click_count + 1;
        $model->save();
        $this->render('view', array(
            'model' => $model,
        ));
    }

 

清除

public function actionClearHistory() {
        unset(Yii::app()->request->cookies['history']);
    }
	/*
	 * 暴力清除商品浏览历史记录!
	 */
	$("#clearRec").click(function(event) {
            alert(1);
	    $.ajax({
		url: "<?php echo Yii::app()->createUrl('/item/clearHistory') ?>",
		async: false
	    }),
	    $(".recent").html("").append("<div style='padding:20px'>没有商品浏览记录!</div>");
	});

 

view 

<ul>
    <?php
    if (isset(Yii::app()->request->cookies['history'])) {
        $ids = Yii::app()->request->cookies['history']->value;
        $item_ids = explode(',', $ids);
        foreach ($item_ids as $item_id) {

            $item = Item::model()->findByPk($item_id);
            ?>
            <li><div class="i-img"><?php echo $item->getRecentThumb() ?></div><div class="i-name"><?php echo $item->getTitle()  ?></div></li>
                    <?php
                }
            } else {
                echo '<div style="padding:20px">没有商品浏览记录!</div>';
            }
            ?>    

</ul>

  

  

posted @ 2013-12-04 18:04  冯志强采桑子  阅读(287)  评论(0编辑  收藏  举报