L
L
Lev Bazdyrev2014-12-16 16:40:48
CodeIgniter
Lev Bazdyrev, 2014-12-16 16:40:48

How to make tree structure from categories database in CodeIgniter?

Good day. The question arose of how to make a tree-like structure of categories. The category data is taken from the database. Tried a bunch of code. But in all cases nothing is output.
Maybe there is some library or helper that will help to make this structure.
I think the code will come in handy!

function get_tree($parent_id = 0, $prefix = "") {
    $CI = &get_instance();
    global $out;
    $query = "SELECT * FROM categories WHERE cat_id = '$parent_id'";
    $result = $CI->db->query($query);
    while ($row = $result->result_array) {
        $out .= $prefix.$row['name']."<br>";
        get_tree($row['cat_id'], $prefix."&nbsp;&nbsp;");
    }
    return $out;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan GiBSON, 2014-12-16
@Bazdyrev_Levan

use for example nested sets - just a tree structure, I think you will find a lot of implementations in Google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question