Tutorial on how one can write a very basic MultiPage Text Counter which uses MySQL to store its data.
To have the counter running, we will require to have a database and a table. If you do not have the database created already or wish to make another database you do it by using the query below:
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
mysql_query("create database dbname", $link);
The following query will create the table needed by the multipage counter script:
mysql_query("select database dbname", $link);
mysql_query("create table counter(url varchar(255),count int)", $link);
?>
Now we have created the required table. We now need to make the counter script that will update or insert pages and their count in the database. We will call this file counter.php
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
or die("Could not connect: " . mysql_error());
mysql_query("select database dbname", $link);
$url=$PHP_SELF;
$q=$DBase->dbx_query("SELECT * FROM $dbtable WHERE url='$url'");
// URL exists in database, so we will need to update the count in this case.
if($result=mysql_fetch_array($q)) {
$cnt_val=$result[COUNT]++;
mysql_query("UPDATE $dbtable SET count='$result[COUNT]' WHERE url='$result[URL]'");
}
// URL doesn't exists in database, so we will need to insert a new row.
else {
mysql_query("INSERT INTO $dbtable VALUES('$url','1')");
$cnt_val=0;
}
echo "This page has been viewed ".$cnt_val+1." times";
?>
Now we are ready to show the counter on our pages. We can simply use the following code in any php file and the counter for that URL will show up.
<?php
include("counter.php");
?>
Hi there,
I am interested in implementing this script for my site(s) but I cannot browse the windows in order to copy it. (safary on a mac laptop);
explorer doesn't even show those script windows!
any suggestion?
thanks,
cd
Posted by: constantin at November 8, 2003 03:41 AMTry to view the source of the page and search for <?php
Then copy the code up until the </pre> to a html editor (in the code window, not the design window), and it should apper correctly in the design window...
or just get a windows emulator :o)
Posted by: bearchase at January 12, 2004 07:45 AMI stumbled on this from Google and wanted to say thanks for posting
Posted by: Jennifer at August 18, 2004 06:45 AM