Welcome Guest, Not a member yet? Register   Sign In
Retrieve Random Photo for Album Cover
#1

[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">&lt;?php echo anchor('member/photos/' . $album->slug, $album->name); ?&gt;</div>
        &lt;?php $image_url = ($album->photo->thumbnail != '') ? base_url() . 'media/photos/' . $album->profile_id . '/thumbnail/' . $album->photo->thumbnail : asset_url() . '/images/no_image.gif'; ?&gt;
        <img src="&lt;?php echo $image_url; ?&gt;" alt="&lt;?php echo $album-&gt;slug; ?&gt;" />
    </div>
&lt;?php } ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB