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 ,获取商品的信息了。

当一个人把寂寞当作人生预约的美丽,

ecshop中的浏览历史代码分析

相关文章:

你感兴趣的文章:

标签云: