How to Use PHP to create a dynamic Counter

Source: Internet
Author: User

When creating a website, we usually have the function of adding counters to the webpage. Today, we will introduce the functions related to PHP dynamic counters in this article.

Is very powerful, you can use it to do almost anything. Without Complex Variables and code, you can quickly make beautiful counters. Next, let's do it step by step.

In fact, everyone knows how to create a dynamic counter in PHP. First, a record file, such as counter.txt or counter. log, adds 1 to this file every time you access this page, and then displays the result. Therefore, our PHP code should start like this:

 
 
  1. <? PHP
  2. $ Countfile = "js/counter. inf ";
  3. // 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.
  4. If ($ fp = fopen ($ countfile, "r +") = false)
    {// Open the file in read/write mode. Exit if the file cannot be opened.
  5. Printf ("Open file % s failed! ", $ Countfile );
  6. Exit;
  7. }
  8. Else
  9. {
  10. // If the file can be opened normally, the data in the file will be read, assuming 1
  11. $ Count = fread ($ fp, 10 );
  12. // Read 10-Bit Data
  13. $ Count = $ count + 1;
  14. // Count ++
  15. Fclose ($ fp );
  16. // Close the current file
  17. $ Fp = fopen ($ countfile, "w + ")
  18. // Open the file in overwrite Mode
  19. Fwrite ($ fp, $ count );
  20. // Write new data after 1
  21. Fclose ($ fp );
  22. // Close the file
  23. }

At this time, the entire PHP production of dynamic Counters is complete, if only a simple text count, here you can output the value of the variable $ count. The following code converts $ count to image mode:

 
 
  1. $ Fp = fopen ($ countfile, "r ");
    // Open the file in read-only mode
  2. $ Array_count = 1; // define a table
    Shows the variable at the position of the array element, which must be used below
  3. While (! Feof ($ fp )){
  4. $ Current_number = fgetc ($ fp );
  5. $ Counter_array [$ array_count] = $ current_number;
  6. $ Array_elements = count ($ counter_array );
  7. $ Array_count = $ array_count + 1;
  8. }

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 PHP dynamic Counter Display code is as follows:

 
 
  1. 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>
  2. <Td bgcolor = ($ % $ 43% ^ # ASD #2 $ # f $ % ^ )#
    Bab389 ($ % $ 43% ^ # ASD #2 $ # f $ % ^)
  3. Align = ($ % $ 43% ^ # ASD #2 $ # f $ % ^) center ($ % $ 43% ^ # ASD #2 $ # f $ % ^)
  4. > Welcome ");
  5. For ($ array_id = 1; $ array_id <$ array_elements; ++ $ array_id ){
  6. Echo (& lt; img src = http://www.asp315.com
    /News/2/($ % $ 43% ^ # ASD #2 @ $ # f $ % ^) images
    // Counter // specify counter_array?$array_id=.gif ($ % $ 43% ^ # ASD #2 @$ # f $ % ^)
    Align = absmiddle> ");
  7. }
  8. Echo ("guest </td> </tr> </table> ");
  9. }

The above PHP code for making a dynamic counter is very simple, that is, to draw a table and display the desired image 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.