PHP Production Dynamic counter

Source: Internet
Author: User
Tags exit count php code variable
Dynamic | Counter PHP is very powerful and you can use it to do almost anything. Without the need for complex variables and code, you can make beautiful counters very quickly. Now, let's do it step-by-step.

In fact, the principle of the counter everyone knows, first of all, to determine a record file, such as Counter.txt or Counter.log, every time you visit this page, add 1 to the file, and then show the results. So, the beginning of our PHP code should be this:
? Php
$countfile = "Js/counter.inf";
Define the counter write file is the current directory in the JS directory//counter.inf, and then we should test whether the file can be opened
if (($fp = fopen ($countfile, "r+")) = = False) {//Open file in read-write mode, exit if not open
printf ("Open file%s failed!", $countfile);
Exit
}
Else
{
If the file can be opened normally, read the data in the file, assuming 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 files in overwrite mode
Fwrite ($fp,$count);
Write new data after add 1
Fclose ($FP);
and close the file
}

At this point, the entire counting work is done, if only a simple word count, here you can output the value of the variable $count. Here is the code to convert $count to picture mode:

$fp = fopen ($countfile, "R"); Open a file in read-only mode
$array_count = 1; Defines a variable that represents the position of an array element, with the following
while (! feof ($fp)) {
$current_number = fgetc ($FP);
$counter_array[$array_count] = $current_number;
$array_elements = count ($counter_array);
$array_count = $array_count + 1;
}


The function of the while loop above is to separate each digit. It reads the value from left to right from the Counter.inf and writes sequentially to an array called Counter_array, which has an index of 1 ($array_count). If the number in Counter.inf is now 158, then 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 pictures, the display 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, section ");
for ($array_id = 1;$array_id < $array_elements; + + $array_id) {
Echo (" ");
}
Echo ("Guest </td></tr></table>");
}

The above code is very simple, that is, draw a table, and in turn, display the desired picture in the table. In the \images\counter directory there are 0.gif to 9.gif 10 pictures, for loop traversal array, from high to low to give each digit corresponding picture, 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.