Use PHP to create a dynamic Counter

Source: Internet
Author: User

PHP is a popular server-side scripting language. It 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 =" 0 "height =" 5 "align =" center "> <tr> <td bgcolor =" # bab389 "align =" center "> welcome, ");

For (§ array _ id = 1; § array _ id <§ array _ elements; ++ § array _ id ){

Echo (" ");

}

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

}

The code above is very simple, that is, to draw a table and display the required images in sequence in the table. Ten images of 0.gif to 9.gif are displayed in the imagescounterdirectory. The for loop traverses the array, and the corresponding image of each digit is displayed 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.