PHP example Webzine editing and sending

Source: Internet
Author: User
Tags php example
As a language for building dynamic Web pages, PHP provides a simplified method to construct complex and powerful Web-related programs. Erik uses an original, real-world website example to demonstrate the basic principles of PHP. This series of articles is divided into two parts, part 1 introduces the basic concepts of PHP, and analyzes an application example, Webzine. As a language for building dynamic Web pages, PHP provides a simplified method to construct complex and powerful Web-related programs. Erik uses an original, real-world website example to demonstrate the basic principles of PHP. This series of articles is divided into two parts, part 1 introduces the basic concepts of PHP, and analyzes an application example, Webzine. Webzine includes an editing page on which the content provider can enter the document text and display the content to a front-end all over the world.
If you contact PHP for the first time, you may be pleasantly surprised to see how easy it is to use in practice. This article aims to give you a good impression on the way PHP works; then you can determine whether it is suitable for you.


1. PHP experience
This series of articles is divided into two parts, part 1 describes how this application works (not including installing PHP on your system ). You will have the opportunity to try out the primary index page and see how some components work behind the scenes. If this is your only motivation, you must check the next article. in this section, we will go into more details about the primary index page. (Part 1 will also use several Program examples to discuss editing components. You can download the source code and put your own ideas into practice .)
If you understand HTML and are familiar with any language similar to C (especially Perl), it is not a problem to understand the following example. Even if you have not used many languages similar to C, you can still understand these examples. However, you do need to understand the basic knowledge of HTML.
This PHP application sample is a Webzine that generates a reader. it has a simple sending module (the code cannot exceed 3 K ). This module displays a topic list.
Under each topic title is a series of articles in reverse chronological order. You can click the title to view the entire article. There is also a slightly more complex editing module that allows any reader to become the author and submit his/her own content. The author must select a category and enter the title, summary, and full story of the article. If you want to, you can enter the URL of an image file and click "preview" to check whether everything is ready. The input by the author must be verified-or even undergo a security check, which will convert all tags except a few security tags into non-active formats, this prevents dangerous or malicious HTML code execution. For example, the string is changed. This conversion actually invalidates the tag.


2. a dedicated PHP Technology
Before studying the actual Webzine code, let's first consider a simple example, which demonstrates the characteristics of PHP. PHP syntax allows you to mix HTML statements with PHP statements at will. This means that HTML statements can appear in the context of loops, if/else statements, and functions. I used this feature in the Webzine program, but the following code sample uses a simpler method to make it simpler.
Assume that we have two arrays, $ names and $ days, which contain information for each month of the year. Therefore, $ days [0] = 31 and $ names [0] = "January ", $ days [1] equals 28 and $ names [1] equals "February", and so on. The following technique is used to create a table containing the day and month names:

List 1: Create a table that contains days and months










For ($ I = 0; $ I <12; $ I ++) {// starts the loop.?> } // The loop ends.?>
Name Days




For clarity, the above PHP statement is displayed in red; the HTML statement is displayed in black. Note that the dedicated tag is switched from HTML to PHP, While?> Switch back to HTML.
Note that you can switch to PHP, start a for loop (or if/else, switch, or other statements), and return to HTML, the HTML commands you are entering will become part of the loop structure, so they are repeated along with the loop. You can enter or exit the PHP mode as needed (for example, send an echo statement as I did above to output the variable to the webpage. Then, when you return to PHP and enter an ending curly braces, the loop ends as expected.
The actual table looks as follows:

Name Days
January 31
February 28
March 31
2017l 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31


If this behavior is confusing, consider it as follows: the PHP interpreter replaces each row in HTML mode with an echo statement to send the row to the output stream. If the echo statement appears in the if/else structure, it is executed conditionally. If it appears in a loop structure (as shown above), it will be executed repeatedly.


3. Application Overview
The Webzine driver, index. php3, has three main components: topic menu, story list, and complete story representation. If the subject selected by the reader does not have any stories, some default texts are displayed. Editing pages are more complex. It contains a form, a feedback message, and a confirmation message. The form is used to accept user input and feedback messages to notify the author of the problems to be corrected, the confirmation message is used to display the content they submitted to the author. It also knows how to verify submitted stories and make necessary changes to ensure that the content does not contain any unapproved HTML and saves the stories in a story file, and update the corresponding menu file of this story.
This application has three data files: Category.txt contains a simple topic list, and the story is organized here. Each topic is associated with a topic menu file. The first topic must be "Main" and be associated with the topic menu file Main.txt. If the second topic is "TheArts", it is associated with the topic menu file named TheArts.txt. In the menu file, each story has an exclusive line of information: a story number, a title, a category, a simple abstract, and an optional image URL. Finally, the story file contains the actual body of a story. The s1.txt file contains the first submitted story. s2.txt contains the second submitted story, and so on. If you know the number of a story (for example, 26), it is easy to determine its file name (s26.txt ).


4. try Webzine!
Try this application before you study it carefully. Use the Webzine driver. It has a topic list on the left and a story list on the right. You can select a topic or select "Main" to view all stories. The first is the list of recent stories. if there are images in the first group of stories, these images will also be displayed. When you click the title of a story, a page containing the complete body of the story appears.
Trial editing page (you can also try it out from Webzine ). It provides a form that allows you to submit a story. When submitting content to Webzine, pay attention to the concept and taste of the masses. If you enter content that is not liked by the program, an error message is displayed. Once a story is accepted, you can return Webzine to see what it looks like to the reader.
Now that you have tried the application, read the following sections to learn about its creation process.


4. Webzine driver


Pass parameters
The index. php3 page allows passing parameters, as shown below: index. php3? Topic = TradeShow & story = 33



For the above call, the following variable assignment will be automatically performed before index. php3 is called:


$ Topic = "TradeShow ";
$ Story = 33;



If you ignore these parameters, the $ topic and $ story variables do not exist. You can explicitly check them, or ask PHP to return the default null value when you reference them.
Note: If this feature does not work On your system, check the php. ini file to ensure register_globals = On.
Webpage title
Let's first look at a common technology in many PHP applications; store some types of information into variable assignment statements starting with the program. This makes it easy for future maintenance and updates.
Listing 2: variable assignment
Similarly, pay attention to the PHP boundary tag: enables you to switch from HTML mode to PHP mode, while?> Switch back to HTML mode. You can switch back and forth any time. Some operations are easier to implement in HTML mode, while others are easier to implement in PHP mode. All you do is define two variables at the beginning of the program and then enter the HTML mode. When a variable is required, the PHP mode is returned, and an echo statement is issued to directly write the variable value into the webpage body.


$ Title = "PHP Demo Webzine ";
$ Slogan = "using Strating the coolness of PHP since September 2000 ";
?>



<? Php echo ($ title)?>







Category Menu
The Category.txt file is only a list of topic names. each topic list excludes one row. For example, if the file contains only three items:


Main
Politics
Technology



You will get three topic menu files: main.txt?politics.txtand policy.txt. Once the "Main" topic is selected, the driver will appear, as shown below:


Main
Politics
Technology


The HTML source code is as follows:







Main


Politics


Technology




Below is the PHP code from compiling Category.txt as the HTML code snippet shown above

Source code snippets extracted from the source code. First, set the file Category.txt

Read an array $ cats:


$ Cats = file ("category.txt ");
$ Elems = count ($ cats );
?>

The file function only copies files to an array. Therefore, $ cats [0] equals to "Main", $ cats [1] equals to "Politics", and $ cats [2] equals to "Technology ". The file function makes it easy to import a small ASCII text file, but do not use it for large files. The count function counts the number of elements in the array. Therefore, $ elems should be 3 in this example. The following describes how to use the array to create the preceding HTML table.

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.