PHP Diary | Script School | PHP Scripts | TD Scripts.com
TD Forum - An unthreaded messageboard script to exchange ideas with your site visitors


[back]
go back 02/19/01 "How to make multiple page counters using php/mySQL" go forward[next]

Multi-colored source code

This tip comes courtesy of the PHP Programming messageboard that I moderate from "Gnilrad"

If your code appears as just a text file would (without the coloring) then you will need to modify your .htaccess file. In that file add the following:
example code:


AddType application/x-httpd-php-source .phps

This will allow syntax highlighting of your code.

Example #30: of .htaccess file to force colorizing of php source code - you can now go back to the php-scripts examples page and view the source of every (where applicable) php example script :) Cool, huh?!

Counting another way

Back on 1/09/2000, I showed you how to make a simple counter using a flat file data system and over time this has proven to be one of the more popular diary entries (by reader rating) that I've done. One thing you couldn't do with that particular script was to have more than one counter (unless you modified it considerably). What if you wanted to have counters on multiple pages in your website using one script? Today let's create a script to do this using mySQL. The first thing we need to do is plan out our mySQL table structure.

CREATE TABLE tds_counter
(
COUNT_ID INT NOT NULL AUTO_INCREMENT,
page_path VARCHAR(250),
impressions INT,
reset_date TIMESTAMP,
PRIMARY KEY (COUNT_ID)
)

Explanation of fields in the mySQL table:

COUNT_ID - this controls the unique counter ID so we can have many different counters
pagepath - where is our page located (URL path) for reference from the domain
impressions - number of times it has been shown
reset_date - date that we reset/created the counter

What if I don't have Telnet access?

It's not the end of the world if you don't have telnet access on your server, you can still write a PHP script that will build the new table for you. Let's create such a script that creates the table and then inserts our first counter page (this one) into the table.

<?
$mysql_db = "DATABASE NAME";
$mysql_user = "YOUR MYSQL USERNAME";
$mysql_pass = "YOUR MYSQL PASSWORD";
$mysql_link = mysql_connect("localhost", $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $mysql_link);

$create_query = "CREATE TABLE tds_counter (
    COUNT_ID INT NOT NULL AUTO_INCREMENT,
    pagepath VARCHAR(250),
    impressions INT,
    reset_counter DATETIME,
    PRIMARY KEY (COUNT_ID)
    )";

mysql_query($create_query, $mysql_link);
print("Table Creation for <b>tds_counter</b> successful!<p>");

$insert = "INSERT into tds_counter VALUES (
    0, '/php_diary/021901.php3', 0, SYSDATE()
   )";

mysql_query($insert, $mysql_link);
print("Inserted new counter successfully for this page: http://www.php-scripts.com/php_diary/021901.php3<p>");

?>

Example #31: creating a mySQL table using a php script instead of telnet [Source Code] (there is no executabe example for this script for obvious reasons).

The counter code itself

The counter code is really pretty easy. We look for a valid ID number coming in and then increment the counter if it is. I'm adding an additional variable called $inv which will be a flag to indicate whether the counter value should be shown or not. We want to make sure the input is a number, if not, we ignore the counter for security purposes.

<?
$mysql_db = "DATABASE NAME";
$mysql_user = "YOUR MYSQL USERNAME";
$mysql_pass = "YOUR MYSQL PASSWORD";
$mysql_link = mysql_connect("localhost", $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $mysql_link);

$result = mysql_query("SELECT impressions from tds_counter where COUNT_ID='$cid'", $mysql_link);
if(mysql_num_rows($result)) {
   mysql_query("UPDATE tds_counter set impressions=impressions+1 where COUNT_ID='$cid'", $mysql_link);
   $row = mysql_fetch_row($result);
   if(!$inv) {
       print("$row[0]");
   }
}
?>

Since we inserted this page when we created the table this page would be COUNT_ID #1, or abbreviated $cid=1. To insert our counter in the php-enabled web page (php, phtml, php3 extension usually) we use the require function and a format like this:

<? $cid=1; require("counter.php"); ?>

To make an INVISIBLE counter use the following code:

<? $cid=1; $inv=1; require("counter.php"); ?>

Example #32: Our working counter shows 0079905 visitors have visited this page since the last reset (2/23/02 Thx Craig!) [Source Code]

ADVANCED USERS: Reusable function?

You could easily wrap the code above in a reusable function as follows:

<?
function pushCount($id, $inv) {
global $mysql_link;
$result = mysql_query("SELECT impressions from tds_counter where COUNT_ID='$cid'", $mysql_link);
  if(mysql_num_rows($result)) {
      mysql_query("UPDATE tds_counter set impressions=impressions+1 where COUNT_ID='$cid'", $mysql_link);
      $row = mysql_fetch_row($result);
      if($inv != 1) {
       print("$row[0]");
      }
  }
}

?>

To call this function from within another script you would use:

pushCount($cid, 0); // visible counter
pushCount($cid, 1); // invisible counter

If you decide you want to use more than one counter. All you have to do is insert a new record into the table and reference that $cid. Now you can call as many different counters as you want on your website. You could put the same counter code on multiple pages and get an aggregate count of all pages as well.

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 4.00 (627)

[back]go back 02/19/01 "How to make multiple page counters using php/mySQL" go forward[next]

PHP Diary | Script School | PHP Scripts | TD Scripts.com

Copyright 1999-2001 php-scripts Last Modified 02/23/02 06:01