How to use PHP to develop a simple navigation bar and URL collection function
The navigation bar and URL collection function are one of the common and practical functions in web development. This article will introduce how to use PHP language to develop a simple navigation bar and URL collection function, and provide specific code examples.
The following is a code example of a simple navigation bar interface:
<!DOCTYPE html> <html> <head> <style> .navbar { background-color: #333; overflow: hidden; } .navbar a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; } .navbar a:hover { background-color: #ddd; color: black; } </style> </head> <body> <div class="navbar"> <a href="#">Home</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Contact</a> </div> </body> </html>
The following is a code example that uses PHP to dynamically generate a navigation bar:
<!DOCTYPE html> <html> <head> <style> .navbar { background-color: #333; overflow: hidden; } .navbar a { float: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; } .navbar a:hover { background-color: #ddd; color: black; } </style> </head> <body> <?php $links = array( "Home" => "#", "About" => "#", "Services" => "#", "Contact" => "#" ); echo '<div class="navbar">'; foreach ($links as $text => $url) { echo '<a href="' . $url . '">' . $text . '</a>'; } echo '</div>'; ?> </body> </html>
The following is a simple PHP code example of the URL collection function:
<!DOCTYPE html> <html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="url" placeholder="Enter the URL"> <input type="submit" name="submit" value="Add to Favorites"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $url = $_POST["url"]; // 保存链接到文件或数据库 echo "Added to favorites: " . $url; } ?> </body> </html>
In the above code, we created a form where the user can enter the URL and click "Add to Favorites" button to save the link. After submitting the form, we can save the link to a file or database.
The above are the steps and code examples for using PHP to develop a simple navigation bar and URL collection function. With these examples, we can start developing our own navigation bar and URL collection features right away, and customize and extend them as needed.
The above is the detailed content of How to use PHP to develop simple navigation bar and URL collection functions. For more information, please follow other related articles on the PHP Chinese website!