|
|
|
|
|
|
| |
Using the script we previously made (more specifically, we need the variable $count), we can now begin to create a graphical counter.
Note: This version does not require the GD library
On this page we'll make an HTML graphical counter. This means that image tags (<img>) will be dynamically added to the HTML, instead of an image being created dynamically. While this way of creating a counter is faster and lighter
on the system, all the pages with the counter must be PHP enabled.
This script can be used with any counter script. In this script, we are simply creating the image tags, not adding or updating the count values. If you use a different counter, just make sure to set the variable $count to the count value. In this tutorial, we'll use the script from the previous lesson. Let's get started...
First of all, you need to upload some digits to your website. A huge selection can be found at Digit Mania. Make sure that all these images have the same height and width.
Now it's time to set some variables:
->$path is the path to the images directory (ex /home/www/counter/images/)
->$url should be the HTTP path to that same directory (ex
http://www.domain.com/counter/images/).
->$ending is the file name of the images (normally '.gif')
->$default_style is the style if none is chosen (styles are explained below)
I thought it would be neat to be able to have multiple counter styles. For example, maybe I want to have a set of black digits as well as white digits. To do this, simply upload 0black.gif, 1black.gif, 2black.gif... 0white.gif,
1white.gif, 2white.gif, etc to the '/images/' directory. Then, if you want the digits to be black unless specified otherwise, make $default_style equal to 'black'. It's that easy! |
|
<?
$path = '/home/www/counter/images/';
$url = 'http://www.somedomain.com/counter/images';
$ending = '.gif';
$default_style = 'black';
?>
|
|
|
Now that we've set the initial variables, it's time to start the real coding. We need to check and see if the variable $style has been set. If it hasn't, then $style should be set equal to the $default_style. A simple trinary operator will suffice. |
|
When that is completed, we can start to build the HTML. We need to get the height and width of the images that we'll be using. Because the height and width of all the images are the same, we only need to get the information once.
Note: The height and width of different styles can be different, just not any images of the same style.
PHP has a very handy function called GetImageSize() which suits our purposes perfectly. This function will actually print out the height and width HTML code! |
|
|
<?
$path = '/home/www/counter/images/';
$url = 'http://www.somedomain.com/counter/images';
$ending = '.gif';
$default_style = 'black';
$style = ($style)?$style:$default_style;
$size = GetImageSize($path. '1'.$style.$ending);
?>
|
|
|
Now it's time to print out the HTML. All we need to do is to loop through each number in $count, printing out the code as we go. That's all that's to it! |
|
<?
$path = '/home/www/counter/images/';
$url = 'http://www.somedomain.com/counter/images';
$ending = '.gif';
$default_style = 'black';
$style = ($style)?$style:$default_style;
$size = GetImageSize($path. '1'.$style.$ending);
for ($i = 0; $i < strlen($count); $i++) {
echo "<img src=\"$url$count[$i]$style$ending\" $size[3]>";
}
?>
|
|
|
|
|
|
|
| |
| Beginners guide to PHP and MySQL - Creating a simple guest book Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL | | | Counting - Creating a simple counter Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL | | | Counting - Creating a more sophisticated GIF based counter using PHP and MySQL Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL | | | Jump Start to Easy URLs Categories : PHP, Beginner Guides, MySQL, File System, To PHP | | | How To add paging (Pagination) with PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP | | | Beginners Guide to PHP - Introduction to cookies Categories : Beginner Guides, Cookies, To PHP, PHP | | | How TO Install PHP, Apache and MySQL on Linux or Unix Categories : PHP, MySQL, Apache, Installation, Beginner Guides | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Who's Linking? Categories : PHP, Beginner Guides, To PHP | | | Grabb'n and Pars'n Categories : Beginner Guides, PHP, To PHP | | | Start Using MySQL Categories : MySQL, Databases, To MySQL, Beginner Guides | | | tracking where and what on your site people are clicking Categories : PHP, MySQL, HTML and PHP, HTML | | | Date Arithmetic With MySQL Categories : PHP, Databases, MySQL, Date Time | | | Time Is Money Part 1 of 2 - Designing and implementing a Web-based application Categories : PHP, Databases, MySQL, Complete Programs | | | PHP and MySQL News with Comments Categories : PHP, Databases, MySQL | |
| |
|
|