Create a dynamic counter _php tutorial with PHP

Source: Internet
Author: User
PHP is now a popular server-side scripting language, it's very powerful, you can use it to do almost anything. Without the need for complex variables and code, you can make beautiful counters very quickly. Next, let's do it step-by-step.

In fact, the principle of the counter everyone knows, first of all, determine a record file, such as Counter.txt or Counter.log, each time you visit this page, add 1 to this file, and then show the results.

So, our PHP code should start with this:

? Php

§countfile = "Js/counter.inf";

The definition counter writes the file is the current directory in the JS directory//counter.inf, 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 open 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 a file in overwrite mode

Fwrite (§fp,§count);

Write new data after adding 1

Fclose (§FP);

and close the file

}

At this point, the entire counting work is completed, if only a simple text count, here you can output the value of the variable §count. Here's the code to convert §count to picture mode:

§FP = fopen (§countfile, "R"); Open a file in read-only mode

§array_count = 1; Define a variable that represents the position of the array element, using the

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 of the digits. It reads the values from left to right in Counter.inf, and then writes to an array called Counter_array, which is the index of the array starting from 1 (§array_count). If the number in Counter.inf now is 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, showing the code as follows:

Echo ("<table border=" 0 "height=" 5 "align=" center "> <tr> <td bgcolor=" #bab389 "align=" center "> welcome you, section");

for (§array_id = 1;§array_id<§array_elements; ++§array_id) {

Echo ("
}

Echo ("Guests </td> </tr> </table>");

}

The code above is simple, just draw a table and display the desired picture in the table in turn. In the Imagescounter 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 completed.

http://www.bkjia.com/PHPjc/532413.html www.bkjia.com true http://www.bkjia.com/PHPjc/532413.html techarticle PHP is now a popular server-side scripting language, it's very powerful, you can use it to do almost anything. Without the need for complex variables and code, you can make it very quickly ...

  • Related Article

    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.