Welcome Guest, Not a member yet? Register   Sign In
Nested foreach Loops in a Gallery Application
#1

[eluser]Unknown[/eluser]
I'm currently making myself a gallery site with images sorted into albums. Here's a sample concept of a gallery page:

Five albums are displayed on a page with 3 to 5 image thumbnails displayed in each album.

Though I'm able to get the view to loop through the five albums, I'm not sure how to get it to loop through the images that belong to them.

Here's the model code:

Code:
function get_albums($category)
{    
    $this->db->select('albums.title, albums.description');
    $this->db->join('categories', 'categories.id = albums.category_id');
    $this->db->where('categories.title', $category);
    $query = $this->db->get('albums');
    return $query->result();
}

Here's the controller code:

Code:
function index()
{    
    $category = 'some_category';
    
    $data['query'] = $this->gallery_model->get_albums($category);

    $this->load->view('album_view', $data);
}

And here's the view code:

Code:
<?php foreach ($query as $album): ?>
    <div class="album_container">
        <div class="thumbs_container">

            &lt;!-- The following five images should be gotten dynamically. Each belong to this specific album --&gt;

            <a href="/"><img class="thumb" src="/images/1.jpg" /></a>
            <a href="/"><img class="thumb" src="/images/2.jpg" /></a>
            <a href="/"><img class="thumb" src="/images/3.jpg" /></a>
            <a href="/"><img class="thumb" src="/images/4.jpg" /></a>
            <a href="/"><img class="thumb" src="/images/5.jpg" /></a>
        </div>
            
        <div class="info_container">
            <h2 class="album_title">&lt;?= $album->title; ?&gt;</h2>
                
            <p class="album_description">&lt;?= $album->description; ?&gt;</p>
        </div>
    </div>
&lt;?php endforeach; ?&gt;

How do I go about doing this? I was considering making a nested foreach loop for the images, but I'd have to pass album information back to the model from within the loop. I was also considering storing image info into a nested array within the model but don't know how to go about that either.

Can anyone help me out here?

Thanks in advance for the help.
#2

[eluser]Burak Guzel[/eluser]
You need to build a multi-dimensional array that has albums and their images, before passing it to the View. Instead of passing a query result object directly.




Theme © iAndrew 2016 - Forum software by © MyBB