PHP Mall Function Tutorial: Implementing Product Classification and Filtering Functions
When developing a PHP mall project, product classification and filtering functions are essential. Through reasonable classification and filtering, users can easily find and purchase products. This article will introduce in detail how to use PHP to implement product classification and filtering functions, and attach code examples.
First, we need to create a product classification table and the corresponding model class. The product classification table must contain at least the following fields: id, name, parent_id. Among them, id is the auto-incrementing primary key, name is the category name, and parent_id is the parent category ID of the category.
The following is a code example for creating a product classification table:
CREATE TABLE `categories` ( `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, `name` varchar(255) NOT NULL, `parent_id` int(11) DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Next, create the Category model class and define the corresponding relationships and methods. The following is a code example:
<?php namespace AppModels; use IlluminateDatabaseEloquentModel; class Category extends Model { protected $fillable = ['name', 'parent_id']; public function parent() { return $this->belongsTo(Category::class, 'parent_id'); } public function children() { return $this->hasMany(Category::class, 'parent_id'); } }
On the front page of the mall, we need to display the product categories in a tree structure to facilitate users to browse and select Classification. The following is a code example:
<?php use AppModelsCategory; function displayCategories($categories, $parent_id = 0) { echo '<ul class="category-list">'; foreach ($categories as $category) { if ($category->parent_id == $parent_id) { echo '<li>'; echo $category->name; $children = $category->children; if (count($children) > 0) { displayCategories($children, $category->id); } echo '</li>'; } } echo '</ul>'; } $categories = Category::all(); displayCategories($categories);
The product filtering function of the mall allows users to quickly find the products they need based on their needs. Usually, product screening involves product attributes, such as price, color, etc. We can use forms to collect users' filtering conditions and query for products that meet the criteria based on the conditions. The following is a code example:
<?php use AppModelsProduct; function getFilteredProducts($filters) { $query = Product::query(); if (isset($filters['price'])) { $query->whereBetween('price', [$filters['price']['min'], $filters['price']['max']]); } if (isset($filters['color'])) { $query->whereIn('color', $filters['color']); } // 添加其他筛选条件 $products = $query->get(); return $products; } // 获取用户提交的筛选条件 $filters = $_POST['filters']; // 调用筛选函数获取符合条件的商品 $products = getFilteredProducts($filters);
The above is a tutorial on using PHP to implement product classification and filtering functions. Through reasonable classification and filtering, the user experience can be improved and help users quickly find the products they need. Hope this article helps you!
The above is the detailed content of PHP mall function tutorial: implement product classification and filtering functions. For more information, please follow other related articles on the PHP Chinese website!