PHP mall function tutorial: implement product classification and filtering functions

王林
Release: 2023-07-31 11:50:02
Original
1348 people have browsed it

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.

  1. Create product classification

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

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');
    }
}
Copy after login
  1. Display product categories

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);
Copy after login
  1. Product filtering

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

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!

Related labels:
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