ecshop团购倒计时放在首页

2009-11-02 09:41 来源:www.chinab4c.com 作者:ecshop专家

   ecshop里面,团购商品往往需要有倒计时的功能。那么如何将倒计时的功能,放在ecshop的首页呢?

   1:修改首页团购列表函数

    function index_get_group_buy()
{
    $time = gmtime();
    $limit = get_library_number('group_buy', 'index');

    $group_buy_list = array();
    if ($limit > 0)
    {
        $sql = 'SELECT gb.act_id AS group_buy_id, gb.goods_id, gb.ext_info, gb.goods_name, g.goods_thumb,gb.start_time start_time,gb.end_time end_time, g.goods_img ' .
                'FROM ' . $GLOBALS['ecs']->table('goods_activity') . ' AS gb, ' .
                    $GLOBALS['ecs']->table('goods') . ' AS g ' .
                "WHERE gb.act_type = '" . GAT_GROUP_BUY . "' " .
                "AND g.goods_id = gb.goods_id " .
                "AND gb.start_time <= '" . $time . "' " .
                "AND gb.end_time >= '" . $time . "' " .
                "AND g.is_delete = 0 " .
                "ORDER BY gb.act_id DESC " .
                "LIMIT $limit" ;
        $res = $GLOBALS['db']->query($sql);

        while ($row = $GLOBALS['db']->fetchRow($res))
        {
            /* 如果缩略图为空,使用默认图片 */
            $row['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
            $row['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
   
            /* 根据价格阶梯,计算最低价 */
            $ext_info = unserialize($row['ext_info']);

   $stat = group_buy_stat($row['group_buy_id'], $ext_info['deposit']);
   
   $row['total'] = $stat['total_goods'];
            $price_ladder = $ext_info['price_ladder'];
            if (!is_array($price_ladder) || empty($price_ladder))
            {
                $row['last_price'] = price_format(0);
            }
            else
            {
                foreach ($price_ladder AS $amount_price)
                {
                    $price_ladder[$amount_price['amount']] = $amount_price['price'];
                }
            }
            ksort($price_ladder);
            $row['last_price'] = price_format(end($price_ladder));

            $row['url'] = build_uri('group_buy', array('gbid' => $row['group_buy_id']));
            $row['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                           sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
            $row['short_style_name']   = add_style($row['short_name'],'');
   $row['end_time1'] = $row['end_time'];
   $row['start_time11'] = local_date('Y/m/d H:i',$row['start_time']);//用来处理开始时间。JS倒计时
   $row['end_time11'] = local_date('Y/m/d H:i',$row['end_time']);//用来处理结束时间JS倒计时
   $row['start_time'] = local_date('Y-m-d H:i',$row['start_time']);
   $row['end_time'] = local_date('Y-m-d H:i',$row['end_time']);
   
   
            $group_buy_list[] = $row;
        }
  
    }

    return $group_buy_list;
}
 

  2:修改library/group_buy.lbi

<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<!-- {if $group_buy_goods} -->
<script>
//在这里初始化变量
var DifferHour = -1
var DifferMinute = -1
var DifferSecond = -1
var daysms = 24 * 60 * 60 * 1000
var hoursms = 60 * 60 * 1000
var Secondms = 60 * 1000
var microsecond = 1000
//初始化结束变量
//开始初始化结束时间
{foreach from=$group_buy_goods item=goods key=key}

var Tday{$key} = new Date("{$goods.end_time11}");//循环出结束时间
{/foreach}
//初始化结束时间结束
</script>
<div class="box">
 <div class="box_1">
  <h3><span>{$lang.group_buy_goods}</span><a href="group_buy.php"><img src="../images/more.gif"></a></h3>
    <div class="centerPadd">
    <div class="clearfix goodsBox" style="border:none;">
      <!--{foreach from=$group_buy_goods item=goods key=key}-->
   <script>
   //开始动态生成数据名称。用foreach的key
   function clock{$key}()//动态生成数组
  {
   var time = new Date()
   var hour = time.getHours()
   var minute = time.getMinutes()
   var second = time.getSeconds()
   var timevalue = ""+((hour > 12) ? hour-12:hour)
   timevalue +=((minute < 10) ? ":0":":")+minute
   timevalue +=((second < 10) ? ":0":":")+second
   timevalue +=((hour >12 ) ? " PM":" AM")
   var convertHour = DifferHour
   var convertMinute = DifferMinute
   var convertSecond = DifferSecond
   var Diffms = Tday{$key}.getTime() - time.getTime()
   DifferHour = Math.floor(Diffms / daysms)
   Diffms -= DifferHour * daysms
   DifferMinute = Math.floor(Diffms / hoursms)
   Diffms -= DifferMinute * hoursms
   DifferSecond = Math.floor(Diffms / Secondms)
   Diffms -= DifferSecond * Secondms
   var dSecs = Math.floor(Diffms / microsecond)
   
   if(convertHour != DifferHour) a=DifferHour+"天";
   if(convertMinute != DifferMinute) b=DifferMinute+"时";
   if(convertSecond != DifferSecond) c=DifferSecond+"分"
     d=dSecs+"秒"
   document.getElementById("leftTime{$key}").innerHTML = a + b + c + d; //初始化显示层的ID
  
  setTimeout("clock{$key}()",1000)//动态调用对应的数组
  
  
  }
   </script>
      <div class="goodsItem">
           <a href="{$goods.url}"><img src="{$goods.thumb}" alt="{$goods.goods_name|escape:html}" class="goodsimg" /></a><br />
      <p><a href="{$goods.url}" title="{$goods.goods_name|escape:html}">{$goods.short_style_name|escape:html}</a></p>
           <font class="shop_s">{$goods.last_price}</font>
      <div id="leftTime{$key}">数据正在加载</div>
        </div>
      <!--{/foreach}-->
    </div>
    </div>
 </div>
</div>
<div class="blank5"></div>
<!-- {/if} -->

 

3:修改index.dwt

<script>
//这里都是循环的列出要到计时的函数
onload = function()
{
  try
  {
    {foreach from=$group_buy_goods item=goods key=key}

  clock{$key}()//循环调用倒计时函数
 {/foreach}
  }
  catch (e)
  {}
}
</script>

 通过onload函数,来加载这个倒计时功能。

来源:中国B4C电子商务