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 8701809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:29:30+00:00 2026-06-13T02:29:30+00:00

i have this problem in codeigniter: I try to make a navigation tree system

  • 0

i have this problem in codeigniter:
I try to make a navigation tree system from database.

model:

function getServices()
{
 $this->db->select('service_url, service_title, category_title');    
 $this->db->join('services_category', 'services_category.id=services.category_id');    
 $this->db->group_by('category_title');
 $this->db->order_by('service_title', 'ASC');    
 $query = $this->db->get('services');

 if($query->result() == TRUE)    
 {    
    foreach($query->result_array() as $row)
    {
       $result[] = $row;
    }
    return $result;
  }
}

view:

<?php if(isset($services) && $services) : foreach($services as $s) : ?>    
   <ul>
     <li><a href="#"><?php echo $s['category_title'] ?></a>    
       <ul>
        <li><?php echo anchor('services/' . $s['service_url'], $s['service_title']); ?></li>
       </ul>    
     </li>    
   </ul>    
<?php endforeach; ?>    
<?php endif; ?>

now so far so good, the result is returning each category the way is supposed to, but the service is returning only one service per category, and in some categories there is like 15 services.
Anybody kind to give me a hand, or an explanation what is going wrong ?
Thank you so much.

“i’m not an expert in php or codeigniter, i just started not long time ago, so please don’t shoot the beginner.”

note: i tried without the group_by and order_by, and is returning all services, but the categories are repeating,

ex:

category-a
   service1
category-a
   service2
category-b
   service10
category-b
   service11
category-c
   service30
category-c
  service31
....
  • 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-06-13T02:29:32+00:00Added an answer on June 13, 2026 at 2:29 am

    This is a good read for aggregate functions when using Group By (MySQL).
    A simple explanation would be like this

    Date        | Amount
    2012-01-01  | 5
    2012-01-01  | 6
    2012-01-02  | 1
    2012-01-02  | 6
    

    Now using SUM aggregate function like this.

    SELECT Date, SUM(Amount) as Total FROM table GROUP BY Date ORDER BY DATE;
    

    The result will be:

    Date        | Amount
    2012-01-01  | 11
    2012-01-02  | 7
    

    In your scenario, GROUP BY will not work as intended as it will only get one Category since you grouped your query by each category.

    The ideal solution to this is to have 2 separate functions, GetCategories and GetServices.

    function getServices($category_id = false)
    {
      $this->db->select('service_url, service_title, category_title');
      $this->db->join('services_category', 'services_category.id=services.category_id');
      if ($category_id !== false)
        $this->db->where('services.category_id', $category_id);
      $this->db->order_by('service_title', 'ASC');
      $query = $this->db->get('services');
    
      if($query->result() == TRUE)
      {
        foreach($query->result_array() as $row)
        {
          $result[] = $row;
        }
        return $result;
      }
    }
    
    function getCategories()
    {
      $this->db->select('category_id, category_title');
      $this->db->order_by('category_title', 'ASC');
      $query = $this->db->get('services_category');
    
      if($query->result() == TRUE)
      {
        foreach($query->result_array() as $row)
        {
          $result[] = $row;
        }
        return $result;
      }
    }
    

    Then in your view file, you just need to do a for loop on the Categories then do another query for each Category. Here’s an outline of what it will look like,

    <?php if(isset($categories) && $categories) : foreach($categories as $category) : ?>
      <ul>
        <li><a href="#"><?php echo $category['category_title'] ?></a>
          <ul>
            // how you get $services is similar to how you get $categories
            <?php $services = $this->modelname->getServices($category['category_id']);
            <?php foreach($services as $s) : ?>
              <li><?php echo anchor('categories/' . $s['service_url'], $s['service_title']); ?></li>
            <?php endforeach; ?>
          </ul>
        </li>
      </ul>
    <?php endforeach; ?>
    <?php endif; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a weird problem with CodeIgniter's URI segment system. When I have this
I have this problem: I want to generate a new source code file from
I have got a problem with my codeigniter library. To transform database data into
Framework : CI Situation: I have this very frustrating problem in codeigniter. here is
I have this problem: $id is id for each user $img = 'http://www.somesite.com/pictures/'; $no_img
I have this problem since I'm beginning in OOP programming I want to close
I have this problem when I run SAS 9.2 on the command line on
I have this problem, when I load my form, the validation messages appears in
I have this problem in my SlashDot Menu. I want to change the picture
I have this problem that I can ref a button for control it. I

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.