Hello, dear stack users. The error consists in pulling categories from the database. The database is hosted, and it looks like this: enter image description here

I am writing a structure using the Codeigniter framework. The script in the model is this:

public function get_cat() { $db_mysql = $this->load->database('zhaimakz_products', TRUE); $query = $db_mysql->get('categories'); // моя таблица foreach($query->result_array() as $row ) { if (!$row['parent']) { // если нет поля blog_parent_id, то есть подкатегории $data[$row['id']][] = $row['title']; // выводим только назву категории } else { $data[$row['id']][] = $row['title']; $data[$row['parent']]['sub'][$row['id']] = $row['title']; } } return $data; } 

Category output:

 <?php foreach($blog_categories as $key => $item): ?> <?php if(count($item) > 1): // якщо це під категорія ?> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordian" href="#womens"><span class="badge pull-right"><i class="fa fa-plus"></i></span> <a href="javascript:;"><?='1'.$item[0]?></a> </a> </h4> </div> <div id="womens" class="panel-collapse collapse"> <div class="panel-body"> <li><a href="<i>Нужно поставить ссылки</i>/<?=$key?>"></a> </li><?php foreach($item['sub'] as $key => $sub): ?> <li><a href="<i>Нужно поставить ссылки</i>/<?=$key?>"><?='3'.$sub?></a></li> <?php endforeach; ?> </div> </div> <?php elseif($item[0]):?> <li><a href="<i>Нужно поставить ссылки</i>/<?=$key?>"><?='2'.$item[0]?></a></li> <?php endif; ?> <?php endforeach; ?> 

Displays categories incorrectly:

enter image description here

Please tell me what could be the matter?

    1 answer 1

    Try to form a sample like this:

     foreach($query->result_array() as $row ) { if (!$row['parent']) { // категория 1 уровня $data[$row['id']]['title'] = $row['title']; } else { // подкатегория $data[$row['parent']]['sub'][$row['id']] = $row['title']; } } 

    And in the answer to display the title , and then brute by sub

    • My idea is that there are 3 category levels. I tried now, it refers to an empty object. Undefined offset: 0 error code. - Chingiz Batirbaev