利用highcharts统计插件做网站来源统计模块

浏览:4084 最后更新:2015-07-25 17:20 分类:类库 关键字: 饼形图 来源统计 highcharts highcharts统计插件 highcharts统计
1, 新建表
--
-- 表的结构 `tb_engines`
--

CREATE TABLE IF NOT EXISTS `tb_engines` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`type` char(10) DEFAULT NULL,
`name` char(10) DEFAULT NULL,
`counts` int(10) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

--
-- 转存表中的数据 `tb_engines`
--

INSERT INTO `tb_engines` (`id`, `type`, `name`, `counts`) VALUES
(1, 'baidu', '百度', 10),
(2, 'soso', '搜搜', 12),
(3, 'sogou', '搜狗', 7),
(4, 'so', '360搜索', 2),
(5, 'google', '谷歌', 55),
(6, 'bing', '必应', 23),
(7, 'yahoo', '雅虎', 34),
(8, 'youdao', '有道', 22),
(9, 'direct', '直接访问', 40);


2, 控制器初始化中加入来源统计
// 本类由系统自动生成,仅供测试用途
class CommonAction extends Action {
public $formToken = '';

public function _initialize(){
//访客来源判断
$refer = $_SERVER["HTTP_REFERER"];//获取上一页面地址
if($refer)
{
$refer_string = parse_url($refer, PHP_URL_HOST);//
$arr=array("baidu","soso","sogou","so","google","bing","yahoo","youdao");//常用搜索引擎,注意"soso","sogou","so"的顺序
foreach($arr as $re)
{
if(substr_count($refer_string,$re) >= 1)
{
$map['type'] = $re;
break;
}
}
M("engines")->where($map)->setInc('counts',1);
}
else
{
$map['type'] = 'direct';
M("engines")->where($map)->setInc('counts',1);
}
}
}

3,输出饼图表到页面
// 统计来源
class AlixAction extends CommonAction {
public function main()
{
/*引入GoogChart类*/
import("ORG.Util.GoogChart");
$chart = new GoogChart();
$engine = M("engines")->field('type,name,counts')->select();

//设置$data数组数据;
foreach($engine as $k=>$value)
{
//格式 ['百度:10次', 10],['谷歌:55次', 55],['必应:23次', 23],['雅虎:34次', 34],['有道:22次', 22],['直接访问:40次', 40]
$data .= "['". $value[name] .":". $value[counts] ."次', ". $value[counts] ."],";
}
$data = substr($data, 0, -1);
$this->assign('data',$data);
$this->display();
}
}


4,附件是main.html页面与插件

























评论( 相关
后面还有条评论,点击查看>>