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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:30:55+00:00 2026-06-01T23:30:55+00:00

I have this in Model: public function index_loop() { $post_user = $this->db->query(SELECT posts.post_title, posts.post_content,

  • 0

I have this in Model:

public function index_loop() { 
        $post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id ORDER BY posts.post_ID DESC");
        //$categories = $this->db->query("SELECT categories.cat_name, categories.cat_id FROM  
        //$comments = $this->db->query("SELECT COUNT(comment_id) FROM comments WHERE
        return $post_user->result_array();
    }

What I need is to show categories for each post and comments (although I guess if I figure out categories passing than comments are the same way)

in View file:

 <?php foreach($posts as $zz) { ?>
         <div class="article">
           <h2><?php echo $zz['post_title']; ?></h2>
           <p>Posted by <a href=""><?php echo $zz['username']; ?></a> | Filed under <a href="#">templates</a>, <a href=>internet</a></p>
            <p><?php echo $zz['post_content']; ?></p>
           <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz['post_date']; ?></p>
        </div> <?php } ?>

So, if I want to loop categories on each blog I need that blogs ID, how do I get it if I make all queries from Model?
The same for comments

Is it good that I make one big complicated DB query ( which is hard but I can do it) or I can do 2 or 3 separate smaller queries?

  • 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-01T23:30:57+00:00Added an answer on June 1, 2026 at 11:30 pm

    Codeigniter allows you to return database results as objects (model objects, for example), which makes data much easier to work with. You can issue your initial query to the Posts table, include the posts.id field in your result set, and pass the name of your Post_model class to the $db->query->result() function to tell codeigniter that you’d like your result(s) returned as instances of the Post_model class.

    You can then define methods on the Post_model class to GetCategories by post_id and GetComments by post_id, and then call these methods to populate your $categories and $comments collections for each Post_model returned from your query.

    Here’s an example, i hope it helps:

        public class Post_model extends CI_Model
        {
    
            // All the properties in the Posts table, as well as a couple variables to hold the categories and comments for this Post:
            public $id;
            public $post_title;
            public $post_content;
            public $post_date;
            public $username;        
            public $categories;
            public $comments;
    
            public function index_loop() 
            { 
                    return $this->GetAllPosts();
            }
    
            // function to get all posts from the database, including comments and categories.
            // returns an array of Post_model objects
            public function GetAllPosts()
            {
                    // define an empty array to hold the results of you query.
                    $all_posts = array();
    
                    // define your sql query. NOTE the POSTS.ID field has been added to the field list
                    $sql = "SELECT posts.id, 
                                   posts.post_title, 
                                   posts.post_content, 
                                   posts.post_date,
                                   users.username
                            FROM posts LEFT JOIN users ON posts.user_id = users.user_id
                            ORDER BY posts.post_id DESC";
    
                    // issue the query
                    $query = $this->db->query($sql);
    
                    // loop through the query results, passing a string to result() which represents a class to instantiate 
                    //for each result object (note: this class must be loaded)
    
                    foreach($query->result("Post_model") as $post)
                    {
                            $post->categories = $this->GetPostCategories($post->id);
                            $post->comments = $this->GetPostComments($post->id);
                            $all_posts[] = $post;
                    }
    
                    return $all_posts;
            }
    
            // function to return categories for a given post_id.
            // returns an array of Category_model objects.
            public function GetPostCategories($post_id)
            {
                    $sql = "SELECT category.id, ... WHERE post_id = ?";
                    $query = $this->db->query($sql, array($post_id));
                    $categories = array();
                    foreach($query->result("Category_model") as $category)
                    {
                            $categories[] = $category;
                    }
                    return $categories;
            }
    
            // function to return comments for a given post_id. 
            //returns an array of Comment_model objects
            public function GetPostComments($post_id)
            {
                    $sql = "SELECT comment.id, ... WHERE post_id = ?";
                    $query = $this->db->query($sql, array($post_id));
                    $comments = array();
                    foreach($query->result("Comment_model") as $comment)
                    {
                            $comments[] = $comment;
                    }
                    return $comments;
            }    
    }
    

    Then, in your view, you can access your $posts array as Post_model objects rather than as result_arrays:

    <?php foreach($posts as $zz) { ?>
             <div class="article">
               <h2><?php echo $zz->post_title; ?></h2>
               <p>Posted by <a href=""><?php echo $zz->username; ?></a> | 
    
                  Filed under 
                   <?php foreach($zz->categories as $category) {
                            echo '<a href="#">{$category->name}</a>, ';
                         }
                   ?>
               </p>
               <p><?php echo $zz->post_content; ?></p>
               <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz->post_date; ?></p>
            </div> <?php } ?>
    

    As for the question of efficiency, it’s going to depend on a lot of factors (is the database located on the same machine as the web server? how many posts are there? etc). A single large query will typically perform quicker than several smaller ones, but it will require profiling to really determine whether the performance gains are worth seeking. I always prefer to try and write readable/understandable code rather than optimize at the expense of adding complexity.

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

Sidebar

Related Questions

I'm using FluentNHibernate but NHibernate XML will do. Say I have this model public
I have a class Similar to this public class Model { public TimeSpan Time1
I'm using MVC3 and I have a model like this: public class Foo {
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a model that looks something like this: public abstract class Parent {
I have this function in my model the purpose of it is to get
I have a controller with this action below: public function addAction() { //action for
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have this model var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess

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.