How to use PHP to provide a navigation bar for webpages

Source: Internet
Author: User
How to use php to create a navigation bar for a webpage original article: BradBulger translation: Li Ping translator note: This article is formerly known as SiteNavigationwithPHP. The original article details how to use PHP programming to make a page navigation bar with ideal results, this article only selects some of the articles, and the selected part is the essence of the article, as long as you can understand this part of the content, you can use the same principles and ideas to make the results we need. we hope to give readers a reference on how to use php to provide a navigation bar for webpages.
Original article: Brad Bulger
Translation: Li Ping
Note: In the original article Site Navigation with PHP, the original article details how to use PHP programming to make a webpage Navigation bar with ideal results. This article only selects some of the articles, the selected part is the essence of the article. as long as you can understand this part, you can use the same principle and idea to make the desired results, it is hoped that readers will be able to play a leading role. This article only requires the reader to have a preliminary understanding of PHP and HTML.
 
As we all know, PHP is powerful for database-driven websites (making database-driven sites), but can we use it to do other things? PHP gave us all the tools we expected: the loop structure of for and while, mathematical operations, etc. you can also reference the file in two ways: directly reference or apply to the server. In fact, let's look at an example of how to use it as a navigation bar:
Complete Original code:

# And this '# 'makes this a PHP comment.
 
$ Full_path = getenv ("REQUEST_URI ");
 
$ Root = dirname ($ full_path );
$ Page_file = basename ($ full_path );
$ Page_num = substr ($ page_file
, Strrpos ($ page_file, "_") + 1
, Strpos ($ page_file, ". html")-(strrpos ($ page_file, "_") + 1)
);

$ Partial_path = substr ($ page_file, 0, strrpos ($ page_file ,"_"));
 
$ PRev_page_file = $ partial_path. "_". (string) ($ page_num-1). ". html ";
$ Next_page_file = $ partial_path. "_". (string) ($ page_num + 1). ". html ";
 
$ Prev_exists = file_exists ($ prev_page_file );
$ Next_exists = file_exists ($ next_page_file );
 
If ($ prev_exists)
{
Print "previous ";
If ($ next_exists)
{
Print "| ";
}
}
If ($ next_exists)
{
Print "next ";
}
 
?> // The original program is complete.

Code analysis:
OK! We have done enough preparations before. now let's take a look at how to use PHP to complete this job:
 

# And this '# 'makes this a PHP comment.
 
$ Full_path = getenv ("REQUEST_URI ");
 
$ Root = dirname ($ full_path );
$ Page_file = basename ($ full_path );
 
/*
The PHP function getenv () is used to obtain the value of the environment variable. The value of REQUEST_URI is the part of the URL that follows the host name.
Is the http://www.yourmom.com/dinner/tuna_1.html, then its value is/dinner/tuna_1.html. now we put the obtained URL in the variable $ full_path, and then use the dirname () function to capture the file directory from the URL and use the basename () function to get the file name, in the preceding example, dirname () returns/dinner/; basename () and tuna_1.html. The next part is relatively skillful. if our file name is named in story_x format, where x represents the page number, we need to extract the page number we use. Between shards. The following method can be used:
*/
$ Page_num = substr ($ page_file
, Strrpos ($ page_file, "_") + 1
, Strpos ($ page_file, ". html")-(strrpos ($ page_file, "_") + 1)
);
/*
Substr ($ string, $ start, [$ length]) the function gives us a string starting from $ start and ending with $ length or ending with $ length (the parameter in square brackets is optional. if $ length is omitted, substr will return the string from $ start to the end of the string), as every good C programmer tells you, the number indicating the start position of the string is "0" rather than "1 ".
The strrpos ($ string, $ what) function tells us where $ what is the last occurrence of the string in the $ string variable. we can use it to find out where the last underline is in the file name, similarly, the first appearance position of strpos ($ string, please what? .".html. We can use these three keywords to get the numbers between the last two values (strpos () + 1 in the code indicates that the number is beyond ).
The rest is simple. first, construct the file name for the previous and next pages:
*/
$ Partial_path = substr ($ page_file, 0, strrpos ($ page_file ,"_"));
 
$ Prev_page_file = $ partial_path. "_". (string) ($ page_num-1). ". html ";
$ Next_page_file = $ partial_path. "_". (string) ($ page_num + 1). ". html ";
 
/*
(String) ($ page_num + 1) converts the result of the mathematical operation $ page_num + 1 to the string type, which can be used to connect to other strings to form the file name we need.
*/
/*
Check whether the file exists (this code assumes that all the files are in the same directory) and finally provides the HTML code that constitutes the page navigation bar.
*/
$ Prev_exists = file_exists ($ prev_page_file );
$ Next_exists = file_exists ($ next_page_file );
 
If ($ prev_exists)
{
Print "previous ";
If ($ next_exists)
{
Print "| ";
}
}
If ($ next_exists)
{
Print "next ";
}
 
?>
(Full text)

[This article is copyrighted by the author and osuo. if you need to reprint it, please indicate the author and its source]


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.