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
Release: 2024-03-06 16:56:02
Original
476 people have browsed it

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

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

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

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!

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