V
V
Vadim Guk2014-07-19 22:15:10
CodeIgniter
Vadim Guk, 2014-07-19 22:15:10

How to make categories with subcategories in Codeigniter?

For the sake of study, I study the Codeigniter framework. And there was a problem. I can't figure out how to display categories with subcategories.
Perhaps someone has already done this, and wrote an article on this topic, or some kind of video lesson?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Guk, 2014-07-20
@Stiflerus

All done:
Table "blog_categories" structure:

blog_categories_id
blog_categories_name
blog_parent_id
blog_categories_discription

Model: pages_model
function get_cat() {
        $query = $this->db->get('<i>blog_categories</i>'); // моя таблица
    
        foreach($query->result_array() as $row ){
            if(!$row['<i>blog_parent_id</i>']){ // если нет поля blog_parent_id, то есть подкатегории
                $data[$row['<i>blog_categories_id</i>']][] = $row['<i>blog_categories_name</i>']; // выводим только назву категории
            }else {
                $data[$row['<i>blog_parent_id</i>']]['sub'][$row['<i>blog_categories_id</i>']] = $row['blog_categories_name']; // если есть подкатегории виводим их как асоциативный масив, насколько я понял
            }
        }
        return $data;  
    }

I use the library to display the site and passed the $data array there:
But you can also pass it in the controller:
Passed it to the controller:
And in the form of blog_right (in my case) enter:
<?php foreach($blog_categories as $key => $item): ?>
  <?php if(count($item) > 1): // якщо це під категорія ?>
    <li><a href="javascript:;"><?=$item[0]?></a>
      <ul class="acc-menu no-circled">
        <li><a href="<i>Нужно поставить ссылки</i>/<?=$key?>">Всі статті</a></li>
          <?php foreach($item['sub'] as $key => $sub): ?>
          <li><a  href="<i>Нужно поставить ссылки</i>/<?=$key?>"><?=$sub?></a></li>
          <?php endforeach; ?>
        </ul></li>
      <?php elseif($item[0]): // якщо це самостійна категорія ?>
    <li><a href="<i>Нужно поставить ссылки</i>/<?=$key?>"><?=$item[0]?></a></li>
  <?php endif; ?>
<?php endforeach; ?>

You can check how the array should look like:
<?php print_r($blog_categories);?>
   Array
(
    [1] => Array
        (
            [0] => Програмування
            [sub] => Array
                (
                    [3] => Веб-розробка
                    [4] => вап в пва
                )
        )
    [2] => Array
        (
            [0] => Веб-розробка
        )
)

I honestly admit I took the decision from the course of the WebForMySelf team. To avoid advertising, I will only say that the course is about the store.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question