Next, let's use the PHP example to describe Webzine editing and sending (2) 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. Part 2 and 4 of this series of articles describe how the sending module presents a story to the reader. the previous PHP example shows how to edit and send Webzine. (2)
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. Part 3 and 4 of this series describe how the sending module presents a story menu to the reader and how the editing module enables the author to submit the story to Webzine.
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.
Introduction
In part 2 of this article, I introduced the sending part of Webzine, a simple PHP application. Although there is only about 3 K code, it contains many features. You have tried this application and started to study how it works in the background. I explained how the application displays the category menu to the user and displays the story based on the selection. I also gave readers some knowledge about how PHP applications work and how they receive parameters from the call page.
This article is part 1. First, you will learn how the sending module presents the story menu to the reader, and then you will quickly check the editing module that enables the author to submit the story to Webzine.
Story menu
Each story in the file has a corresponding line in the topic menu file. I have decided to use the delimiters ("^") as delimiters to separate different items in the file, but you can use any character (tab is a common choice ). The basic layout is as follows:
Story No. ^ title ^ image URL ^ Abstract
.
For example, the topic menu file (TradeShow.txt) that contains only three stories may be as follows:
Listing 1. topic menu file containing three stories
33 ^ Great New Products This Year ^/images/proddemo.jpg ^ Thursday's product demo...
12 ^ Opening Event Well Attended ^/images/opnfoto.jpg ^ Ticket sales to...
5 ^ Trade Show Opens ^/images/tradelogo.gif ^ The Fourth Annual Trade Show...
The menu driver accepts this information and provides readers with a story overview. The method is as follows:
First, the driver copies the appropriate entry number from the topic menu file to the array $ stories. For the above example, $ stories [0] contains the first line (story number is 33), $ stories [1] contains story number 12, $ stories [2] contains story number 5. You also need to count the number of elements in the array and save it in the variable $ numstories.
Next, the driver provides this information to the user, as shown below:
List 2. drivers
For ($ I = 0; $ I <$ numstories; $ I ++ ){
$ Storyinfo = split ("\ ^", $ stories [$ I]);
$ Storynum = $ storyinfo [0]; // story number (for example, 33)
$ Storydesc = $ storyinfo [1]; // The title of the story (for example, "Great New Products This Year ").
$ Storyimg = $ storyinfo [2]; // image URL (for example, "/images/proddemo.jpg ").
$ Dtext = $ storyinfo [3]; // story summary (longer text description ).
$ Url = "$ storydesc
";
If ($ I <10 ){
$ Url = "". $ url ."";
If ($ storyimg! = ""){
$ Url ="
. "Src = \" $ storyimg \ ">
\ N"
. $ Url;
}
$ Url = "". $ url;
} Else {
$ Url = "". $ url ."";
}
Echo ("$ url \ n ");
Echo ("
$ Dtext
\ N ");
}
The split function copies the story content from a string to an array. Note that a backslash is added before the escape character to become an escape character. This program converts an array node to a variable name with a higher description, making the code easier to read. Then the URL is constructed. The if/else structure processes the first 10 stories differently from other stories. The first 10 stories have a separation line, marked with H2, and contain a picture (if any ). The rest of the story is marked with H3, with no separation lines and no photos. This process highlights the latest stories and retains older ones for access by readers who need them. Note that an image numbered with an odd number is right aligned, while an image numbered with an even number is left aligned.
As the end of this example, the HTML code generated for the above theTradeShow.txt file is as follows:
Listing 3. HTML code generated for the theTradeShow.txt file
Great New Products This Year
Thursday's product demo has Ded a couple of exciting new
Surprises from competing firms in the industry.
Opening Event Well Attended
Ticket sales to the Trade Show opening event were
Up 15% from last year's show.
Trade Show Opens
The Fourth Annual Trade Show opened 10/11/2000 to an enthusiastic injection
From attendees.
Click here to view the source code of index. php3. You may want to view the entire source code before starting the next part. After all, the code size is less than 3 kb!
Edit page "author. php3"
The analysis of the editing page is not as detailed as the analysis of the sending module, because it is long and we have discussed many basic concepts.
To briefly describe the basic structure of a PHP function, let's take a look at the complain function in the editing module. This is a simple function, but it is a good entry-level material.
Listing 4. a PHP function example
// Notify the user of input problems.
Function complain ($ problem ){
Global $ status;
If ($ status! = "") $ Status = $ status ."
\ N ";
$ Status = $ status. $ problem;
}
You can call this function anywhere in the code. For example, complain ("The URL is too long. "); receives the global variable $ status and appends the new error message to its tail. if $ status is not empty, add a line break before the error message.
Prepend
The prepend function is one of the most important functions in the editing module. It adds a new entry to the first line of the file. Next, I will use it to add the author's new submission to the beginning of the topic menu file.
Listing 5. prepend function
// Add a new line at the beginning of an existing file.
Function prepend ($ file, $ string ){
// To be improved-use the database to improve the scalability of this algorithm.
// Currently, it is limited to 100 stories.
If (file_exists ($ file )){
$ Filetext = file ($ file );
$ Lines = count ($ filetext );
} Else {
$ Lines = 0;
}
If ($ lines> 100) $ lines = 100;
$ Handle = fopen ($ file, "w ");
Fputs ($ handle, "$ string \ n ");
For ($ I = 0; $ I <$ lines; $ I ++)
Fputs ($ handle, $ filetext [$ I]);
Fclose ($ handle );
}
If the file exists, the entire body is saved to the $ filetext array. Then count the number of trips. If the object does not exist, it is considered to have zero rows. The length is limited to 100 rows (actually 101 rows, because the first row is added before the count starts ). When the file is opened, the program first writes the current string to the file, and then writes the remaining rows (up to 100 rows) to the file. Any content after these 100 rows will be discarded.
This function depends on PHP
Several important built-in file processing functions. The following table describes the operations performed by each function.
Function description
File_exists (string) uses the string as the file name. if the file name currently exists, true is returned.
File (string) uses a string as the file name and returns the object body with an array. each row corresponds to an array element.
Note that the line terminator is located at the end of each line in the array.
Count (array) returns the number of elements in the array.
The fopen (string, mode) string is a file name. The mode is a string. for example, "w" indicates writing, and "r" indicates reading.
Open the file and return a handle.
Fputs (handle, string) writes the string to the specified output file. A file handle must be specified,
The file name cannot be specified as a string.
Fclose (handle) closes the file and terminates the output.
Save
The save function writes the author's input to a file. Because this function is too long,
Here I cannot analyze it line by line, but I will emphasize several interesting statements.
$ Entry = "$ storynum ^ $ storytitle ^ $ storyimg ^ $ synopsis ";
The preceding statement connects $ storynum, $ storytitle, $ storyimg, and $ synopsis variables and uses the delimiters as separators.
Nested substitution of variables makes it easier and more interesting than explicit connections.
Prepend ("Main.txt", $ entry );
$ Topicfile = "your subject.txt ";
Prepend ($ topicfile, $ entry );
The $ entry variable value is added to the main story menu file and the topic-specific story menu file. Therefore, if the variable $ subject is equal to "Politics", it will be added to the file "Politics.txt.