Good day!

I won’t figure out how to most effectively implement the conclusion.

There is a table with categories, there is a table with documents (each document has a category id).

It is necessary to display the following way:

Category 1:

  • A document with an id of this category.
  • Document 2, which has an id of this category.
  • ....

Category 2:

  • A document with an id of this category.
  • Document 2, which has an id of this category.
  • ....

How to implement this?

    1 answer 1

    function category($document) { $result = array(); foreach($document as $value) { $result[$value['cat']][] = $value; } return $result; } $_document = $category(массив документов полученный из таблицы документы); 

    As a result, you get this array. $ document = array (M1 => array (0 => document, 1 => document, ..., n => document), M2 => array (0 => document, ..., n => document), ..., Mn => array ())

    With the query, you will receive all the categories that are used in the documents and, through foreach, derive $ cat - your array is derived from the category table

     foreach($cat as $value) { echo $value['name_category']; echo $document[$value['id_category']]['name_document']; }