EllisLab text mark
Advanced Search
     
howto: return a non-empty random value
Posted: 21 June 2007 07:29 AM   [ Ignore ]
Joined: 2007-06-19
18 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>
            
{exp:weblog:entries weblog="guestbook" limit="1" orderby="random"}
            {guestbook_{site_lang}}
            
<class="signature">{guestbook_name_{site_lang}}</p>
            
{/exp:weblog:entries}
        
</div
 
Posted: 21 June 2007 07:35 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2002-04-29
26055 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?

 
Posted: 21 June 2007 08:08 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-06-19
18 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

 
Posted: 21 June 2007 10:13 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2002-04-29
26055 posts

How are you doing multi-lingual comments?

 
Posted: 21 June 2007 09:56 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-06-19
18 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

<p>{if site_lang == "en"}
    
<label for="field_id_76" class="required">{label_name_{site_lang}}</label><br />
    <
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><br />
    <
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}
</p>

<
p>{if site_lang == "en"}
    
<label for="field_id_78">{label_location_{site_lang}}</label><br />
    <
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><br />
    <
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}
</p>

<
p>{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}
</p

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.

 
Posted: 22 June 2007 07:35 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2002-04-29
26055 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.

 
Posted: 22 June 2007 07:37 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-06-19
18 posts

hi, 

The version of expressionengine is ExpressionEngine 1.5.2

 
Posted: 22 June 2007 08:15 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2002-04-29
26055 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>
{body}
{
/exp:weblog:entries}
{
/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?

 
Posted: 22 June 2007 08:59 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2007-06-19
18 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  "}
<p>{comment}</p>
{/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  "}
<p>{comment}</p>
{/exp:query} 
{
/if} 

Thanks very much for your help

 
Posted: 03 April 2009 05:35 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-31
6 posts
Sue Crocker - 22 June 2007 12:15 PM

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>
{body}
{
/exp:weblog:entries}
{
/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?

 
Posted: 03 April 2009 06:48 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-31
6 posts

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

 
Posted: 03 April 2009 06:53 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2005-10-16
104 posts

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

 Signature 

Lunchbox Collective, LLC
http://www.lunchboxcollective.com


“We produce websites worth talking about.”