E
E
ex3xeng2016-02-07 23:24:58
PHP
ex3xeng, 2016-02-07 23:24:58

How to split a tree into pages (PHP+MySQL)?

I have a table with values:

term_IDterm_parentterm_title
one0Uncategorized
2oneNews
32Sport
43tennis
50Articles

I make a query to the database:
//Выбираем данные из БД
$result=mysql_query("SELECT * FROM  terms");
//Если в базе данных есть записи, формируем массив
if   (mysql_num_rows($result) > 0){
    $cats = array();
//В цикле формируем массив разделов, ключом будет id родительской категории, а также массив разделов, ключом будет id категории
    while($cat =  mysql_fetch_assoc($result)){
        $cats_ID[$cat['term_ID']][] = $cat;
        $cats[$cat['term_parent']][$cat['term_ID']] =  $cat;
    }
}

Result array:
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [term_ID] => 1
                    [term_parent] => 0
                    [term_title] => Без рубрики
                )

            [1] => Array
                (
                    [term_ID] => 5
                    [term_parent] => 0
                    [term_title] => Статьи
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [term_ID] => 2
                    [term_parent] => 1
                    [term_title] => Новости
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [term_ID] => 3
                    [term_parent] => 2
                    [term_title] => Спорт
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [term_ID] => 4
                    [term_parent] => 3
                    [term_title] => Тенис
                )

        )

)

With the help of the function I carry out an elementary output:
echo build_tree($cats,0);
function build_tree($cats,$parent_id,$only_parent = false){
    if(is_array($cats) and isset($cats[$term_parent])){
        $tree = '<ul>';
        if($only_parent==false){
            foreach($cats[$parent_id] as $cat){
                $tree .= '<li>'.$cat['term_title'].' #'.$cat['term_ID'];
                $tree .=  build_tree($cats,$cat['term_ID']);
                $tree .= '</li>';
            }
        }elseif(is_numeric($only_parent)){
            $cat = $cats[$parent_id][$only_parent];
            $tree .= '<li>'.$cat['term_title'].' #'.$cat['term_ID'];
            $tree .=  build_tree($cats,$cat['term_ID']);
            $tree .= '</li>';
        }
        $tree .= '</ul>';
    }
    else return null;
    return $tree;
}

We get something like:
Uncategorized
-News
--Sports
--- Tennis
Articles
And now there was a need for pagination, as there was about a hundred information and you need to break it into pages, but! Yes Important but, if the selection is limited to limit 0.2 - 2.4, etc. The result will be:
Page 1:
Uncategorized
-News
Page 2:
--Sports
---
Tennis then a descendant, that is, you need it like this:
Page 2:
Uncategorized
-News
--Sports
---Tenis
Page 3:
Articles
This is implemented in wordpress in the admin panel when displaying categories, there is pagination.
Tell me how to implement the plan?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2016-02-08
@IvanTheCrazy

Use nested set. You will be selecting the entire tree with one simple select that you can apply limit to.
www.getinfo.ru/article610.html

A
Alexander Aksentiev, 2016-02-07
@Sanasol

Assemble the tree completely, then cut N elements from the beginning, taking into account sub elements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question