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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:34:28+00:00 2026-06-16T14:34:28+00:00

I am new to both PHP and the Codeigniter(v2.0) framework. Hoping you can help

  • 0

I am new to both PHP and the Codeigniter(v2.0) framework. Hoping you can help me find my way through my first webapp. Details below, and thank you in advance!

Goal/Issue Summary: I would like to display within my view all blog_Comments that are attached to each paticular blog_Post.

Details: My blog_Posts table contains all original blog posts [id(int), title, entry, author (all varchar), and date(timestamp)]. My blog_Comments table[id(int), entry_id(int), author, comment(varchar)] contains all comments; they are associated with the original blog_Post via the *entry_id* attribute.

Controller:

$query1 = $this->blog_model->get_posts();
$query2 = $this->blog_model->get_comments();
$data = array();
$data['posts'] = $query1;
$data['comments'] = $query2;

Model:

function get_posts() {
$query = this->db->get('blog_Posts');
return $query->result;
}

function get_comments() {
$query = this->db->get('blog_Comments');
return $query->result;}

View:

<?php if(isset($posts)) : foreach($posts as $posts=>$row) : ?>
     <?php $row->title; ?>
     <?php $row->author; ?>
     <?php $row->entry; ?>

<?php foreach($comments as $comments=>$com) : ?>
    <?php if($com->entry_id == $row->id) : ?>
        <p>author: <?php echo $com->author; ?></p>
        <p>comment: <?php echo $com->comment; ?></p>
    <?php endif; ?>
<?php endforeach; ?>

<?php endforeach; ?>
<?php else : ?> <p>no blog_Posts found</p>
<?php endif; ?>

Edit/Update:

My attempt to display the comments is pasted into the view code block above, I’ve also listed the two issues it is giving me. Right now I have 2 sample original blog posts, and comments associated with each.

Issues:

  1. the first blog entry (first time through the loop) it is displaying ALL comments, including those associated with other entry_id s.
  2. The second blog entry (second time through loop), it throws the following error: “Invalid argument supplied for foreach()”, and points to the line in my view containing the foreach for $comments.

In short, I’m trying to loo through only those comments with the entry_id that matches the original posts “id.”

Thanks again for your assistance and time.

-AJ

  • 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-16T14:34:29+00:00Added an answer on June 16, 2026 at 2:34 pm

    Well since you do not need to display all comments at once, you could just alter your get_comments function like below to return comments for each post and of course display them in your way, order by date created, last entry etc:

    So for the model part:

    function get_comments($post_id){
    $query = $this->db->query("select * from `blog_Comments` where `entry_id` = $post_id ORDER BY `created_on` DESC");
    
      if($query->num_rows() != 0){
       return $query->result();
     }else{
       return false;
     }
    }
    

    And in your view file:

    foreach($posts as $row){
     echo 'Title: ' . $row->title .
     'Author: ' . $row->author .
     'Entry:' . $row->entry;
     $comments = $this->blog_model->get_comments($row->id);
     if(!$comments){ 
      echo 'No comments'; 
     }else{
      foreach($comments as $com){
        echo $com->comments . '<br/>' . $com->user . '<br/>' . etc etc
      }
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to both Django and Python. This is my first time using
Good morning! I'm having great success so far with CodeIgniter. I'm new to PHP
I'm new to both XML and PHP but I have this project and this
I am pretty new to both php and SQL. I have a login page
I am pretty new to both php and mysql. I have a txt file
first to say I'm really new to both Symfony and Doctrine and am trying
First of all, I'm quite new to both OOP and NodeJS. My only experience
I am in the process of migrating to a new PHP Framework. I have
I'm new to both HTML & PHP. I'm attempting to remove multiple DOM elements
Possible Duplicate: Where is Kohana Version I'm new to both PHP and kohana. 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.