Nested language PHP application tips 7 _ PHP Tutorial

Source: Internet
Author: User
7. nested PHP application skills. HypertextPreprocessor is an embedded HTML language and a popular Web programming language. It supports a wide range of back-end databases, covering almost all of the current PHPHypertext Preprocessor is an embedded HTML language and a popular Web programming language. It supports a wide range of back-end databases and covers almost all the current database systems. At the same time, it includes general language functions such as mathematical operations, time processing, file systems, string processing, and travel processing. In addition, it is a free system, making the cost-benefit ratio, almost equal to infinite.

The following are some tips when I use PHP for my website.

1. determine whether a function is supported

Since we can flexibly use modules supported by PHP to increase or decrease the number of modules supported by php, sometimes we always determine whether a module is loaded before using PHP. for example, check whether the GD graphic module is supported. use the following code:

 
 
  1. if(!function_exists("imagecreate"))
  2. ?>

Similarly, we can use similar code to test whether MSSQL, OCI, and other modules are supported.

2. change the URL to a hyperlink in the string.

When a form is submitted on a webpage, some URLs, such as personal homepage, are often displayed in the submitted plain text. if the URL is automatically converted into a hyperlink when displayed, that would be a pleasant task, just as the hyperlink automatically appears when you use WORD to edit a document. The following code implements its functions.

 
 
  1. $ String = "connecting to CCID website ";
  2. // Note: a space or carriage return is required after the connection.
  3. $ String = eregi_replace ("http: // ([^, rn] *)", "<a href = tarrget = _ blank> </a>", $ string );
  4. $ String = eregi_replace ("ftp: // ([^, rn] *)", "<a href = target = _ blank> </a>", $ string );
  5. Print $ string;
  6. ?>

3. use PHP to process multiple check boxes with the same name

If a form contains multiple check boxes with the same name, there is only one value when it is submitted to php, rather than a string of values separated by commas like asp. The solution is to use arrays. Add [] to the name of the check box, for example, change :. In this way, php will get an array called pp. In the submitted form, Count (pp) is used to determine the number of arrays, that is, the number of selected arrays, and then process the arrays separately.

The same principle applies to the problem of multi-choice in the drop-down box.

4. use static to display the color of a table in different rows

We use PHP to query data from the database and output the results to the browser. if there are many rows in the results, if the bgcolor (background color) of the table is all monochrome, the browser will feel uncomfortable. So how can we make the color of each row of the table different? See the following code:

 
 
  1. Function getcolor ()
  2. {
  3. Static $ colorvalue; // defines a static variable.
  4. If ($ colorvalue = "# eeeeee ")
  5. $ Colorvalue = "# F5F5F5 ";
  6. Else $ colorvalue = "# eeeeee ";
  7. Return ($ colorvalue );
  8. }
  9. Print ("<table border = 1> n"); // output 10 rows below
  10. For ($ I = 0; $ I <10; $ I ++)
  11. {
  12. $ Bcolor = getcolor (); // change the background color
  13. Print ("<tr bgcolor = $ bcolor> n ");
  14. Print ("<td> $ I </td> n ");
  15. Print ("</tr> ");
  16. }
  17. Print ("</table> n ");
  18. ?>

Note:

This program defines a static variable static $ colorvalue, which means that the variable $ colorvalue remains unchanged after the function call ends. When the getcolor () function is called again, the value of the variable $ colorvalue is the value of $ colorvalue at the end of the last function call.

5. avoid repeated references in php

As you know, in C, we can use # define to define a MACRO name, and check whether the MACRO name is defined to determine whether the header file is referenced. The same problem also exists in PHP. for example, A references B, C, and B to C. If no action is taken, C will be referenced twice. This may cause some strange problems. Solution: define a global variable and check whether the variable is defined to solve this problem. The solution is very simple, similar to C. I recommend that you use the naming rules of ["user_packages"] ["headfilename"] for this global variable.

 
 
  1. if (!emptyempty($GLOBALS["FOODTAILS"]["GLOBALDEFINE"])) return;
  2. $GLOBALS["FOODTAILS"]["GLOBALDEFINE"] = true;
  3. class FOODTAILS ;
  4. ?>

In addition, use require_once "headfiles. php" in the main program to avoid repeated references.

6. how to avoid repeated submission of forms

When we are doing a website, we often get bored with some texts. Sometimes, because the user does not know whether the submission is successful due to network conditions or other reasons, the same form will be submitted again, which leads to repeated submission of the form. There is a simple way to avoid repeated submission of the same form. First, define a session variable to save the submission serial number of a form. Here I define it as "$ userLastAction ". Add a hidden variable to the form and set the value to $ userLastAction + 1:> Finally, determine whether the form has been submitted before processing the submission.

 
 
  1. If ($ lastAction> $ userLastAction ){
  2. $ UserLastAction ++; // add 1 to the serial number
  3. // Process form data
  4. }
  5. ?>

The main principle of this technique is that users are not allowed to submit again after Rollback. that is to say, it is not allowed to modify and submit again after Rollback. However, the irrigation method of Ctrl-C/Ctrl-V cannot be avoided.

7. backfill of the number of downloaded files

When downloading software, we often see the number of downloads of the software, which makes it easy for the website administrator to analyze the popularity of the software. The implementation principle is: the unique identifier and number of software downloads are stored in the backend database. when users download the software, they first update the number of downloads of the corresponding software, that is, the number of downloads is increased by 1, then, go to the downloaded file to officially start the download. See the following implementation code:

A. create a data table download (MySql is used at the backend)

The download table contains the following two fields:

The unique identifier of the downloaded object.

Downnum downloads. the default value is 0.

B. implementation

Assume that the file name has been extracted from the database: the description file name is five-stroke; the download file name is wbzx.zip; the id value is 2

The generated hyperlink code is:

Five strokes

The following code downloads. php processes the total number of downloads and locates the downloaded files based on the transferred file name.

 
 
  1. // Code for connecting to the Mysql database
  2. $ Res = mysql_query ("update download set downnum = (downnum + 1) where id =" $ id "", $ db );
  3. Header ("location: $ filename ");
  4. ?>


Hypertext Preprocessor is an embedded HTML language, which is also a popular Web programming language. It supports a wide range of back-end databases, covering almost all of the current...

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.