ecshop显示商品按月统计销售数量的代码实例

这里给出ECShop显示商品按月统计销售数量的代码实例:

1. 定义报表类文件/admin/report/goods_sale_month.php:

<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/* 实例化对象 */
$report = new report_goods_sale_month();

/* 显示报表 */    
$report->display();

/* 报表类 */
class report_goods_sale_month 
{
    public function __construct()
    {
        /* 设置报表名称 */
        $this->name = '商品月销售量报表';    

        /* 设置报表 SQL */
        $this->sql = 'SELECT goods_id, SUM(goods_number) AS num, FROM_UNIXTIME(add_time, "%Y%m") AS month  FROM ' . $GLOBALS['ecs']->table('order_goods') . ' WHERE FROM_UNIXTIME(add_time, "%Y%m") >= ' . get_begin_month() . ' GROUP BY goods_id, month ORDER BY num DESC';
    }
    
    public function display()
    {
        /* 统计数据 */
        $data = $this->get_data();    
        
        /* 分页大小 */
        $size = 10;   
        
        /* 计算页数 */
        $page_count = ceil(count($data) / $size);    
        
        /* 获取当前页 */
        $page = empty($_REQUEST['page']) ? 1 : intval($_REQUEST['page']);
        
        /* 获取页数范围 */
        $pages = get_pages($page, $page_count, $size);    
        
        /* 获取商品数据 */
        $items = array_slice($data, ($page-1)*$size, $size);
        
        /* 模板赋值显示 */
        $smarty->assign('name',     $this->name);    
        $smarty->assign('data',     $items);
        $smarty->assign('pages', $pages);
        $smarty->display('report_goods_sale_month.html');
    }
}

2. 定义报表模板文件/admin/templates/report_goods_sale_month.html:

<table class="list-table">
    <thead>
        <tr>
            <th>商品名称</th>
            <th>月份</th>       
            <th>销售数量</th>      
        </tr>
    </thead>
    <tbody>
    {foreach from=$data item=item}
        <tr>
            <td>{$item.goods_name}</td>    
            <td>{$item.month}</td>  
            <td align="right">{$item.num}</td>
        </tr>
    {/foreach}  
    </tbody>
</table>
{$pages}  

3. 设置cron任务定期运行/admin/report/goods_sale_month.php脚本生成报表。

4. 在ECShop后台点击“报表”->“商品月销售量报表”查看结果。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论