Home > Backend Development > PHP Problem > How to jump to the details page in php

How to jump to the details page in php

PHPz
Release: 2023-04-24 09:21:17
Original
757 people have browsed it

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:

  1. PHP basic syntax
  2. Basic knowledge of database
  3. Basic knowledge of website development
  4. Basic knowledge of front-end

2. Implementation method

Let’s take a look at how to implement this function :

  1. Build the database

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:

  • id: product ID
  • name: product name
  • description:product description
  • price:product price
  • image:product image url
  • created_at:created time
  • updated_at:updated time
  1. Building page

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>
Copy after login

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.

  1. PHP Jump

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>
Copy after login

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[&#39;id&#39;])) {
    $id = $_GET[&#39;id&#39;];
} else {
    header(&#39;Location: index.php&#39;);
    exit;
}
?>
Copy after login

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(&#39;localhost&#39;, &#39;username&#39;, &#39;password&#39;, &#39;database&#39;);

$query = "SELECT * FROM products WHERE id = $id";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) == 0) {
    echo &#39;产品不存在&#39;;
    exit;
} else {
    $product = mysqli_fetch_assoc($result);
}
?>

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $product[&#39;name&#39;]; ?></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[&#39;image&#39;]; ?>" alt="<?php echo $product[&#39;name&#39;]; ?>">
        <h2><?php echo $product[&#39;name&#39;]; ?></h2>
        <p><?php echo $product[&#39;description&#39;]; ?></p>
        <p>价格:<?php echo $product[&#39;price&#39;]; ?></p>
    </div>
</body>
</html>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template