Building Counters
Now for the second installment in our counter series
, a graphical counter . This si much the same as before
but of course yo need some images for your counter .
Here are two great resources which I use frequentlt
for my graphic counters.
Digitmania : this has a fantastic range of counter
digits available to download.
Counterart : another huge range of counter digits ,
this is a must see
Now for the first part of the script , I thought about
just using the same script as last time but then thought
no we will show a different way of accessing a file
, reading a file , writing to a file.
You will have to create a text file called mycount.txt
and make sure this file is readable and writable .
Here is the script
<?php
//this is our file with the count in it
//note this is in the same directory as the script
$counter_file = "mycount.txt";
//now we open our file for reading
if(!($fp = fopen($counter_file , "r"))) die
("cannot open file");
//store the first 24 bytes of data from the file into
$counter
$counter = (int)fread($fp, 24);
//close the file
fclose($fp);
//increment the counter by 1
$counter++;
//open the counter file for writing
$fp = fopen($counter_file , "w");
//write the new $counter value into the file
fwrite($fp, $counter);
//close the file
fclose($fp);
?>
Now we save this as grcounter.php and include this
onto the page where we want the counter to appear like
this.
<?php
//include the main part of the counter
include "grcounter.php";
?>
We have just one bit to go and this displays the graphics
on the screen .
<?php
//graphics 1 stored in images directory and are
//numbered from 0.gif to 9.gif
//loop through the values of the $counter variable
//ensure that all digits are processed by using the
strlen
//function to check the length of the $counter variable
for ($i = 0 ;$i < strlen($counter) ; $i++)
{
//build of our <img src> tag
$imgsrc = SubStr($counter,$i ,1);
echo "<img src =\"images/" . $imgsrc
. ".gif\">";
}
?>
And here is this example in all its glory
2069
Warning: fopen("mycount.txt", "w") - Permission
denied in /home/sites/site16/web/grcounter.php
on line 9
Warning: Supplied argument is not a valid File-Handle
resource in /home/sites/site16/web/grcounter.php
on line 10
Warning: Supplied argument is not a valid File-Handle
resource in /home/sites/site16/web/grcounter.php
on line 11
  
And now for our second example
<?php
//graphics stored in images/1/ directory , the images
//are labelled from 0.taskbar.gif to 9.taskbar.gif
for ($i = 0 ;$i < strlen($counter) ; $i++)
{
$imgsrc = SubStr($counter,$i ,1);
echo "<img src =\"images/1/" . $imgsrc
. "taskbar.gif\">";
}
?>
  
And finally because I like this one here is another
digit example , this one is called punk
  
That is it for just now I hope you found this helpful.
|