Home  >  Article  >  Backend Development  >  How to use PHP to develop simple navigation bar and URL collection functions

How to use PHP to develop simple navigation bar and URL collection functions

王林
王林Original
2023-09-20 15:14:121320browse

How to use PHP to develop simple navigation bar and URL collection functions

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.

  1. Create a navigation bar interface
    First, we need to create a navigation bar interface. The navigation bar usually contains links for quick navigation to other pages. We can use HTML and CSS to design and arrange these links.

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>
  1. Use PHP to dynamically generate the navigation bar
    Next, we can use PHP to dynamically generate the navigation column. In this way, when we add or delete navigation links, we do not need to manually modify the HTML code, but only by modifying the PHP code.

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>
  1. Add URL collection function
    In addition to the navigation bar, we can also add a URL collection function , allowing users to save links to their favorite web pages.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn