Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7057267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:57:56+00:00 2026-05-28T03:57:56+00:00

So basically what I am trying to do is retrieving a list of categories

  • 0

So basically what I am trying to do is retrieving a list of categories from the database and placing it in a variable called $data[‘categories’].

In the view I am listing this up by simply using a foreach() statement. So far so good.
Though, some categories have subcategories (not all of them). Now, I added a row in the database saying ‘is_direct’ (has subcategories or not) which displays whether the category has subcategories or not.

Now, I want to list up those subcategories in case they exist.

Here the method to display the header (where the categories and subcategories will be listed) (Don’t mind the private, it should be that).

private function show_header($template='default',$page_name='default')
{
    // Get the categories.
    $data['categories']     = $this->CI->ws_category->get_categories();

    // Display the header.
    $this->CI->load->view("templates/".$template."/template/header",$data);
}

This method calls for the method get_categories(), that’s the one below (simple Active Record).

public function get_categories()
{
    // Prepare the SQL query.
    $this->db->select('*');
    $this->db->from(APP_PREFIX.'categories');
    $this->db->order_by('name_clean');

    // Execute the query.
    $query = $this->db->get();

    // Get the results from the query.
    $result = $query->result_array();

    // Return the array with data.
    return $result;
}

And below the view, you can see where the submenu would go.

<?php

    // Loop through all the categories.
    foreach ($categories as $item):

?>
<li class='nav_link <?php ($item['is_direct'] == '1' ? 'nav_subcats' : ''); ?>'>
    <a href='<?php echo base_url().'category/'.$item['category_id'].'-'.$item['name_clean'].'.html';?>' class='nav_main_link'><?php echo $item['name']; ?></a>
    <?php

        // In case subcategories are present.
        if ($item['is_direct'] != '1')
        {
    ?>
    <div class='nav_submenu'>
        <ul>
            <li><a href='#' class='nav_sub_link'>submenu item</a></li>
        </ul>
    </div>
    <?php
        }
    ?>
</li>
<?php
    // End the loop.
    endforeach;

?>

Can somebody help me?
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:57:57+00:00Added an answer on May 28, 2026 at 3:57 am

    The approach I would take would be to build out more complete data in your model (or controller) so that you can simply loop through it in the view (like you are currently doing). Below I have written a possible solution. I have modified your get_categories() method to query for subcategories as well. Then, in the view, we check to see if a category has any subcategories and outputs them as necessary.

    public function get_categories_and_subcategories()
    {
        // Prepare the SQL query.
        $this->db->select('*');
        $this->db->from(APP_PREFIX.'categories');
        $this->db->order_by('name_clean');
    
        // Execute the query.
        $query = $this->db->get();
    
        // Get the results from the query.
        $result = $query->result_array();
    
        // Get subcategories
        for ($i = 0; $i < count($result); $i++) {
    
            // Check if subcategory
            if ($result[$i]['is_direct']) {
    
                // Query for subcategories - made up for demonstrational purposes
                $query = $this->db->query("SELECT * FROM subcategories WHERE parent_id='".$result[$i]['id']."'");
    
                // Add subcategories to category item
                $result[$i]['subcategories'] = $query->result_array();
    
            } else {
    
                $result[$i]['subcategories'] = array();
    
            }
    
        }
    
        // Return the array with data.
        return $result;
    }
    

    Then in the view:

    <li class='nav_link <?php ($item['is_direct'] == '1' ? 'nav_subcats' : ''); ?>'>
        <a href='<?php echo base_url().'category/'.$item['category_id'].' '.$item['name_clean'].'.html';?>' class='nav_main_link'><?php echo $item['name']; ?></a>
    
        <?php if (count($item['subcategories']) > 0): ?>
        <div class='nav_submenu'>
            <ul>
                <foreach ($item['subcategories'] as $subcategory): ?>
                <li><a href='<?php echo $subcategory['url']; ?>' class='nav_sub_link'><?php echo $subcategory['name']; ?></a></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
    </li>
    

    Keep in mind I haven’t tested this code but you should be able to adopt the general idea to your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am retrieving a fetch from my core data database and trying to iterate
I'm basically trying to get all touch event data from something like a system
I'm basically trying to create a linked list from a text file and add
I'm basically trying to build an html ul/li nested list from a multidimensional array
I'm using C# and ASP.NET 3.5. Basically I'm retrieving a column of data from
I'm stuck with retrieving data from a MySQL-database in an efficient way. I have
I'm trying to basically trying to separate a specific amount of text from the
I'm basically trying to pass a method to another class to be called later,
I'm basically trying to get the target element from event.target and if not equal
I am basically trying to create a new database and enter the database values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.