Welcome Guest, Not a member yet? Register   Sign In
Show album data and images in my view
#1

[eluser]Lykos22[/eluser]
Hi, I 'd like some help please. I have 2 tables:
Code:
Albums with:
album_id
title
description

and Albumimages with:
img_id
album_id
image_file

What I want to do is to fetch all data from these two tables , loop through them and display them in my view so it should look something like portfolio section (2nd screenshot) of this template as the ending result.

This is what I have so far in my controller:
Code:
// fetch all album data
$albums = $this->album_model->get();

// fetch all images of each album
foreach ($albums as $album ) {
$images = $this->albumimage_model->get_by('album_id', $album->album_id);
}

$data = array(
'albums'  =>  $albums,
'images'  =>  $images,
);
$this->load->view('album_listing', $data);

my view:
Code:
<?php foreach($albums as $album): ?>
<div class="left-col">
<p>&lt;?php echo $album->title; ?&gt;</p>
<p>&lt;?php echo $album->description; ?&gt;</p>
<div>
&lt;?php endforeach; ?&gt;

&lt;?php foreach($images as $image): ?&gt;
<div class="right-col">
<img src="uploads/&lt;php? echo $album-&gt;title;  ?&gt;.'/'.&lt;php? echo $image-&gt;image_file;  ?&gt; " />
</div>
&lt;?php endforeach; ?&gt;

I'm not sure if this is the best way to do it as the running queries (which shown in Profiler) will be increase when more albums added. Is there a better way to do this ??

NOTE: the ending result in my view should be like the 2nd screenshot you see on the link.
#2

[eluser]CroNiX[/eluser]
Use a join? 1 query to get all published albums and their images.
#3

[eluser]CroNiX[/eluser]
something like:
Code:
$albums = $this->db
  ->select('Albums.album_id, Albums.title, Albums.description')
  ->select('Albumimages.img_id, Albumimages.image_file')
  ->join('Albumimages', 'Albumimages.album_id = Albums.album_id')
  ->order_by('Albums.album_id')
  ->get('Albums')
  ->result_array();
Then loop through them to create the albums
Code:
$last_album_id = 0;
foreach($albums as $album)
{
  if ($last_album_id !== $album['album_id'])
  {
    $last_album_id = $album['album_id'];
    //start new album output
    echo $album['title'] . '<br>';
  }
  else
  {
    //part of the last album, output image
    echo $album['image_file'] . '<br>';
  }
}

Probably not 100% correct, but should show you the way.
#4

[eluser]Lykos22[/eluser]
Well I tried to do it using join but when I loop through the data I get them twice each time (cause currently I have 2 albums), like:

album1 - images1
album1 - images1
album2 - images2
album2 - images2

I 'll have to test your way though, which has the if condition.*
I also noticed that my view isn't correct. So basicly I need one loop to display the data, but each one should appear once.

EDIT * still the same thing, I've tested it
#5

[eluser]Lykos22[/eluser]
Some additional information:
I've also tested the JOIN query
Code:
SELECT `albums` . * , `albumimages`.`image`
FROM `albums`
LEFT JOIN `albumimages` ON `albums`.`album_id` = `albumimages`.`album_id`
in phpmyadmin and here are the returned rows:
Code:
album_id,          title,                  description,                image
   1                 firstalbum             this is a test                  car.jpg
   1                 firstalbum             this is a test                  sailing.jpg
   2                 secondalbum        another test                 butterflies.jpg
   2                 secondalbum        another test                 dogs.jpg
So although the data return correct, when I loop through them in my view I get the same rows as above.
What I actually want to display in my view is this:
Code:
album title 1
description  1                             and here all images of 1st album

-----------------------------------------------------------------------------------
album title 2
description   2                            and here all images of 2nd album




Theme © iAndrew 2016 - Forum software by © MyBB