PHP
  Home arrow PHP arrow Page 6 - Making Usage Statistics in PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Making Usage Statistics in PHP
By: Adam Szanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 109
    2004-02-16

    Table of Contents:
  • Making Usage Statistics in PHP
  • Save the HTML Title
  • Showing the Statistics Table
  • Showing Figures Based on the Statistics
  • Creating the Image Library
  • Charting the Results
  • Before Saying Goodbye

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Making Usage Statistics in PHP - Charting the Results


    (Page 6 of 7 )

    There is another interesting thing, that I've done it with $visitors array. It's keys are the hostnames of the visitors, values are the number of their visits. To draw them in our figure, I use $pixel_per_visit. That's relative to the Top 1 - the visitor whose column is the highest on the diagram.


    imageline($image202020180$pen);
    // The vertical axe
    imageline($image, 15, 25, 20, 20, $pen);
    // The arrow
    imageline($image, 25, 25, 20, 20, $pen);
    imageline($image, 20, 180, 380, 180, $pen);
    // The horizontal axe
    imageline($image, 375, 175, 380, 180, $pen);
    imageline($image, 375, 185, 380, 180, $pen);
    $visitors=array();
     
    while ($stat=mysql_fetch_array($stat_r))
        $visitors
    [$stat['Host']]=$stat['Count_ID'];
            
    // Loading up the array...
        $max=current($visitors);
           // How many pages did the Top 1 visit?
        $pixel_per_visit=150/$max;
           // This is because we get the data
           // in number of visits, not in pixels
     
        
    $i=0;
          
    // This is for the loop, to know
          // the number of the current visitor


       
    foreach($visitors as $host => $visits) {
         
    // now $visits is number of visits
       imagefilledrectangle($image, 35+$i*30, 180-(visits*$pixel_per_visit),
            45+$i*30, 180, $pen);
       imagettftext($image, 8, 0, 40+$i*30, 175-($visits*$pixel_per_visit),
       $pen, "\windows\fonts\arial.ttf", $host);
       imagettftext($image, 15, 0, 32+(($i++)*30), 200,
       $pen,"\windows\fonts\arial.ttf", $visits);
    }

    When you're ready with drawing the figure you have to send the output to the browser. Here are the functions and the code:

    1. ImageGIF (resource image) - Outputs the image in GIF format
    2. ImageJPEG (resource image) - Outputs the image in JPEG format
    3. ImagePNG (resource image) - Outputs the image in PNG format imagepng($image);

    Note that these functions accept an optional second argument, the name of a new file to save the picture there. This can be useful if you'd like to make a picture saving option for your site.
    And here's the result:

    top10

     

     

     

     

     

     

    More PHP Articles
    More By Adam Szanto


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT