ecshop中的浏览历史

来源:互联网 发布:xboxone运行windows 编辑:程序博客网 时间:2024/06/11 21:06

今天看了看 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
原创粉丝点击