通过GoogChart.class.php类 ,实现饼形图显示统计;
显示效果如图:
简单实现搜索引擎来源统计的饼形图-LMLPHP

首先,创建数据库,代码如下:
--
-- 表的结构 `ed_engines`
--

CREATE TABLE IF NOT EXISTS `ed_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 ;

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

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

在网站主入口文件插入以下代码:class BaseAction extends Action {
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);
}
}
在后台管理页面中插入以下代码,显示饼形图统计效果: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){
$data[$value[name].",".$value[counts]."次访问"]=$value[counts];
}

//设置颜色值
$color = array(
'#0181ec'
);
$chart->setChartAttrs( array(
'type' => 'pie',
'title' => '搜索引擎来源统计',
'data' => $data,
'size' => array( 550, 300 ),
'color' => $color
));
$this->assign('chart',$chart);
//设置$chart,通过页面模版中调用{$chart},显示统计图;

}
/**来源统计结束**/
附件,附上GoogChart.class.php类和example.php

简单实现搜索引擎来源统计的饼形图-LMLPHPgoogchart_0_1.zip( 3.12 KB 下载:138 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

09-09 11:11