ecshop後臺新增一個完整的統計使用者資訊的功能
開發ecshop大概有半年多時間了,每次改的功能也沒有做總結和記錄,今天新增完一個功能,記錄一下吧,省得以後忘記又要麻煩去查詢。
需求:在後臺菜單-報表統計-下面新增-使用者情況-的功能,包括:每週新增註冊使用者,每週累積註冊使用者,活躍使用者-周內至少登陸1次,每月訪問量等等。
第一步:建立資料庫的表
說明:個人本地資料庫是:eshop,新建表名:ecs_user_situation。
第二步:建立儲存過程
說明:個人本地儲存過程名字:pro_users。DELIMITER $$ DROP PROCEDURE IF EXISTS `eshop`.`pro_users`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `pro_users`() BEGIN DECLARE week_count INT; DECLARE user_count INT; DECLARE over_count INT; DECLARE month_count INT; -- 每週註冊使用者 SELECT COUNT(*) INTO week_count FROM ecs_users WHERE reg_time>(UNIX_TIMESTAMP(NOW())-7*24*60*60); -- 總註冊使用者 SELECT COUNT(*) INTO user_count FROM ecs_users; -- 每週的活躍使用者 SELECT COUNT(*) INTO over_count FROM ecs_users WHERE last_login>(UNIX_TIMESTAMP(NOW())-7*24*60*60); -- 每月的訪問量 SELECT COUNT(*) INTO month_count FROM ecs_stats WHERE access_time>UNIX_TIMESTAMP(DATE_SUB(DATE_SUB(DATE_FORMAT(NOW(),'%y-%m-%d 00:00:00'),INTERVAL EXTRACT(DAY FROM NOW())-1 DAY),INTERVAL 0 MONTH)); INSERT INTO ecs_user_situation(week_count,user_count,over_count,month_count,week_time) VALUES (week_count,user_count,over_count,month_count,NOW()); END$$ DELIMITER ;
第三步:建立觸發器
DELIMITER $$
ALTER EVENT `eve_users` ON SCHEDULE EVERY 1 HOUR STARTS '2013-09-02 12:11:14' ON COMPLETION PRESERVE ENABLE DO BEGIN
CALL pro_users();
END$$
DELIMITER ;
說明:個人本地觸發器名字:eve_users。
第四步:新增後臺選單
1.新增選單URL
在目錄admin/includes/inc_menu.php的-報表統計-的那部分程式碼下面新增:
$modules['06_stats']['user_situation'] = 'user_situation.php?act=list';
2.新增選單名稱
在目錄languages/zh_cn/admin/common.php的/* 報表統計 */的那部分程式碼下面新增:
$_LANG['user_situation'] = '使用者情況';
3.新增選單許可權
在目錄admin/includes/inc_priv.php的-報表統計許可權-的那部分程式碼下面新增:
$purview['user_situation'] = 'user_situation';
4.新增管理許可權
在目錄languages/zh_cn/admin/priv_action.php新增下面的程式碼:
$_LANG['user_situation'] = '使用者情況';
5.新增資料庫許可權記錄
INSERT INTO ecs_admin_action(parent_id,action_code) VALUES(6,'user_situation');
第五步:業務邏輯程式碼
在目錄admin下面新增user_situation.php,程式碼如下:
<?php
/**
* add by zbl 2013-08-29
* 新增註冊使用者,累積註冊使用者,活躍使用者-周內至少登陸1次,每月訪問量
* @var unknown_type
*/
define('IN_ECS', true);
//頁面引用
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
if($_REQUEST['act']=='list'){
$user_situation=get_user_situation();
$smarty->assign('user_situation_list', $user_situation['user_situation_list']);
$smarty->assign('filter', $user_situation['filter']);
$smarty->assign('record_count', $user_situation['record_count']);
$smarty->assign('page_count', $user_situation['page_count']);
$smarty->assign('full_page', 1);//解決分頁查詢出現頁面重複情況
$smarty->assign('ur_here', $_LANG['user_situation']);
/* 在頁尾顯示記憶體資訊 */
assign_query_info();
$smarty->display('user_situation.htm');
}elseif($_REQUEST['act']=='query'){
$user_situation=get_user_situation();
$smarty->assign('user_situation_list', $user_situation['user_situation_list']);
$smarty->assign('filter', $user_situation['filter']);
$smarty->assign('record_count', $user_situation['record_count']);
$smarty->assign('page_count', $user_situation['page_count']);
$smarty->assign('ur_here', $_LANG['user_situation']);
$tpl = 'user_situation.htm';
make_json_result($smarty->fetch($tpl), '',array('filter' => $user_situation['filter'], 'page_count' => $user_situation['page_count']));
}
?>
說明:這裡有兩個方法,一個是列表資料查詢,另外一個是分頁查詢,每行的程式碼解析就不詳細說。第六步:操作資料庫的方法
在目錄admin/includes/lib_goods.php裡面新增get_user_situation()方法:
/**
* zbl add 20130902
* Enter description here ...
*/
function get_user_situation(){
//分頁
$filter['record_count']=$GLOBALS['db']->GetOne('SELECT count(*) FROM '.$GLOBALS['ecs']->table('user_situation'));
$filter = page_and_size($filter);
//排序
$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'week_time' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
//查詢
$sql='SELECT week_count,user_count,over_count,month_count,week_time FROM '.$GLOBALS['ecs']->table('user_situation').' order by '.$filter[sort_by].' '.$filter[sort_order].' LIMIT ' . $filter['start'] . ',' . $filter[page_size];
//echo $sql;
$row = $GLOBALS['db']->getAll($sql);
//返回資料
return array('user_situation_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
說明:這裡的程式碼順序不能放錯,$sql的where後面查詢條件必須加上才可以實現分頁功能,所以$filter['sort_by']和$filter['sort_order']這兩個引數不能缺少,詳細請檢視/admin/js/listtable.js的listTable.sort()方法。
第七步:新增顯示頁面
在目錄/admin/templates下面新增user_situation.htm程式碼如下:
<!-- $Id: user_situation.htm 16752 2013-08-28 zbl $ -->
{if $full_page}
{include file="pageheader.htm"}
{insert_scripts files="../js/utils.js,listtable.js"}
<form method="post" action="" name="listForm" onsubmit="return confirmSubmit(this)">
<div class="list-div" id="listDiv">
{/if}
<table cellpadding="3" cellspacing="1">
<tr>
<th><a href="javascript:listTable.sort('week_time'); ">{$lang.week_time}</a></th>
<th><a href="javascript:listTable.sort('week_count'); ">{$lang.week_count}</a></th>
<th><a href="javascript:listTable.sort('user_count'); ">{$lang.user_count}</a></th>
<th><a href="javascript:listTable.sort('over_count'); ">{$lang.over_count}</a></th>
<th><a href="javascript:listTable.sort('month_count'); ">{$lang.month_count}</a></th>
<tr>
{foreach from=$user_situation_list item=user_situation}
<tr>
<td align="center">{$user_situation.week_time}</td>
<td align="center">{$user_situation.week_count}</td>
<td align="center">{$user_situation.user_count}</td>
<td align="center">{$user_situation.over_count}</td>
<td align="center">{$user_situation.month_count}</td>
</tr>
{foreachelse}
<tr><td class="no-records" colspan="10">{$lang.no_records}</td></tr>
{/foreach}
</table>
<!-- 分頁 -->
<table id="page-table" cellspacing="0">
<tr>
<td align="right" nowrap="true">
{include file="page.htm"}
</td>
</tr>
</table>
{if $full_page}
</div>
</form>
<script type="text/javascript">
listTable.recordCount = {$record_count};
listTable.pageCount = {$page_count};
{foreach from=$filter item=item key=key}
listTable.filter.{$key} = '{$item}';
{/foreach}
</script>
{include file="pagefooter.htm"}
{/if}
說明:html頁面注意引入js檔案和用{if $full_page}{/if}來包含首位程式碼來解決分頁查詢出現的重複頁面。
第八步:新增顯示頁面的列表名字
在目錄languages/zh_cn/admin/下面新增user_situation.php程式碼如下:
<?php
/**
* add by zbl 2013-08-30
* Enter description here ...
* @var unknown_type
*/
$_LANG['week_time'] = '時間';
$_LANG['week_count'] = '每週註冊使用者數';
$_LANG['user_count'] = '累積註冊使用者數';
$_LANG['over_count'] = '每週活躍使用者數';
$_LANG['month_count'] = '每月訪問量';
?>
第九步:測試執行
說明:本人這裡都是修改觸發器出現的當天資料。
這個流程從修改資料庫到頂層頁面都有說明,ecshop的二次開發按照這個流程基本能開發所有功能了。詳細的功能實現程式碼再慢慢研究就可以了。