Home >Backend Development >PHP Problem >How to jump to the details page in php
With the continuous development of the modern Internet, more and more websites use information lists and corresponding detailed information pages to display different categories of information. In the process of website development, how to realize the jump from the information list page to the detailed information page is a very important step. This article will introduce how to use PHP to achieve this function.
1. Prerequisite knowledge
Before studying this article, we need to master the following prerequisite knowledge:
2. Implementation method
Let’s take a look at how to implement this function :
First, we need to build a table in the database to store the information list and its details. Let's take "products" as an example and create a table named "products" in the database, including the following fields:
Next, we need to build an information list page to display different products. We use HTML and CSS to build this page, as shown below:
<!DOCTYPE html> <html> <head> <title>产品列表</title> <style> .product { margin-bottom: 30px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; display: inline-block; width: 30%; vertical-align: top; margin-right: 3%; margin-left: 3%; } .product img { margin-bottom: 10px; width: 100%; height: 200px; object-fit: cover; } .product h2 { font-size: 20px; margin-bottom: 5px; } .product p { font-size: 16px; margin-bottom: 10px; } .product a { display: block; text-align: center; background-color: #333; color: #fff; padding: 10px; border-radius: 5px; text-decoration: none; } </style> </head> <body> <div class="product"> <img src="https://via.placeholder.com/200x200.png?text=Product+1" alt="Product 1"> <h2>Product 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a href="">详细信息</a> </div> <div class="product"> <img src="https://via.placeholder.com/200x200.png?text=Product+2" alt="Product 2"> <h2>Product 2</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a href="">详细信息</a> </div> <div class="product"> <img src="https://via.placeholder.com/200x200.png?text=Product+3" alt="Product 3"> <h2>Product 3</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a href="">详细信息</a> </div> </body> </html>
We use div elements to display different products, mainly including pictures, titles, descriptions and "details" buttons. But we haven't added any functionality to the "Details" button yet.
Now, we need to jump to the detailed information page of the product after clicking the "Details" button. We can use PHP to achieve this function.
First, add URL parameters to each "Details" button to mark the ID of the current product. For example:
<a href="details.php?id=1">详细信息</a>
In this example, we use "details.php" as the URL of the details page, and pass the product ID parameter "id=1" to the page.
Then, we need to use PHP in the details page to get this parameter and get the product details from the database. First, we need to add the following code to the details page to get the URL parameters:
<?php if (isset($_GET['id'])) { $id = $_GET['id']; } else { header('Location: index.php'); exit; } ?>
In this code, we use the isset() function to check whether the "id" parameter exists in the $_GET array, if If it exists, get the value and assign it to the $id variable. Otherwise, we redirect the user to the information list page and use the header() function to implement the jump.
Next, we need to use SQL query statements to obtain the detailed information of the product from the database and display it on the page. We can use the following code to achieve this:
<?php $db = mysqli_connect('localhost', 'username', 'password', 'database'); $query = "SELECT * FROM products WHERE id = $id"; $result = mysqli_query($db, $query); if (mysqli_num_rows($result) == 0) { echo '产品不存在'; exit; } else { $product = mysqli_fetch_assoc($result); } ?> <!DOCTYPE html> <html> <head> <title><?php echo $product['name']; ?></title> <style> .product { margin-bottom: 30px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; overflow: hidden; width: 100%; } .product img { margin-bottom: 10px; width: 100%; height: 200px; object-fit: cover; } .product h2 { font-size: 20px; margin-bottom: 5px; } .product p { font-size: 16px; margin-bottom: 10px; } </style> </head> <body> <div class="product"> <img src="<?php echo $product['image']; ?>" alt="<?php echo $product['name']; ?>"> <h2><?php echo $product['name']; ?></h2> <p><?php echo $product['description']; ?></p> <p>价格:<?php echo $product['price']; ?></p> </div> </body> </html>
In this code, we first use the mysqli_connect() function to connect to the MySQL database, and use the SELECT statement to obtain the product data corresponding to the ID from the database. If the product does not exist, use the echo statement to output "Product does not exist" and end the script. Otherwise, we store the product data in the $product array and display it on the page using HTML and PHP code.
3. Summary
This article introduces how to use PHP to jump from the information list page to the detailed information page. In the process of realizing this function, we need to master multiple aspects of knowledge such as database query statements, HTML, CSS and PHP. I hope this article can help you better understand how to use PHP to jump to the details page.
The above is the detailed content of How to jump to the details page in php. For more information, please follow other related articles on the PHP Chinese website!