Home >Backend Development >PHP Tutorial >How to develop a travel guide website using PHP and CGI
How to use PHP and CGI to develop a travel guide website
In modern society, tourism has become an indispensable part of people's lives. More and more people choose to explore new places and experience different cultures during holidays or leisure time. In order to meet the needs of tourists, it is necessary to develop a comprehensive travel guide website. This article will introduce how to use PHP and CGI to develop a comprehensive travel guide website.
1. Overview
The development of travel guide websites requires the support of server-side languages and databases. In this article, we will use PHP as the server-side language and combine it with the advantages of CGI to achieve a better user experience. We will use MySQL as the database to store the website's data.
2. Environment preparation
Before you start, you need to ensure that PHP, CGI and MySQL database are installed on your server. You can install and configure these software according to the official documentation.
3. Database design
First, we need to create a database named "tourist_guide". You can use phpMyAdmin or the MySQL command line tool to accomplish this step.
Create a table named "attractions" to store information about tourist attractions. The structure of the table is as follows:
CREATE TABLE attractions ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT NOT NULL, image VARCHAR(255) NOT NULL );
4. Website development
First, we need to create the basic structure of the website , including the header, navigation bar, body and bottom. Here is a simple example:
<!DOCTYPE html> <html> <head> <title>Tourist Guide</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <h1>Tourist Guide</h1> <nav> <ul> <li><a href="index.php">Home</a></li> <li><a href="attractions.php">Attractions</a></li> <li><a href="contact.php">Contact</a></li> </ul> </nav> </header> <main> <!-- 网站主体内容 --> </main> <footer> © 2021 Tourist Guide </footer> </body> </html>
In the main part, we will display a list of tourist attractions, and users can click on the attraction names to view detailed information. Here is an example:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "tourist_guide"); // 查询所有旅游景点 $query = "SELECT * FROM attractions"; $result = mysqli_query($conn, $query); // 显示景点列表 echo "<ul>"; while ($row = mysqli_fetch_assoc($result)) { echo "<li><a href='attraction.php?id=" . $row['id'] . "'>" . $row['name'] . "</a></li>"; } echo "</ul>"; // 关闭数据库连接 mysqli_close($conn); ?>
When the user clicks on the attraction link, we will display the detailed information of the attraction based on the attraction ID passed. The following is an example:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "username", "password", "tourist_guide"); // 获取景点ID $attraction_id = $_GET['id']; // 查询景点信息 $query = "SELECT * FROM attractions WHERE id = " . $attraction_id; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); // 显示景点详细信息 echo "<h2>" . $row['name'] . "</h2>"; echo "<p>" . $row['description'] . "</p>"; echo "<img src='" . $row['image'] . "' alt='" . $row['name'] . "'>"; // 关闭数据库连接 mysqli_close($conn); ?>
5. Summary
Through the above simple example, we can see that it is not complicated to develop a travel guide website using PHP and CGI. We first design the database and then write PHP code to implement the functionality of the website. With proper organization and design, we can develop a fully functional and user-friendly travel guide website.
However, this is just a basic example, you can extend the functionality and optimize the user experience according to your needs and creativity. I hope this article can provide you with some help, and I wish you smooth development of your travel guide website!
The above is the detailed content of How to develop a travel guide website using PHP and CGI. For more information, please follow other related articles on the PHP Chinese website!