Forums / Developer / No Relation? Pick one at random

No Relation? Pick one at random

Author Message

David Jones

Thursday 31 August 2006 6:23:46 am

I have lots of images in my media folder in a class called sub_image.

I add these to a page by adding them as a related object. This is working great.

However, if one isn't related I wont to automatically pick one at random to display from eveything in the class sub_image.

I'm sure that this must be possible.

Any ideas.

Thanks

Claudia Kosny

Thursday 31 August 2006 11:49:11 am

Hi David

I have given you the link to this already in regards to another topics, but if you read this first, you can find some ideas on hwo to fetch a random node in this thread:
http://ez.no/community/forum/setup_design/how_to_fetch_random_content_3_8/re_how_to_fetch_random_content_3_8__3

Limit the fetch function to objects of the class sub_image, the rest is the same as usual.

Greetings from Luxembourg

Claudia

David Jones

Monday 04 September 2006 6:30:16 am

Thanks but I can't seem to get this to work

This is what I have

{$max=fetch('content', 'tree_count', hash(parent_node_id, 132))}
{$randsub_imaget=fetch('content', 'tree', 
        hash(parent_node_id, 132, offset, rand(0,$max), limit, 1))}
		
             {$randsub_image.data_map.title.content|wash}
	     test: {rand(0,$max)}

132 is the node in the media folder which contains my sub images.

Not only does

{$randsub_image.data_map.title.content|wash}

not return anything but

test: {rand(0,$max)}

returns test: 0 indicating that there are no items.

What am i doing wrong?

Thanks

Claudia Kosny

Monday 04 September 2006 7:31:25 am

Hello David

Do you get any error messages in the debug output?

I miss the def operator in your code (it must be {def $max=... } ). So if that is a real copy of the code you use please declare the variables properly and try again. You alos have a type in the second line, $randsub_imaget should be without the letter t at the end.
If these are just typos in your post and not n your code I frankly do not know at the moment - the code works for me. In this case please check your debug output.
Please note as well that the rand operator seems to be available only in EZ 3.8.x.

Greetings from Luxembourg

Claudia

David Jones

Monday 04 September 2006 7:50:55 am

Sorry, those were just typos.

I am using 3.8.0 and I have no errors in the debug.

Thanks anyway for your help.

Claudia Kosny

Monday 04 September 2006 12:05:02 pm

Hello David

What value does $max have? If it is 0 then you are fetching from the wrong node or do not have permission to to fetch anything from there, so make sure this correct.

If $max is alright try the rand operator on its own with some other values eg. rand(0, 5) or so. Do you get any other results than 0?

Claudia

David Jones

Tuesday 05 September 2006 2:44:42 am

Thanks again Claudia,

$max does equal 0

The anonymous user has read access to that node.

132 is the node which contains all the subimages from which I wish to select something random.

Is this correct?

Thanks

Xavier Dutoit

Tuesday 05 September 2006 3:42:01 am

Hi,

1) Does anonymous have read access on the images ?
2) are you sure that the node id is 132 ?
3) are you 100% sure that the node id is 132 ?
4) isn't it the object id instead ? ;)

Anyway, if you put all the images directly under the node 132, then fetch list instead of fetching tree (and list_count instead of tree_count).

X+

http://www.sydesy.com

David Jones

Tuesday 05 September 2006 4:12:48 am

Thanks Xavier,

Switching it to list seems to have fixed the issue.

For those interested I have ended up with this

	{def $max=fetch('content', 'list_count', hash(parent_node_id, 132))}
						{let $randSub_images=fetch('content', 'list', hash(parent_node_id, 132, offset, rand(0,$max), limit, 1))}
							{section loop=$randSub_images var=randSub_image}
								{$randSub_image.data_map.title.content|wash}
							{/section}
						{/let}

I do have one problem though.

There are currently 4 images in the folder. However, rand seems to be picking a number between 0 and 4 which means sometimes (ie when it picks 0) no image is displayed.

Can I stop it pulling out zero?

Claudia Kosny

Tuesday 05 September 2006 4:59:16 am

Hello David

Just use $max|sub(1) as parameter for the rand operator - as far as I recall an offset of 4 causes the problem, not a offset of 0.

Claudia

Xavier Dutoit

Tuesday 05 September 2006 5:13:14 am

rand (a,b) could equals b ?

I thought it was in [a..b[ (ie. never include the upper limit). Anyway, going to check if this is a problem I have for one of my site.

FYI and to solve the cache problem, what I did in your case was to fetch n images (randomly), build a javascript array with all the images' urls, and write a tiny javascript script that does the random "fetch" on the client side.

Therefore, with the same cache, I got several different random images.

X+

http://www.sydesy.com

David Jones

Tuesday 05 September 2006 5:56:22 am

Thanks Claudia, That was spot on.

Thanks again to you both for all your help.