We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

random, limit and if statement help.

How Do I?

andrew001's avatar
andrew001
136 posts
9 years ago
andrew001's avatar andrew001

So, Im trying to display a few images on the home page. Here is the code I have no, and it’s not working:

<div class="expanded row">
{exp:channel:entries channel="portfolio" limit="3" orderby="random" disable="pagination"}
   {if '{img_on_home}' == "Yes"} 
     <div class="large-4 medium-4 columns zero-pad">{home_page_image}</div>
   {/if}
{/exp:channel:entries}
</div>

Now, the output of this should look like:

<div class="expanded row">
     <div class="large-4 medium-4 columns zero-pad">path/to/image</div>
     <div class="large-4 medium-4 columns zero-pad">path/to/image</div>
     <div class="large-4 medium-4 columns zero-pad">path/to/image</div>
</div>

I do not understand why this does not work. I have a hunch its something to do with “random” or the “limit”

The idea here is, “img_on_home” is a checkbox, and there is a file (image)associated with it. Would you like to display an image on the home page? Check for yes, then upload the image. The user could upload any amount of images, but I only want to display 3 at random. Makes sense?

How would I go about pulling this off?

Thanks

       
James Mathias's avatar
James Mathias
225 posts
9 years ago
James Mathias's avatar James Mathias

Hi,

can you try changing

{if '{img_on_home}' == "Yes"}

to

{if img_on_home == 'Yes'}

Also make sure that the value for that checkbox is in fact capitalized, it’s case sensitive.

       
andrew001's avatar
andrew001
136 posts
9 years ago
andrew001's avatar andrew001

well, the problem is, it randomly displays the images. When I mean random, it’s like, sometimes one, then two, then none at all, then maybe one, etc…. but never all three at once.

Even tho I have a total of 5 in the DB. I only want to display 3, at random.

Make sense?

Not sure why this happens.

       
James Mathias's avatar
James Mathias
225 posts
9 years ago
James Mathias's avatar James Mathias

try adding

dynamic='no'

to the channel entries tag. Without that the URL can affect the output, which what sounds like what is happening.

       
andrew001's avatar
andrew001
136 posts
9 years ago
andrew001's avatar andrew001

same result. =(

       
andrew001's avatar
andrew001
136 posts
9 years ago
andrew001's avatar andrew001

If I run this:

{exp:channel:entries channel="portfolio" disable="pagination"}
    <div class="large-4 medium-4 columns zero-pad">{home_page_image}</div>
{/exp:channel:entries}

I get the expected results. For example, I have 9 entries. lets say 4 of them are tagged as described above (show on home). The above code will spit out all entries, and return images as well as “broken images” - this is expected… I’ve chosen all, but only a few entries have this particular image.

Now, if I add limit=”3”, I get the expected results again, with some images broken. Logic says, I should “filter” these, so I add the “if statement” ….. if the checkbox that reads “show on home page only” is checked, then that would be the filter.

Show on home page? yes/no if “show_on_home_page” == yes, then display.

Since there are more than three, randomize these.

So we finally end up with the following:

<div class="expanded row">
{exp:channel:entries channel="portfolio" limit="3" orderby="random" disable="pagination"}
   {if '{img_on_home}' == "Yes"} 
     <div class="large-4 medium-4 columns zero-pad">{home_page_image}</div>
   {/if}
{/exp:channel:entries}
</div>

But this fails. I do in fact get random images, sometimes one, sometimes two, and other times none at all… but NEVER three. Ever.

SOOO…. it seems like I need to run the the if statement BEFORE the channel, but that obviously wont work. I pull 3, at random, but we dont know if they are properly tagged yet. then i run the if statement, which will return nothing at all.

So, the question is:

How can i limit by 3 in the if statement… i guess?

       
andrew001's avatar
andrew001
136 posts
9 years ago
andrew001's avatar andrew001

and…. here is the answer, for anyone that cares….

https://ellislab.com/forums/archive/viewthread/213856

…search:img_on_home=”=yes”

{exp:channel:entries channel="portfolio" limit="3" orderby="random" disable="pagination" search:img_on_home="=yes"}

Woot.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.