P
P
Pro Service2018-12-15 14:25:35
MySQL
Pro Service, 2018-12-15 14:25:35

How to store categories with subcategories in the database?

List of categories
$list_category = [
        "автоматика",
        "архитектура",
        "бетонные работы",
        "бурение скважин",
        "двери"=>[
            "деревянные"=>[
                "изготовление",
                "установка",
                "реставрация",
            ],
            "металлические"=>[
                "изготовление",
                "установка",
                "реставрация",
            ],
            "пластиковые"=>[
                "изготовление",
                "установка",
                "реставрация",
            ],
        ],
        "земляные работы",
        "камин",
        "кладка"=>[
            "каменная",
            "кирпичная",
        ],
    ];

How to save it in the database (MySQL) to display it like this:
spoiler
5c14df66e5f12563669614.png
spoiler
5c14e46c955cc876350894.png

Category list page
spoiler
5c14dfe254613499227494.png

The first level categories will need to be displayed on this page (ex. Doors, Masonry, etc.)
And the subcategories will be displayed on another page when you click on the first level category (ex. doors => Manufacture, Installation, Restoration)
Another task on the Personal page cabinet :
There is a list of categories:
spoiler
5c14e1c584848631923349.png

The user, in order to submit an ad, selects the categories he needs and the ad will appear in the categories he has chosen.
Now the list of categories in the database is stored in the following form:
spoiler
5c14e38e61501253793016.png

There are no subcategories here.
I display them on the list page like this:
spoiler
<?php
    
        
        require_once "../config/function.php";
        $sql = 'SELECT * FROM categories';
        
            $lastChar = null;
            
            foreach ($conndb->query($sql) as $row):
            
                if ($lastChar !== ($currentChar = mb_convert_case(mb_substr($row['category_name'], 0, 1), CASE_UPPER))): 
        
                    $lastChar = $currentChar;
                    
                    
    ?>
                    
                    <h1 class="bukva" id="bukva_<? echo replaceSymbolRuToLat($lastChar); ?>"><? echo mb_convert_case($lastChar, CASE_LOWER); ?></h1>
        
    <?php 
                
                endif;
            
    ?>
                
            <a href="http://localhost/menu_general/masters_page/masters/masters.php">
        
                <div class="kategoriya">
                    <div class="kolichestvo">0</div>
                    <?php echo $row['category_name'] . "\t"; ?>
                </div>
            
            </a>

    <?php

            endforeach;
    
    ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ildus Siraev, 2018-12-20
@pro100nic

Educational program on storing tree-like (hierarchical) data structures in the database
https://gist.github.com/codedokode/10539720

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question