Output data from the database in the form of blocks

Good evening. Question:

I just can’t figure out how to write a class to display data from the database in the form of blocks. There is data in the database, table PAGES. It has pages with different fields. You need to write such functions, so that later you can extract data from the associative array by the names of the table fields. And the principle of constructing blocks is as follows: There are pages where PARENT_ID = 0are parent pages that include subpages. In a block menu, they are headings before three blocks, in which child menu items are already displayed.

So far, I’ve only been able to display all the vpages, but I can’t group them into the correct block menu. I will be glad to any advice and wishes.

In queries to the database, the main and child menu items were filtered out.
For child items, PARENT_ID is equal to the IDof the parent element.


Answer 1, authority 100%

make a selection from the database of pages and their parents sorted by PARENT_ID, PAGE_ID(ORDER BY PARENT_ID, PAGE_ID), we get an array at the beginning of which there will be pages of the first level and by growing deeper levels. We form an array from it (add additional fields to taste) – an example of the resulting array:

$pages = array (
    array('PAGE_ID' => 1, 'PARENT_ID' => 0),
    array('PAGE_ID' => 2, 'PARENT_ID' => 0),
    array('PAGE_ID' => 3, 'PARENT_ID' => 1),
    array('PAGE_ID' => 4, 'PARENT_ID' => 2),
    array('PAGE_ID' => 5, 'PARENT_ID' => 4),
    array('PAGE_ID' => 6, 'PARENT_ID' => 5),
    array('PAGE_ID' => 7, 'PARENT_ID' => 5),
    array('PAGE_ID' => 8, 'PARENT_ID' => 5)
);

Let’s start crawling from the pages of a deeper hierarchy, to do this, flip the array

$pages = array_reverse($pages);

and go through it pulling out sub pages and adding them in the right order in the current array:

foreach  ($pages as $k => &$p) {
  foreach ($pages as &$page){
    if($page['PAGE_ID'] == $p['PARENT_ID']){
      $page['sub'][] = $p;
      unset($pages[$k]);
      continue;
    }
  }
}

output what we got:

print_r($pages);

we see the following construction (a bit long, but what can you do for clarity 🙂 )

Array
(
    [6] => Array
        (
            [PAGE_ID] => 2
            [PARENT_ID] => 0
            [sub] => Array
                (
                    [0] => Array
                        (
                            [PAGE_ID] => 4
                            [PARENT_ID] => 2
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [PAGE_ID] => 5
                                            [PARENT_ID] => 4
                                            [sub] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [PAGE_ID] => 8
                                                            [PARENT_ID] => 5
                                                        )
                                                    [1] => Array
                                                        (
                                                            [PAGE_ID] => 7
                                                            [PARENT_ID] => 5
                                                        )
                                                    [2] => Array
                                                        (
                                                            [PAGE_ID] => 6
                                                            [PARENT_ID] => 5
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
    [7] => Array
        (
            [PAGE_ID] => 1
            [PARENT_ID] => 0
            [sub] => Array
                (
                    [0] => Array
                        (
                            [PAGE_ID] => 3
                            [PARENT_ID] => 1
                        )
                )
        )

i.e. we have an array with hierarchies, on the basis of which it is quite possible to implement navigation, sitemap generation, etc.

PS I hope this is what was required