Simple counter:
- Keeps track of views of each page.
- Shows you an overview of your website's traffic.
Procedure:
- First of all create a database and call it Counter, also create a table in that database with 2 fields, 'Page' which as a 20 character (can be more) varcharacter format and 'Count' which has an intermediate integer format.
- Insert rows equal to the number of pages on your site, and enter 'Count' as zero.
- At every page insert the following code:CODE<?php
mysql_connect("localhost","username","password") or die(mysql_error);
mysql_select_db("counter");
$result=mysql_query("SELECT* FROM counter WHERE `Page`='Main page'");
$row=mysql_fetch_array($result);
$i=$row['Count']+1;
mysql_query("UPDATE `counter`.`Counter` SET `Count` = '$i' WHERE CONVERT( `counter`.`Page` USING utf8 ) = 'Main page'");
echo "This page has ".$i." views!";
?>CODEmysql_connect("localhost","username","password") or die(mysql_error);
mysql_select_db("counter");
$result=mysql_query("SELECT* FROM counter WHERE `Page`='Main page'");
$row=mysql_fetch_array($result);
This code connects to the database and fetches the array(in this case row) of the viewed page.
Note: change the 'Main page' according to the names you entered in the table, example 'index.php', 'forums.php'.....CODE$i=$row['Count']+1;
mysql_query("UPDATE `counter`.`Counter` SET `Count` = '$i' WHERE CONVERT( `counter`.`Page` USING utf8 ) = 'Main page'");
This code increments the number of views of the selected page which was stored in $row['Count'], next the code updates the database with the new count.
Note: again change the 'Main page' according to the names you entered in the table, example 'index.php', 'forums.php'.....CODEecho "This page has ".$i." views!";
This is optional, incase you want the page to view how many times it has been visited so far, not necassary. You can change the message into something more attractive. - Now for the overview of your website's traffic:
Note:You can insert this code anywhere.CODE<?php
mysql_connect("localhost","username","password") or die(mysql_error);
mysql_select_db("counter");
$result=mysql_query("SELECT* FROM counter ");
$row=mysql_fetch_array($result);
echo "<table border=1 celspacing=1><tr><th>Page</th><th>Views</th></tr>";
echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";
while($row=mysql_fetch_array($result))
{echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";
}
echo "</table>";
?>CODEmysql_connect("localhost","username","password") or die(mysql_error);
mysql_select_db("counter");
$result=mysql_query("SELECT* FROM counter ");
$row=mysql_fetch_array($result);
This gets the FIRST row in the table we made.CODEecho "<table border=1 celspacing=1><tr><th>Page</th><th>Views</th></tr>";
echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";
This makes a table and views the data of the first page in it.CODEwhile($row=mysql_fetch_array($result))
{echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";
}
echo "</table>";
Using the while loop here, we can get every row in the table until it ends, when the table ends the mysql_fetch_array function returns false. So, with each value a new row is shown. When the table ends and false is return the loop ends and the table is closed. If you are wondering why i didn't do this in the begining instead of addressing the first row alone, well that is because in some cases the first row will be skipped, according to my experience anyways.
There you go a simple counter for your website, feel free to use,modify but not sell it


