How to use PHP to make a navigation bar for a Web page

Source: Internet
Author: User
Keywords How to use PHP to make a navigation bar for a Web page
How to use PHP to make a navigation bar for a Web page
Original: Brad Bulger
Translation: Li Ping
Translator Note: This article, "Site Navigation with PHP", the original text detailed how to use PHP programming to make the desired effect of the Web navigation bar, this article only selected some of the articles, the selected part is the essence of the article, as long as we can understand this part of the content can be used the same principle, Thought makes the effect we need, hoping to give the reader a chance to play a role. This article only needs the reader to have the basic knowledge of PHP, HTML can basically read.
 
PHP is powerful for database-driven Web sites (making Database-driven sites), but can we use it to do something else? PHP gives us all the tools we expect: The loop structure of for and while, mathematical operations, and so on, there are two ways to refer to a file: direct reference or request to the server. In fact, let's look at an example of how to use it to do the navigation bar:
The 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 procedure is finished.

Code Analysis:
Ok! Before doing enough work on the groundwork, let's look at how to use PHP to do the 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 get the value of the environment variable, and the Request_uri value is the part URL immediately following the hostname, if the URL
Is http://www.yourmom.com/dinner/tuna_1.html, then its value is/dinner/tuna_1.html. Now we will get the part of the URL in the variable $full_path, and then use the DirName () function to fetch the file directory from the URL, using the basename () function to obtain the filename, using the above example dirname () return value:/dinner/ ; basename () returns: Tuna_1.html. The next section is relatively tricky, assuming that our filenames are named in story_x format, where x represents the page number, we need to extract the page numbers we used. Of course, the file name is not necessarily a single-digit pattern or only one underline, it can be tuna_2.html, it can also be called tuna_234.html or even candy_apple_3.html, and what we really want is to be located in the last "_" and "." HTML "between the east and the east. You can use the following methods:
*/
$page _num = substr ($page _file
, Strrpos ($page _file, "_") + 1
, Strpos ($page _file, ". html")-(Strrpos ($page _file, "_") + 1)
);
/*
substr ($string, $start, [$length]) function gives us string $string from $start, which is $length or to the end (the arguments in square brackets are 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 starting at the beginning of the string is "0" instead of "1".
The function Strrpos ($string, $what) tells us where the string $what in the last occurrence of the variable $string, which we can use to find out where the last underscore in the file name is, in the same vein, followed by Strpos ($string, $what) Tell us where ". html" first appeared. We use these three functions to get the number between the last "_" and ". html" (Strpos () +1 in the code means crossing "_" itself).
The rest of the section is simple, first constructing the file name for the previous page and the next page:
*/
$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 a mathematical operation $page_num+1 into a string type so that it can be used to eventually connect with other strings to become the file name we need.
*/
/*
Now check that the file exists (this code assumes all the files are in the same directory) and eventually gives the HTML code that makes up 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";
}
 
?>
(End of full text)

"The copyright of this article is owned by the author and house Orso near net, if need to reprint, please specify the author and 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.