All of you know to create daily stats using php and mysql. Otherwise visit How to calculate daily statistics using PHP and MySQL. You can also create monthly statistics using php and mysql. The php and mysql allows user to create charts on daily, monthly and charts between two dates.
You need to follow the below steps to create monthly stats using php.
First you have to create table for update values.
Then you add the number of views to mysql table using php. Add the following code to header of each page in your website. Because header is in all pages. So you can easily calculate the number of views of your website.
<?php
where,
- select * from stats where date IN (CURDATE()) is select the views if present in current date.
- mysql_num_rows() return the number of rows selected. If it is 0, then value inserted into mysql table. Otherwise values are updated.
Then you need to create statistics using php and mysql through google charts. There are many types of charts in google. You can choose anything as you want. The php code for create monthly stats:
monthly_stats.php
<html>
<body>
<?php
mysql_connect('localhost','root','');
mysql_select_db('new');
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {var data = google.visualization.arrayToDataTable([
<?php
$str=" ['Month', 'Month'] ";
$query="select SUM(view) as vi, DATE_FORMAT( date, '%M' ) as dat from stats group by DATE_FORMAT(date, '%Y-%M') order by DATE_FORMAT(date, '%Y-%M') ASC";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result,MYSQL_BOTH)){
$str =$str . ",['". $rows['dat'] ."'," .$rows['vi'] ."]" ;
}
echo $str;
?>
]);
var options = {
//title: 'Company Performance',
hAxis: {title: 'Month', titleTextStyle: {color: 'red'}},
vAxis: {title: 'Total views', titleTextStyle: {color: '#FF0000'}, maxValue:'5', minValue:'1'},
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<p style="font-size:20px;">Monthly Stats</p>
<div id="chart_div" style="width: 400px; height: 200px;"></div>
</body>
</html>
where,
The following MySQL query is used to find total number of views of your websites per monthly
- select SUM(view) as vi, DATE_FORMAT( date, '%M' ) as dat from stats group by DATE_FORMAT(date, '%Y-%M') order by DATE_FORMAT(date, '%Y-%M') ASC
is select the number of views monthly.
Consider the following example:
Suppose your table look like this:
Then you'll get output like below:
Now you can calculate the number of views of your website monthly.
Related Post:
Create daily statistics using PHP and MySQL
Create yearly statistics dynamically using PHP and MySQL
Monthly stats using php and mysql:
You need to follow the below steps to create monthly stats using php.
1.Create table in mysql database:
First you have to create table for update values.
Create table stats(view int(10),date date)
Now the table is created with fields view and date.
2.Counter code in php:
Then you add the number of views to mysql table using php. Add the following code to header of each page in your website. Because header is in all pages. So you can easily calculate the number of views of your website.
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('new') or die(mysql_error());
$q=mysql_query('select * from stats where date IN (CURDATE())') or die(mysql_error());
$n=mysql_num_rows($q);
if($n==0)
{
mysql_query("insert into stats values(1,CURDATE())");
}
else
{
mysql_query("update stats set view=view+1 where date IN (CURDATE())");
}
?>
- select * from stats where date IN (CURDATE()) is select the views if present in current date.
- mysql_num_rows() return the number of rows selected. If it is 0, then value inserted into mysql table. Otherwise values are updated.
3. Stats(chart) using php and mysql:
Then you need to create statistics using php and mysql through google charts. There are many types of charts in google. You can choose anything as you want. The php code for create monthly stats:
monthly_stats.php
<html>
<body>
<?php
mysql_connect('localhost','root','');
mysql_select_db('new');
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {var data = google.visualization.arrayToDataTable([
<?php
$str=" ['Month', 'Month'] ";
$query="select SUM(view) as vi, DATE_FORMAT( date, '%M' ) as dat from stats group by DATE_FORMAT(date, '%Y-%M') order by DATE_FORMAT(date, '%Y-%M') ASC";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result,MYSQL_BOTH)){
$str =$str . ",['". $rows['dat'] ."'," .$rows['vi'] ."]" ;
}
echo $str;
?>
]);
var options = {
//title: 'Company Performance',
hAxis: {title: 'Month', titleTextStyle: {color: 'red'}},
vAxis: {title: 'Total views', titleTextStyle: {color: '#FF0000'}, maxValue:'5', minValue:'1'},
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<p style="font-size:20px;">Monthly Stats</p>
<div id="chart_div" style="width: 400px; height: 200px;"></div>
</body>
</html>
where,
The following MySQL query is used to find total number of views of your websites per monthly
- select SUM(view) as vi, DATE_FORMAT( date, '%M' ) as dat from stats group by DATE_FORMAT(date, '%Y-%M') order by DATE_FORMAT(date, '%Y-%M') ASC
is select the number of views monthly.
Consider the following example:
Suppose your table look like this:
Then you'll get output like below:
Now you can calculate the number of views of your website monthly.
Related Post:
Create daily statistics using PHP and MySQL
Create yearly statistics dynamically using PHP and MySQL
why month cannot be sort like "jan,feb,march,aprl" etc?
ReplyDelete