ecshop中的浏览历史

今天看了看 ecshop 中的浏览历史的代码,分析了一下,有什么不对的地方,欢迎指出纠正,不胜感激...


不难看出,在你每次浏览一件商品的同时,会在左侧中记录您的浏览记录,在ecshop中是通过cookie来记录的, 在goods.php里可以查到如下代码:


 

if (!empty($_COOKIE['ECS']['history']))

{

    $history = explode(',', $_COOKIE['ECS']['history']);

    array_unshift($history, $goods_id);

    $history = array_unique($history);


    while (count($history) > $_CFG['history_number'])

    {

        array_pop($history);

    }


    setcookie('ECS[history]', implode(',', $history), gmtime() + 3600 * 24 * 30);

}

else

{

    setcookie('ECS[history]', $goods_id, gmtime() + 3600 * 24 * 30);

}

 


每一次浏览,都会记录$good_id(商品的id),放到cookie里。


在模版里goods.dwt里是引用了<!-- #BeginLibraryItem "/library/history.lbi" --><!-- #EndLibraryItem -->在history.lbi里可以看到 {insert name='history'}, 基本上学过smarty的都知道,这是局部不缓冲用到的,那么它肯定存在一个方法: insert_history(),果然,在lib_insert.php中找到了。其实lib_insert.php 就是一个动态内容函数库。

 

function insert_history()

{

    $str = '';

 

    if (!empty($_COOKIE['ECS']['history']))

    {

        $where = db_create_in($_COOKIE['ECS']['history'], 'goods_id');

        $sql   = 'SELECT goods_id, goods_name FROM ' . $GLOBALS['ecs']->table('goods') .

                " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0";

        $query = $GLOBALS['db']->query($sql);

 

        $res = array();

        while ($row = $GLOBALS['db']->fetch_array($query))

        {

            $res[$row['goods_id']] = $row;

        }

 

        $tureorder = explode(',', $_COOKIE['ECS']['history']);

 

        foreach ($tureorder AS $key => $val)

        {

            $goods_name = htmlspecialchars($res[$val]['goods_name']);

            if ($goods_name)

            {

                $short_name = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($goods_name, $GLOBALS['_CFG']['goods_name_length']) : $goods_name;

                $str .= '<li><a href="' . build_uri('goods', array('gid' => $val), $goods_name). '" title="' . $goods_name . '">' . $short_name . '</a></li>';

            }

        }

    }

 

    return $str;

}

 

其实该函数返回的字符串就是history.lbi里的所需内容,需要说明一下的是像代码中类似 $_CFG['history_number'],一般是系统定义的常量或是数据库中保存的字段,查了一下发现是在 ecs_shop_config表里。剩下的就是看一下代码,怎么通过商品的id ,获取商品的信息了。不再多说,提醒一下,上面用到了几个针对数据函数,如array_unshift,array_unique还是经常遇到的,顺便巩固一下。

 

好了,今天就到这里吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值