Random Favorite photos in photo gallery
Posted: 03 July 2008 09:31 AM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  02-18-2008

I am having a hard time figuring out if there is a way to show random favorite images in the Photo Gallery module. I set up a custom field one with the name “favorite”, and on my favorite photos, I type in “yes”.

I want my box to show probably 6 photo thumbnails. The problem I am having is when I orderby=“random”, it seems to randomizes all the photos, instead of just ones marked favorite. So in my box, it will show 1, or 2, or no photos (e.g. it seemed to randomly pick 6 photos, but only one of the random photos were marked as a favorite, so it only showed one).

here is my latest code—and I’ve tried a bunch of different ways.

{exp:gallery:entries gallery=”{gallery_name}” orderby=“random” columns=“8” rows=“8” limit=“4”}
{entries}
{row}
{if custom_field_one == “yes”}<a href=”{id_path=gallery/image_full}”>{thumb_url}</a>{/if}
{/row}
{/entries}
{/exp:gallery:entries}

It seems to me that my IF statement is in the wrong place, but I can’t figure out where to put it.

Profile
 
 
Posted: 03 July 2008 09:43 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

An if isn’t really a good way to handle this.  Even if it works as intended- it just varies up the display of the entries returned.  If you randomly get no photos with ‘favorite’ in the custom field- you’ll end up with no photos showing. 

What you really want to do for this is specify that EE only pull back ‘favorite’ entries.  About the only way to do it stock is to make ‘favorite’ a category rather than a custom field- and use the category paramter.

That should work fine- and you can ditch the conditional.

However- that will jam things up if you don’t want to use categories.  In which case- the best way to do it is via a custom query and the query module.  So- I stuck the above tag all by itself in a template, turned show queries on, and hunted down the query used to generate the results:

SELECT e.*, p.gallery_image_url, p.gallery_thumb_prefix, p.gallery_medium_prefix, p.gallery_text_formatting, p.gallery_auto_link_urls, p.gallery_cf_one_formatting, p.gallery_cf_one_auto_link, p.gallery_cf_two_formatting, p.gallery_cf_two_auto_link, p.gallery_cf_three_formatting, p.gallery_cf_three_auto_link, p.gallery_cf_four_formatting, p.gallery_cf_four_auto_link, p.gallery_cf_five_formatting, p.gallery_cf_five_auto_link, p.gallery_cf_six_formatting, p.gallery_cf_six_auto_link, c.cat_folder, c.cat_name, c.cat_description, m.screen_name, m.username FROM exp_gallery_entries AS e LEFT JOIN exp_galleries AS p ON p.gallery_id = e.gallery_id LEFT JOIN exp_gallery_categories AS c ON c.cat_id = e.cat_id LEFT JOIN exp_members AS m ON e.author_id = m.member_id WHERE e.gallery_id = '1' AND e.status = 'o' AND e.entry_date < 1215117450 ORDER BY rand() desc LIMIT 4


The date stuff is dynamic- you’d have to use php to properly specify ‘now’- but unless you have entries in the future you don’t want to show- you can just ditch the data bit.  You’d need to add in the check on the custom field after the WHERE bit: WHERE custom_field

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 03 July 2008 09:48 AM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Bugger- it’s eating my code.  But that’s the gist.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 03 July 2008 10:10 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  6
Joined  02-18-2008

I think I’ve got it working (at least it is so far). Here is the code I used:

{exp:query sql=“SELECT entry_id FROM exp_gallery_entries WHERE custom_field_one = ‘yes’ ORDER BY rand() LIMIT 6”}
{exp:gallery:entries gallery=”{gallery_name}” entry_id=”{entry_id}” dynamic=“off”}
{entries}
{row}
<a href=”{id_path=gallery/image_full}”>{thumb_url}</a>
{/row}
{/entries}
{/exp:gallery:entries}
{/exp:query}

Profile
 
 
Posted: 03 July 2008 10:18 AM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

No- you seriously don’t want to do it that way.  It’s the equivilant of putting 6 gallery tags on the page.  The added overhead just is not worth it.  The query approach isn’t killer doing it that way- IF you do the query in php, turn php parsing on and set it to output- then echo out the resulting pipe delimited list of entry ids.  That way you are only adding one extra query to the page.  Rather than 6 weblog tags.

Er- long way of saying- what you have there will work, but I wouldn’t do it that way.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 03 July 2008 10:33 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  6
Joined  02-18-2008

I’m sorry—I know practically no PHP. Do you have an example of what you are talking about?

Profile
 
 
Posted: 06 July 2008 09:34 AM   [ Ignore ]   [ # 6 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23522
Joined  05-20-2002

Ah- sorry.  And like I say- I’d probably do the whole thing w/the query module instead of using the gallery tag.  But- turn ‘php parsing’ on for the template- set it to parse on input.  Then- take a look at the developer docs- db class.

<?php
global $DB;
$ids = array();
$query = $DB->query("SELECT entry_id FROM exp_gallery_entries WHERE custom_field_one = 'yes' ORDER BY rand() LIMIT 6");

    if (
$query->num_rows > 0)
    
{
        
foreach($query->result as $row)
        
{
            $ids[]
= $row['entry_id'];
        
}
    }
$entry
= implode("|", $ids);
?>

Some stuff that is not php
- yada yada yada

{exp
:gallery:entries gallery="{gallery_name}" entry_id="<?php echo $entry; ?>" dynamic="off"}
etc
....

Does pretty much the same thing- but with 2 queries rather than 7.  (In general.)

Make sense?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64914 Total Logged-in Users: 22
Total Topics: 81869 Total Anonymous Users: 20
Total Replies: 440136 Total Guests: 170
Total Posts: 522005    
Members ( View Memberlist )
Newest Members:  smilepolitelyrvmcleodbjmohrAqua193Bios Elementmjpoteetguimogranwelshmrcfthenetmonkey