PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: count
count - phponwebsites.com
Showing posts with label count. Show all posts

27 Jul 2014

Daily statistics using php and mysql

                       All website owners wants to know how many times the site is viewed ?. So they create this details in admin panel of their website. You can create daily statistics using php and mysql. The php and mysql allows user to create charts on daily, monthly and charts between two dates.

Daily stats using php and mysql:


                      You need to follow the below steps to create daily 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())");
    }
?>


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.

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 stats:

daily_stats.php


<html>
<body>
<?php
    mysql_connect('localhost','root','')  or die(mysql_error());
    mysql_select_db('new') or die(mysql_error());
?>
    <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=" ['Day', 'Day'] ";
$query="select view as vi, DATE_FORMAT( date, '%b-%d' ) as dat from stats order by date ASC";
$result=mysql_query($query)  or die(mysql_error());
while($rows=mysql_fetch_array($result,MYSQL_BOTH)){
$str =$str . ",['". $rows['dat'] ."'," .$rows['vi'] ."]" ;
}
echo $str;
?>
        ]);

      var options = {
          //title: 'Company Performance',
          hAxis: {title: 'Day', 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;">Dialy 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 daily
        - select view as vi, DATE_FORMAT( date, '%b-%d' ) as dat from stats order by date ASC
is select the number of views daily.

Consider the following example:
    Suppose your table look like this:

Daily stats using php and mysql


Then you'll get output like below:

Dialy statistics using php and mysql

        Now you can calculate the number of views of your website.

Related Post:
Create monthly statistics using PHP and MySQL
Create yearly statistics dynamically using PHP and MySQL 

23 Jun 2014

How to store array values into session in php

                You can store single value to session in PHP easily. Visit store values to session in php.


Store array values to session in PHP:


                          You can also store multiple values into session using PHP. You can store one or more than one values in array that can be stored into session. Consider the following example. It describes the array values directly stored into session in PHP. The following PHP script is used to store values to session in PHP.

<?php
 session_start();
 $ar=array('php','mysql','jquery');
 $_SESSION['arr']=$ar;
 $session=$_SESSION['arr'];
 foreach($session as $val)
 {
   echo $val.'<br>';
 }
 ?>

 where,
            session_start() - start the session
            $ar=array() - array variable

                  Now you can get output like this:

                                    php
                                    mysql
                                    jquery

Store multiple (array) values into session in PHP on form submit:

                          Consider the following example:


 <html>
 <body>
 <?php
 session_start();
 error_reporting('E_ALL ^ E_NOTICE');
 if($_POST['submit'])
 {
  if(count($_SESSION['arr'])==0)
  {
   $ar=array();
   $val=$_POST['value'];
   array_push($ar,$val);
   $_SESSION['arr']=$ar;
   print_R($_SESSION['arr']);
  }
  else
  {
   $val=$_POST['value'];
   array_push($_SESSION['arr'],$val);
   print_R($_SESSION['arr']);
  }
 }
?>
 <form action="#" method="post">
  <input type="text" name="value">
  <input type="submit" name="submit" value="Submit">
 </form>
 </body>
 </html>


                    The above example describes array values are storing into session in PHP while form submitting.
            where,
                      if(count($_SESSION['arr'])==0) {} - check  whether the array contains values or not. Create array variable and store value into array, then value is stored into session if there is no values in session. Otherwise it directly add values into session.
                     array_push() - add values to array in PHP.

Related Post: