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

  • Home
  • SEARCH
  • 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 6803435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:20:38+00:00 2026-05-26T19:20:38+00:00

I have a question regarding db queries inside of a foreach loop. Im making

  • 0

I have a question regarding db queries inside of a foreach loop. Im making a guestbook for users on a site. i store “user_id”, “owner_id” “message” in db table “guestbook”. And loop through them to display the posts on the page. But in that loop i get the “username” in the “users” table where id = user_id by calling a whois_model directly.

<?php foreach ($records as $record) : ?>
From: <?=anchor('profile/view/' . $record->user_id . '', $this->whois_model->_whois($record->user_id)); ?>

<p><?=$record->message; ?></p>
<?php endforeach; ?>

Is it better to store the username in the guestbook table directly, or is there a better approach to avoid running multiple queries?

Thanks in advance

George

  • 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-26T19:20:39+00:00Added an answer on May 26, 2026 at 7:20 pm

    Don’t denormalize your data unless you have a good reason to. Not only you are wasting space, but if you also have a username column in your records table, what happens when a user changes their name?

    The better way to do this is to have your model or your controller retrieve the username automatically whenever you plan to use it. This can be done using a join, eg:

    $this->db->select('username, message');
    $this->db->from('guestbook');
    $this->db->join('users', 'guestbook.user_id = users.user_id');
    
    $query = $this->db->get();
    

    Remember that the view should only be used to display information; you shouldn’t have to fetch anything from the database (eg: via another model) in the view.

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

Sidebar

Related Questions

I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have a question regarding handling errors in a J2EE application. Our current application
I have one question regarding domain account. I have one domain controller where all
I have a question regarding dependency injection. say i want to create a class
I have a question regarding static function in php. let's assume that I have
I have a question regarding the onkeypress event on JavaScript. Is it possible to
I have a question regarding the integration of business objects developed using Linq To

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.