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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:26:39+00:00 2026-05-16T22:26:39+00:00

To start off, this really isn’t CodeIgniter specific. I’m having trouble grasping an idea,

  • 0

To start off, this really isn’t CodeIgniter specific. I’m having trouble grasping an idea, so anyone that knows PHP/SQL (or whatever my problem is) can jump in.

I have 2 tables, ‘Photo’ and ‘Album’.

Album columns : ID  TITLE  USER_ID  CREATED  MODIFIED
Photo columns : ID  ALBUM_ID  TITLE  LOC  CREATED  MODIFIED

I’m using the query

 $this->db->select('*');
    $this->db->from('album');
    $this->db->join('photo', 'album.id = photo.album_id');
    $this->db->limit(10);
    return $this->db->get();

which I think translates into

SELECT * FROM album JOIN photo ON album.id = photo.album_id LIMIT 10;

Anyways, what I don’t understand is how to display each album with the related photos.

So something like…

<div class="album">
<img src="image1" />
<img src="image2" />
</div>
<div class="album">
<img src="image3" />
</div>

Basically how do I “echo” out the correct albums and their relative images? As far as I can tell, a foreach loop is just going to write out each image without discriminating between which album the image belongs to.

Should I even be using a JOIN query?

UPDATE:

Wow, ok tilman, you are pro. Thanks a lot for the help. For anyone else referencing this, make sure you change ‘id’ in $array[$row[‘id’]][] = $row; to whatever your album id is. In my case, its album_id. My final join statement was overwriting id with album_id.

  • 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-16T22:26:40+00:00Added an answer on May 16, 2026 at 10:26 pm

    Yes, JOINing the two tables is the way to go.

      $query = $this->db->query('
          SELECT *
          FROM album a
          JOIN photo p ON a.id = p.album_id
          LIMIT 10
          ');
    
        foreach ($query->result_array() as $row) {
          $array[$row['id']][] = $row;
        }
    
        foreach ($array as $album) {
          echo '<div class="album">';
          foreach ($album as $photo) {
            echo '<img src="' . $photo['src'] . '"/>';
          }
          echo '</div>';
        }
    

    I hacked up this piece of code, I hope this works for you.
    We pull all the data from the db, put them in a multi-nested array, grouped by albums.

    Then iterate over that array and display the final html.

    Probably not the prettiest solution, but should get the job done 😉

    I am using Doctrine with Codeigniter, which provides this functionality out-of-the-box, it’s called the array hydrator.
    Might want to have a look at it.

    Let me know if you need help setting it up.

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

Sidebar

Related Questions

Let me start off with a bit of background. This morning one of our
First off, let me start off that I am not a .net developer. The
I'm trying to create a bookmarklet that will start off an AJAX call to
/* I start with this: */ <Report> <prop1>4</prop1> <prop2>2255</prop2> <prop3>true</prop3> <prop4>false</prop4> <prop5>true</prop5> </Report> /*
Start a new Silverlight application... and in the code behind (in the Loaded event),
I start by creating a string variable with some non-ascii utf-8 encoded data on
I start to write a client - server application using .net (C#) for both
To start, I know there are two kinds of JSON serialization currently built into
I start a .Net server side program on my local workstation, but soon it
I start build a system with distributed architecture by all applications will communication by

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.