Use PHP to implement dynamic web pages

Source: Internet
Author: User
Tags php database how to use ftp
Today, Web sites are desperately trying to provide users with things they have never experienced before. In addition to friendly interfaces, meticulous services, and practical information, dynamic Web content customized for users can also improve the usefulness of Web sites and enhance the targeted access, this makes it more likely for users to visit your website again in the future. In this article, we will try our best to provide users with things they have never experienced. In addition to friendly interfaces, meticulous services, and practical information, dynamic Web content customized for users can also improve the usefulness of Web sites and enhance the targeted access, this makes it more likely for users to visit your website again in the future. In this article, we will begin with an overview of dynamic information. Then we will explain how to use PHP to create dynamic content on the Web page and view a demo.

Dynamic Information theory

According to Merriam-Webster Online, the word "dynamic" means that the content of a webpage is composed of "normally continuous and real-time generated activities or changed information ." Therefore, when talking about dynamic data, we are talking about the information sent to users as Web pages, which is composed of different source data. This is relative to the concept of static web pages. the content of static web pages does not depend on the data entered by users and is usually directly parsed to users. There are three main types of dynamic information on the web page:

  • Dynamic Data-variables generated on a Web page.
  • Dynamic Web page-the entire Web page is dynamically generated.
  • Dynamic content-some of the web pages are dynamically generated.

If you want to slightly control the process of generating dynamic content, just like the type of dynamic data, the data processing process will be more complicated. If you want to generate a large range of information, just like the dynamic web page generation, the logic of the program will become complicated. Dynamic content generation is a compromise between the two methods. it allows us to use two useful PHP functions, include () and require ().

Remember, the more logic you add to the backend, the more serious the performance of your Web site will be lost. Fortunately, PHP can perform preprocessing smoothly, so I use as many PHP functions as possible when processing dynamic content and data.

Data source and PHP functions

All dynamic content has one thing in common: they come from a data source outside the original page. Figure A lists some common data sources and their corresponding PHP functions.

Figure

Data source
PHP functions
Note
User
$ HTTP_POST_VARS
$ HTTP_GET_VARS
These functions process data directly input by users through Web forms.
Database (local or remote)

_ Connect ()
_ Pconnect ()
_ Close ()
_ ()
Example:
Mysql_fetch_array ()

These are only part of many PHP database access functions, and many functions are specially written for each different database. You can find a complete list of these functions in the PHP Function Reference Manual.
Remote file
Fopen (), fclose ()
Fgets (), fputs ()
These functions process data in files on a remote server, which can be accessed through FTP.
Local file
Include (), require ()
Fopen (), fclose ()
These functions process data in files on the local server, such as configuration files.
Common data sources and PHP functions that process them

In this article, "tutorial: PHP started," We watched a demo script that asked users to enter their favorite numbers. Based on the user input results, a message is displayed on the web page. This is an example of user-driven dynamic Web content. The results returned from the Web form determine the displayed content. A more complex example is the "click process" application, which can decide which advertisement to send to a user or her based on the page that the user has accessed on the Web site.

Once the data has been input, whether it is input by the user or other means, it will be saved in a database and used again later. If it is used to determine the displayed content, the content can be considered as "database-driven dynamic content ." We will take a closer look at this type of dynamic information in the next article. At present, let's first look at a simple PHP script with file-driven dynamic content. We will use the logic of a configuration file to determine what page style and font should be displayed on the web page. The selected page style will be displayed when the user requests the web page. (Here I want to remind you of the example of a file: you should use the style page in this example to complete the required functions .)

Example program: Display. php

The Display script uses an independent configuration file to include variable values and HTML-containing variables. Although this does not seem very dynamic, you can easily ask users to use Web forms to create a configuration file and use certain logic to determine which configuration file should be loaded, and so on. (Our discussion in the article "understanding PHP functions and classes" will help you complete this job .)

Due to the purpose of this article, we will skip this process and try to simplify it. Table A shows our home page and the page you call through A browser, Display. php. (PHP code will be displayed in bold .)

Table

This simple code must do three things:

  • Use the PHP include () function to include
          Mood Page 
       

    This is the best "mood page" ever!
    Variables in Displayconf. php and evaluate them.
  • Create a variable that represents the file name requested by the user. In this example, the variable $ display defined in the Displayconf. php file is evaluated, and the. php suffix is added. (This is done by our logic .)
  • Use the PHP require () function to display the correct content in the file.

In our example, PHP require () and include () functions are completely interchangeable. The main difference between the two functions is that the processing method of the target file is different. A require () statement will be replaced by the file it calls. This means that in a loop, remote files are called only once. On the other hand, when the include () function is encountered every time, it will be re-evaluated. This means that during a loop, the file will be accessed once during each loop, and the variables set in the included files will be updated each time.

In this example, I try to explain when the function is suitable. For the Displayconf. php file, it is very likely that the variable value in it has changed. After all, this is a configuration file. Therefore, I chose the include () function. On the other hand, the $ required file may not change during interaction. If the user requests different file bodies, we may create a new file and include it. Therefore, I use the require () function.

Advanced users may want to see the PHP Manual to learn more about the functions require_once () and include_once () so that they can better control file processing and management of configuration file variables.

Table B shows our configuration file Displayconf. php. (For simplicity, we will put all the files in the same directory as the Web server .) What we need to do here is to set the $ display variable to an optional value.

Table B

# Displayconf. php # configuration file of display. php # ----------------------------------------------- # set the variable $ display to one of the following values: # happy, sad, or generic $ display = "happy";?>

Finally, we need some content files-corresponding to every option in the configuration file. Because these contents are static HTML, we do not need to add the PHP script in the file. When you use the include () or require () function in PHP, the called file is skipped at the beginning of the processing process and then added at the end of processing.

"Happy" file content (happy. php)

"Sad" file content (sad. php)

"Generic" file content (generic. php)

When you click Display. php, the appearance and feeling of the page will change according to the value entered in the configuration file.

Summary

In this article, we discuss the basics of dynamic information and use a script to create dynamic content driven by a file. Specifically, we use the include () and require () PHP functions to extract and send our data.

The following are some final words. Although I believe you will be familiar with WAI Web programming guidelines, you may also need to look at W3C's control over dynamic content and the ability of users to access it. You may also need to take a look at this chapter "use remote files" in the PHP Manual to learn how to use FTP to extract configuration data.

 
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.