A PHP shopping mall is a common type of e-commerce website where users can browse, search, and purchase products. Product search and sorting features are crucial for user experience, helping users quickly find the products they need. In this article, we will discuss how to implement product search and sorting features in a PHP shopping mall.
First, we need to prepare a database to store product information, including fields like product name, description, and price. We will assume the use of MySQL, where we create a table named 'products'.
CREATE TABLE products (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL
);
In this table, product name, description, and price are stored as basic information, with an auto-incrementing id as the primary key.
Next, we need to add a search form on the shopping mall webpage for users to input search keywords:
<form method="GET" action="search.php">
<input type="text" name="keyword" placeholder="Enter product name">
<input type="submit" value="Search">
</form>
The form uses the GET method to submit the user's input to the search.php page.
In the search.php page, we use the user's input to query the database for matching products:
<?php
$keyword = $_GET['keyword'];
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Query for matching products
$sql = "SELECT * FROM products WHERE name LIKE '%$keyword%'";
$result = $mysqli->query($sql);
// Output search results
while ($row = $result->fetch_assoc()) {
echo "<div class='product'><h4>" . $row['name'] . "</h4><p>" . $row['description'] . "</p><p>Price: " . $row['price'] . "</p></div>";
}
// Close the database connection
$mysqli->close();
?>
The above code uses the LIKE operator to query products that match the input keyword and then outputs the results.
To implement product sorting, we need to provide a sorting option on the webpage for users to select the sorting criteria:
<form method="GET" action="sort.php">
<select name="sort">
<option value="name">Sort by name</option>
<option value="price">Sort by price</option>
</select>
<input type="submit" value="Sort">
</form>
Once the user selects a sorting option, the form will submit the data to the sort.php page.
In the sort.php page, we accept the user's sorting selection and query the database to sort the products:
<?php
$sort = $_GET['sort'];
// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Query and sort products
$sql = "SELECT * FROM products ORDER BY $sort";
$result = $mysqli->query($sql);
// Output sorted results
while ($row = $result->fetch_assoc()) {
echo "<div class='product'><h4>" . $row['name'] . "</h4><p>" . $row['description'] . "</p><p>Price: " . $row['price'] . "</p></div>";
}
// Close the database connection
$mysqli->close();
?>
In this code, we use the ORDER BY clause to query and sort products based on the user's selected criteria and output the results.
This article discussed how to implement product search and sorting features in a PHP shopping mall. The search functionality allows users to find products based on keywords, and the sorting functionality enables users to sort products by name or price. These features significantly enhance the user experience and help users find the products they need more efficiently.
Depending on specific needs, these features can be further optimized, such as adding more search criteria or supporting additional sorting methods.