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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:17:33+00:00 2026-06-02T01:17:33+00:00

I have 2 many to many relationship tables; Posts and Categories. One post can

  • 0

I have 2 many to many relationship tables; Posts and Categories. One post can have many categories. My question is how can I show list of posts with their categories?

Like that:

My post 1 (cat1, cat2, cat3)
My post 2 (cat2, cat3)
My post 3 (cat1)

I’ve tried these methods;

// Create post object
$p = new Post();

// Get 30 posts
$p->get(30);

// Loop through all posts
foreach ($p as $post)
{
    // Get the current user's group
    $post->category->get();

    foreach($post->category as $category) {
       // ...
    }
}

Don’t like this, because if I’m get 30 posts, then on every post loop again make a query and find category again and again.

and tried this:

$p = new Post();

$p->include_related('category', array('id', 'name'), TRUE, TRUE)->get(30);

foreach($p as $post) {
  // ...  
  foreach($post->category as $category) {
     // ...
  }
}

This is more close, but this one problem is I setting limit get(30) so if my per post have 2 categories than showing 15 post + 15 categories.

What is the true method for many to many listing?

  • 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-02T01:17:34+00:00Added an answer on June 2, 2026 at 1:17 am

    Well in this case i will opt for caching both tables in php associative arrays, and then loop just the arrays.

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

Sidebar

Related Questions

I have 2 NHibernate entities (tables) that are in a one to many relationship.
I have the following tables in my database that have a many-to-many relationship, which
I created 3 tables: User User-Group Group Where I can have a many-to-many relationship.
I have a one to many relationship between two tables. The many table contains
i have a many to many table relationship that involves 2 logical tables. Record
Lets say I have 3 tables Posts, PostTags and Tags defining a many-to-many relationship.
I am storing tags to posts as a many-to-many relationship, like in this post
I have two tables, Table_A and Table_B. Table_A has a one-to-many relationship with Table_B.
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I've just gone blank. I have a many to many relationship with three tables.

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.