Use PHP to create a dynamic counter

Source: Internet
Author: User
PHP is very powerful, and you can use it to do almost everything. Without complex variables and code, you can quickly make beautiful counters. Next, let's do it step by step. Actually, we all know how the counter works.

PHP is very powerful, and you can use it to do almost everything. Without complex variables and code, you can quickly make beautiful counters. Next, let's do it step by step.

Actually, the principle of the counter is that, first, a record file is closed, such as counter.txt or counter. log. each time you visit this page, add 1 to the file and then display the result. Therefore, our PHP code should start like this:
$ Countfile = 'JS/counter. inf ';
// Define the file written into the counter in the JS directory under the current directory // counter. inf, and then we should test whether the file can be opened
If ($ fp = fopen ($ countfile, 'r') = false) {// open the file in read/write mode. if the file cannot be opened, exit.
Printf ('open file % s failed! ', $ Countfile );
Exit;
}
Else
{
// If the file can be opened normally and the data in the file is read, assume that it is 1
$ Count = fread ($ fp, 10 );
// Read 10-bit data
$ Count = $ count 1;
// Count
Fclose ($ fp );
// Close the current file
$ Fp = fopen ($ countfile, 'w ')
// Open the file in the shadow mode
Fwrite ($ fp, $ count );
// Write the new data after adding 1
Fclose ($ fp );
// Close the file
}

At this time, all the counting work is complete. if it is just a simple text counting, the value of the variable $ count can be output here. The following code converts $ count to image mode:

$ Fp = fopen ($ countfile, 'r'); // open the file in read-only mode
$ Array_count = 1; // defines a variable that represents the position of an array element.
While (! Feof ($ fp )){
$ Current_number = fgetc ($ fp );
$ Counter_array [$ array_count] = $ current_number;
$ Array_elements = count ($ counter_array );
$ Array_count = $ array_count 1;
}


The above while loop is used to separate each digit. It reads values from counter. inf from left to right, and writes them into an array called counter_array in sequence. The index of this array starts from 1 ($ array_count ). Assume that counter. if the number in inf is 158, the array $ counter_array [] is like this: $ counter_array [1] = 1, $ counter_array [2] = 5, $ counter_array [3] = 8. With the above work, we can easily display different digital images. the code is as follows:

Echo ('

Welcome ');
For ($ array_id = 1; $ array_id <$ array_elements; $ array_id ){
Echo ('');
}
Echo ('guest
');
}

The code above is very simple, that is, to draw a table and display the required images in turn in the table. There are 10 images from 0.gif to 9.gif in the \ images \ counterdirectory. the for loop traverses the array, and the corresponding image of each digit is given from the high position to the low position until the end of the array. In this way, a complete counter is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.