ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

howto: return a non-empty random value

June 21, 2007 7:29am

Subscribe [4]
  • #1 / Jun 21, 2007 7:29am

    ussher

    14 posts

    Hi,

    I have found the orderby=“random” for use in returning queries from a database but i have been unable to locate the documentation on how to include a where this field is not empty clause.

    in sql it would be something like

    select * from comments where comment not like ‘’

    is there a way to do this in expression engine.

    the current template is this

    <div id="sb_guestbook" class="sideblock">
                <h3>{label_from_guestbook_{site_lang}}</h3>
    <p>            {exp:weblog:entries weblog="guestbook" limit="1" orderby="random"}<br />
                {guestbook_{site_lang}}<br />
                {guestbook_name_{site_lang}}<br />
                {/exp:weblog:entries}<br />
            </div>

  • #2 / Jun 21, 2007 7:35am

    Sue Crocker

    26054 posts

    So in other words, you want to return information back at random only if the weblog guestbook entry has at least one comment?

    Or what exactly?

  • #3 / Jun 21, 2007 8:08am

    ussher

    14 posts

    Exactly.

    Because this site has an english and a japanese side when a comment is posted in japanese then the english comment is blank.

    the reverse is true too. when a comment is posted in english the japanese section is blank.

    The problem comes when a random comment is chosen but the comment entry is made in the other language.

    so for example if entry 1001 is made in japanese the entry will look like this:
    english:

    japanese:
    “we had a great time rafting with your company”

    Then when a random comment is selected and the user is on the english side of the site then they will see ” “

    If the user is using the japanese site then they will see the comment.

    cheers

  • #4 / Jun 21, 2007 10:13am

    Sue Crocker

    26054 posts

    How are you doing multi-lingual comments?

  • #5 / Jun 21, 2007 9:56pm

    ussher

    14 posts

    Hi,

    I did not create the system, I have just been asked to help tidy up some stuff.

    The way the multilingual comments is set up a different column in the database is used for containing the separate values.

    here is some code from the booking form

    {if site_lang == "en"}
        <label for="field_id_76" class="required">{label_name_{site_lang}}</label>
    
        <input type="text" name="field_id_76" id="field_id_76" dir="ltr" size="40" maxlength="60" value="" />
        <input type="hidden" name="field_ft_76" value="none" />
        {if:elseif site_lang == "ja"}
        <label for="field_id_77" class="required">{label_name_{site_lang}}</label>
    
        <input type="text" name="field_id_77" id="field_id_77" dir="ltr" size="40" maxlength="60" value="" />
        <input type="hidden" name="field_ft_77" value="none" />
        {/if}
    
    
    {if site_lang == "en"}
        <label for="field_id_78">{label_location_{site_lang}}</label>
    
        <input type="text" name="field_id_78" id="field_id_78" dir="ltr" size="40" maxlength="60" value="" />
        <input type="hidden" name="field_ft_78" value="none" />
        {if:elseif site_lang == "ja"}
        <label for="field_id_79">{label_location_{site_lang}}</label>
    
        <input type="text" name="field_id_79" id="field_id_79" dir="ltr" size="40" maxlength="60" value="" />
        <input type="hidden" name="field_ft_79" value="none" />
        {/if}
    
    
    {if site_lang == "en"}
        <textarea name="field_id_64" id="field_id_64" dir="ltr" cols="60" rows="12"></textarea>
        <input type="hidden" name="field_ft_64" value="br" />
        {if:elseif site_lang == "ja"}
        <textarea name="field_id_65" id="field_id_66" dir="ltr" cols="60" rows="12"></textarea>
        <input type="hidden" name="field_ft_66" value="br" />
    {/if}
    

    as you can see it changes the form feild name depending on what language the interface is currently set to.

    If the person is viewing the site in japanese and enters an english comment then that would be considered a japanese comment and showen to japanese customers.

    The problem with this system is that when a comment is left in one language the other language is left blank for this row so this is why i need to be able to only select a value if the value is not empty

    Thanks for your help.

  • #6 / Jun 22, 2007 7:35am

    Sue Crocker

    26054 posts

    When you said comments, I assumed you meant weblog comments where there is a comment to an existing entry. What you are referring to are actual entries, not comments.

    So in other words, if someone is creating an entry in Japanese, the text is saved in field_id_66, and in English it is field_id_64.

    Which version/build of EE are you on? You can find that in the bottom of each CP page.

  • #7 / Jun 22, 2007 7:37am

    ussher

    14 posts

    hi, 

    The version of expressionengine is ExpressionEngine 1.5.2

  • #8 / Jun 22, 2007 8:15am

    Sue Crocker

    26054 posts

    Here’s a very simple bit of code I used on one of my sites.

    {exp:query sql="select entry_id, field_id_2 as body from exp_weblog_data where weblog_id = '5' and field_id_2 !='' order by RAND() limit 1  "}
    {exp:weblog:entries entry_id="{entry_id}" disable="categories|member_data|trackbacks"}
    <h3>{title}</h3>
    <p>{body}<br />
    {/exp:weblog:entries}<br />
    {/exp:query}

    I’m using the query module to bring back a random entry from the test weblog where there is a value in field_id_2 (which in this case is the body field).

    Then I’m using the generated entry_id to pass it’s value to the exp:weblog:entries tag.

    In your case you’d have two different queries to use, depending on the site_lang.

    Does this simple example help?

  • #9 / Jun 22, 2007 8:59am

    ussher

    14 posts

    Awesome!
    That worked perfectly.

    {if site_lang == "en"}
    {exp:query sql="select field_id_64 AS comment  from ee_weblog_titles as t, ee_weblog_data as d where t.entry_id =  d.entry_id and t.weblog_id=d.weblog_id and t.title = 'Guestbook Entry' and field_id_64 !='' order by RAND() limit 1  "}
    {comment}
    {/exp:query} 
    {if:elseif site_lang == "ja"}
    {exp:query sql="select field_id_65 AS comment  from ee_weblog_titles as t, ee_weblog_data as d where t.entry_id =  d.entry_id and t.weblog_id=d.weblog_id and t.title = 'Guestbook Entry' and field_id_65 !='' order by RAND() limit 1  "}
    {comment}
    {/exp:query} 
    {/if}

    Thanks very much for your help

  • #10 / Apr 03, 2009 5:35pm

    Here’s a very simple bit of code I used on one of my sites.

    {exp:query sql="select entry_id, field_id_2 as body from exp_weblog_data where weblog_id = '5' and field_id_2 !='' order by RAND() limit 1  "}
    {exp:weblog:entries entry_id="{entry_id}" disable="categories|member_data|trackbacks"}
    <h3>{title}</h3>
    <p>{body}<br />
    {/exp:weblog:entries}<br />
    {/exp:query}

    Sue, I’m trying to do something very similar to what you posted here! I want to publish 1 random entry, in my case, ONLY when 2 fields are populated.

    How would you adjust the code in this case?

    weblog id is 6
    first field id is 12 and field name is staff-bio
    second field id is 29 and field name is staff-cutout

    Also, do I have to change anything under template preferences when it comes to Allow PHP? and PHP parsing?

  • #11 / Apr 03, 2009 6:48pm

    Please disregard, solved the issue. Was passing the wrong field id’s in the query tag.

  • #12 / Apr 03, 2009 6:53pm

    Sue, no response necessary. We have worked it out. This is a useful method for custom queries of weblog data!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases