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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:20:37+00:00 2026-05-28T00:20:37+00:00

Basically i’m trying to build a comment system. The user looks at a photo

  • 0

Basically i’m trying to build a comment system. The user looks at a photo on the site, and then can view all the comments made by other members.

Each comment will be looped out using a foreach (currently working fine), but what I need to do is then run a seperate query for each comment to get the user details of the user who posted it. These details are stored on a seperate database (otherwise i’d just do a join).

My model has this in it so far:

public function get_comment($id)
{
    $db_photos = $this->load->database('photos', TRUE);
    $db_photos->select('id, comment, userid, photoid');
    $db_photos->from('comments');
    $db_photos->where('photoid', $id);

    return $db_photos->get()->result();
}

And here’s the controller:

public function view($id)
    {   

        $data['comment'] = $this->viewphoto_model->get_comment($id);
        if (empty($data['comment'])) { show_404(); }

        $this->load->view('templates/header', $data);
        $this->load->view('viewphoto/viewphoto', $data);
        $this->load->view('templates/footer', $data);
    }

And then the view:

<?php foreach ($comment as $comments): ?>
        <div class="ViewPhoto-CommentsBox">
        <? echo $comments->comment; ?>
        </div>
        <?php endforeach ?>

So basically I need to grab the ‘userid’ value from each comment and then run a query on the ‘users’ database to get the user details for each comment posted.

Any help is most appreciated 🙂

EDIT:

Still not working, here’s latest version.

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Viewphoto extends CI_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->model('viewphoto_model');
    }


    public function view($id)
    {
        $data['photo'] = $this->viewphoto_model->get_photo($id);
        if (empty($data['photo'])) { show_404(); }

        $data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);
        if (empty($data['user'])) { show_404(); }


        $comment = $this->viewphoto_model->get_comment($id);
        if($comment->num_rows() > 0)
            {
                foreach ($comment->result() as $r)
                {
                    $data['reg'][$i]['comment']=$r->comment;
                    $data['reg'][$i]['id']=$r->id;           

                   // Get user details from user table
                    $user_profile = $this->viewphoto_model->get_comment_user($r->userid);
                    if($user_profile->num_rows() > 0)
                    {
                        foreach ($user_profile->result() as $row)
                        {
                            // user details whatever you have in your db.
                            $data['reg'][$i]['id']=$row->id;
                            $data['reg'][$i]['firstname']=$row->firstname;
                            $data['reg'][$i]['lastname']=$row->lastname;
                        }
                    }

                    $i++;
                }
            }

        $data['title'] = $data['photo']->title.' by '.$data['user']->firstname.' '.$data['user']->lastname;
        $data['meta_description'] = $data['photo']->description;
        $data['directory'] = 'sub';

        $this->load->view('templates/header', $data);
        $this->load->view('viewphoto/viewphoto', $data);
        $this->load->view('templates/footer', $data);
    }
}

Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Viewphoto_model extends CI_Model {


    public function get_photo($id)
    {
        $db_photos = $this->load->database('photos', TRUE);
        $db_photos->select('*');
        $db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
        $db_photos->from('photos');
        $db_photos->where('approved', '1');
        $db_photos->where('id', $id);

        return $db_photos->get()->row();
    }

    public function get_user($userid)
    {
        $db_users = $this->load->database('users', TRUE);
        $db_users->select('id, firstname, lastname, email, type, type_staff, count_approved, count_sales, count_comments, count_editorial, featured, subscriber');
        $db_users->from('useraccounts');
        $db_users->where('id', $userid);

        return $db_users->get()->row();
    }


    public function get_comment($id)
    {
        $db_photos = $this->load->database('photos', TRUE);
        $db_photos->select('id, comment, userid, photoid');
        $db_photos->from('comments');
        $db_photos->where('photoid', $id);

        return $db_photos->get()->result();
    }


    public function get_comment_user($userid)
    {
        $db_users = $this->load->database('users', TRUE);
        $db_users->select('id, firstname, lastname');
        $db_users->from('useraccounts');
        $db_users->where('id', $userid);

        return $db_users->get();
    }

}

View:

<?php foreach ($reg as $comments): ?>
        <div class="ViewPhoto-CommentsBox">
        <? echo $comments['comment']; ?> by <? echo $comments['firstname'];?>
        </div>
        <?php endforeach ?>
  • 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-28T00:20:37+00:00Added an answer on May 28, 2026 at 12:20 am

    change your controller like this.

     public function view($id)
            {       
                  $comment= $this->viewphoto_model->get_comment($id);
    
                  if($comment->num_rows() > 0)
                    {
                        foreach ($comment->result() as $r)
                        {
                            $data['reg'][$i]['comment']=$r->comment;
                            $data['reg'][$i]['id']=$r->id;           
    
                           // Get user details from user table
                            $user_profile=$this->viewphoto_model->get_user_profile_details($r->userid);
                            if($user_profile->num_rows() > 0)
                            {
                                foreach ($user_profile->result() as $row)
                                {
                                    // user details whatever you have in your db.
                                    $data['reg'][$i]['name']=$row->name;
                                    $data['reg'][$i]['gender']=$row->gender;
                                    $data['reg'][$i]['phone_no']=$row->phone_no;
                                }
                            }
    
                            $i++;
                        }
                    }
                $this->load->view('templates/header', $data);
                $this->load->view('viewphoto/viewphoto', $data);
                $this->load->view('templates/footer', $data);
            }
    

    View file: you can use details like below.

    <?php if(isset($reg)) { foreach ($reg as $comments): ?>
            <div class="ViewPhoto-CommentsBox">
            <? echo $comments['comment']; ?>
             <? echo $comments['name']; // user detail ?>
            </div>
            <?php endforeach ?>
            <? } ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I am trying to achieve is check if user is logged in
Basically I'm trying to make a web coding tutorial site (awesome that i cant
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically, I would like a brief explanation of how I can access a SQL
Basically I am trying to restart a service from a php web page. Here
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
Basically, what I'm trying to create is a page of div tags, each has
Basically what I am doing is giving a user a list of terms via
Basically what I'm trying to do is make a very simple vertical bullet projectile

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.