[eluser]RMinor[/eluser]
I am displaying a list of albums that a user has and I want to retrieve a random photo from that particular album to display as the cover photo. What is the best way to go about doing this? I was thinking about doing a foreach() loop in the controller and then running a query to grab a random photo within the loop, but then I was unsure on how to match it up with the right album. My code is below.
Controller:
Code:
public function index() {
$data['albums'] = $this->album_model->get_many_by('profile_id', $this->member_id);
// I need to grab a random photo to display
$data['current_albums'] = $this->album_model->count_by('profile_id', $this->member_id);
$this->load->model('profile_model');
$data['maximum_albums'] = $this->profile_model->with('membership')->get($this->member_id);
$data['allowed_albums'] = $data['maximum_albums']->membership->albums - $data['current_albums'];
$this->layout->title(site_name() . ' | My Albums');
$this->layout->view('member/albums/index', $data);
}
View:
Code:
<?php foreach ($albums as $album) { ?>
<div class="span4 text-center">
<div class="caption"><?php echo anchor('member/photos/' . $album->slug, $album->name); ?></div>
<?php $image_url = ($album->photo->thumbnail != '') ? base_url() . 'media/photos/' . $album->profile_id . '/thumbnail/' . $album->photo->thumbnail : asset_url() . '/images/no_image.gif'; ?>
<img src="<?php echo $image_url; ?>" alt="<?php echo $album->slug; ?>" />
</div>
<?php } ?>