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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:16:49+00:00 2026-05-26T02:16:49+00:00

Ah, the aged old question of parent / child categories in PHP. My apologies

  • 0

Ah, the aged old question of parent / child categories in PHP. My apologies if this question has been asked already (I know it has in many forms), but doing a search couldn’t specifically answer how to do this using one query and Codeigniter.

Basically I am developing a classifieds website using Codeigniter. A listing can be assigned to a category. A category can be stand-alone (no children) or a category can have one child.

Using Codeigniter’s “Active Record” functions, I have the following that almost works.

$this->db->select('l.*');
$this->db->select('c.cat_parent, c.cat_slug, c.cat_name, c.cat_description, c.cat_display');
$this->db->select('c2.cat_slug AS parent_cat_slug, c2.cat_name AS parent_cat_name, c2.cat_description AS parent_cat_description, c2.cat_display AS parent_cat_display');

$this->db->limit($limit, $offset);

$this->db->join('listing_status ls', 'ls.status_id = l.listing_status');
$this->db->join('categories c', 'c.cat_id = l.listing_category');
$this->db->join('categories c2', 'c2.cat_parent = c.cat_id');

return $this->db->get('listings l')->result();

I want to be able to pull out a listing, it’s assigned category and if that category the listing belongs to has a parent, get the parent category details as well. I’ve added in 2 joins and it should be working, is there something I’ve missed?

  • 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-26T02:16:50+00:00Added an answer on May 26, 2026 at 2:16 am

    After a little messing around, I realised that my second join was wrong. Here is my updated and working code in hopes it helps someone else.

    $this->db->select('l.*');
    $this->db->select('c.cat_parent, c.cat_slug, c.cat_name, c.cat_description, c.cat_display');
    $this->db->select('pc.cat_slug AS parent_cat_slug, pc.cat_name AS parent_cat_name, pc.cat_description AS parent_cat_description, pc.cat_display AS parent_cat_display');
    
    $this->db->limit($limit, $offset);
    
    $this->db->join('listing_status ls', 'ls.status_id = l.listing_status');
    $this->db->join('categories c', 'c.cat_id = l.listing_category');
    $this->db->join('categories pc', 'pc.cat_id = c.cat_parent');
    
    return $this->db->get('listings l')->result();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this is a pretty simple question but I'm having trouble finding the
I have some complicated object, such as a Cat, which has many properties, such
Switching aged 2003 SRV to 2008 caused my Asp.net 2 application fail: The application
I am using PHP and I am looking to create links within my text
I'm trying to understand how to solve this trivial problem in C, in the
Good day everyone I need to execute command on linux machine this command is
I want to develop a personal blogging website using ASP.NET MVC 4. I know
Any recommendation on a C# utility (open source) that can can cleanup/delete aged files.
I have written a php class with 2 functions in but when i call
We are currently in the process of migrating from an aged proprietary directory service

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.