How to make your own counters in PHP

Source: Internet
Author: User
Using PHP to implement the counter is very simple, the implementation of the counter is divided into two stages: one is to achieve the count, the second is to realize the digital display, and in the implementation of the count, there are two ways: one is based on the file count, and the second is based on the database count method.

1. File-based Count

<?  if (!file_exists ("Visit_log/counter.txt")) {  $fp =fopen ("Visit_log/counter.txt", "w");  Fwrite ($fp, "000001"); Assuming a maximum of 6 bits, you can modify  $counter = "000001" as needed;  Fclose ($FP);  } Check to see if the Counter.txt file exists, create a new file if it does not exist, and write "00001".  else{  $fp =fopen ("Visit_log/counter.txt", "R");  $counter =fread ($FP, 6);  -----------------------------  $counter +=1;  -----------------------------  fclose ($fp);  Switch (strlen ($counter)) {//counter variable is formatted so that it looks beautiful case  1:  $counter = "00000". $counter;  break;  Case 2:  $counter = "0000". $counter;  break;  Case 3:  $counter = "$". $counter;  break;  Case 4:  $counter = "xx". $counter;  break;  Case 5:  $counter = "0". $counter;  break;  }  $FP =fopen ("Visit_log/counter.txt", "w");  Fwrite ($fp, $counter);  Fclose ($FP);  }  ? >

2. Database-based Count

<?  $CN =mysql_connect ("localhost", "root", "******");  mysql_select_db ("Visit_log", $CN);  $sql = "SELECT * from Visit_counter";  $result =mysql_query ($sql, $CN);  $record =mysql_fetch_array ($result);  if (empty ($visited)) {  $counter = $record ["Amount"]+1;  $sql = "Update visit_counter set amount= $counter";  $result =mysql_query ($sql, $CN);  }  Mysql_close ($CN);  Switch (strlen ($counter)) {//counter variable is formatted case  1:  $counter = "00000". $counter;  break;  Case 2:  $counter = "0000". $counter;  break;  Case 3:  $counter = "$". $counter;  break;  Case 4:  $counter = "xx". $counter;  break;  Case 5:  $counter = "0". $counter;  break;  }? >

Summarize:

The above in two ways to achieve the Count function, careful netizen may see counter variable seems useless. It is actually the variable we use to display it.

Related recommendations:

PHP Counter Program

PHP counter Program: Solve all possible problems _php tutorial

PHP Counter Program _php Basics

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.