Home >Backend Development >PHP Tutorial >Several ways to efficiently implement loop query subcategories in PHP

Several ways to efficiently implement loop query subcategories in PHP

PHPz
PHPzOriginal
2024-03-06 16:54:04488browse

Several ways to efficiently implement loop query subcategories in PHP

PHP is a commonly used server-side scripting language that is widely used in web development. In PHP development, we often encounter situations where we need to query and display categories and their subcategories. This article will explore several ways to efficiently implement loop query subcategories and give specific code examples.

The first way: recursive query

Recursion is a common method of traversing a tree structure and is suitable for querying nested classifications. The following is an example code for a simple recursive query subcategory:

function getSubcategories($category_id) {
    $subcategories = [];
    $query = "SELECT id, name FROM categories WHERE parent_id = $category_id";
    $result = mysqli_query($connection, $query);
    
    while ($row = mysqli_fetch_assoc($result)) {
        $subcategories[] = $row;
        $subcategories = array_merge($subcategories, getSubcategories($row['id']));
    }
    
    return $subcategories;
}

$category_id = 1;
$subcategories = getSubcategories($category_id);
print_r($subcategories);

Second way: Use closure function

The closure function is a powerful feature of PHP that can be used inside the function Define an anonymous function and call it outside the function. The following is a sample code that uses a closure function to query subcategories:

$categories = [];

$fetchCategories = function ($parent_id) use (&$fetchCategories, &$categories) {
    $query = "SELECT id, name FROM categories WHERE parent_id = $parent_id";
    $result = mysqli_query($connection, $query);

    while ($row = mysqli_fetch_assoc($result)) {
        $categories[] = $row;
        $fetchCategories($row['id']);
    }
};

$fetchCategories(1);
print_r($categories);

The third way: use recursive query and store it in an array

Get all categories in one database query, Then use PHP code to recursively build the sub-category tree. The following is a sample code:

$query = "SELECT id, name, parent_id FROM categories";
$result = mysqli_query($connection, $query);

$categories = [];
while ($row = mysqli_fetch_assoc($result)) {
    $categories[$row['id']] = $row;
}

$nestedCategories = [];
foreach ($categories as $id => $category) {
    if ($category['parent_id'] == 0) {
        $nestedCategories[$id] = $category;
    } else {
        $categories[$category['parent_id']]['children'][$id] = $category;
    }
}

print_r($nestedCategories);

Through the above three methods, we can efficiently implement loop query subcategories. Choosing a method that suits the project needs and data structure can improve code efficiency and reduce the number of database queries, thereby optimizing program performance. Hope the above content is helpful to you.

The above is the detailed content of Several ways to efficiently implement loop query subcategories in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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