Use PHP to create a dynamic counter

Source: Internet
Author: User
Tags php code
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.
In fact, the principle of counting is that, first, a log file, such as counter.txt or counter. log, is set. Each time you access this page, 1 is added to the file and the result is displayed. Therefore, our PHP code should start like this:
<? PHP
$ Countfile = "js/counter. inf ";
// Define that the file written by the counter is in the JS directory under the current directory // counter. inf. 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, the data in the file will be read, assuming 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 overwrite mode
Fwrite ($ fp, $ count );
// Write new data after 1
Fclose ($ fp );
// Close the file
}
At this time, the entire counting is complete. If it is just a simple text counting, you can output the value of the variable $ count 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 to an array called counter_array in sequence. The index of this array starts from 1 ($ array_count ). If 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 ("<table border = ($ % $ 43% ^ # ASD #2 $ # f $ % ^) 0 ($ % $ 43% ^ # ASD #2 $ # f $ % ^) height = ($ % $ 43% ^ # ASD #2 $ # f $ % ^) 5 ($ % $ 43% ^ # ASD #2 @ $ # f $ % ^) align = ($ % $ 43% ^ # ASD #2 @ $ # f $ % ^) center ($ % $ 43% ^ # ASD #2 $ # f $ % ^)> <tr> <td bgcolor = ($ % $ 43% ^ # ASD #2 $ # f $ % ^) # bab389 ($ % $ 43% ^ # ASD #2 $ # f $ % ^) align = ($ % $ 43% ^ # ASD #2 $ # f $ % ^) center ($ % $ 43% ^ # ASD #2 $ # f $ % ^)> Welcome, th ");
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.