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.

eZ debug

Timing: Jan 18 2025 19:17:28
Script start
Timing: Jan 18 2025 19:17:28
Module start 'content'
Timing: Jan 18 2025 19:17:29
Module end 'content'
Timing: Jan 18 2025 19:17:29
Script end

Main resources:

Total runtime1.1706 sec
Peak memory usage4,096.0000 KB
Database Queries223

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0113 587.7109180.8359
Module start 'content' 0.01130.9610 768.5469676.4297
Module end 'content' 0.97230.1982 1,444.9766344.4375
Script end 1.1705  1,789.4141 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00460.3900210.0002
Check MTime0.00170.1415210.0001
Mysql Total
Database connection0.00070.058010.0007
Mysqli_queries1.065891.04962230.0048
Looping result0.00350.30152210.0000
Template Total1.125896.220.5629
Template load0.00210.175720.0010
Template processing1.123896.002120.5619
Template load and register function0.00020.014210.0002
states
state_id_array0.00180.150910.0018
state_identifier_array0.00270.230920.0014
Override
Cache load0.00210.1768830.0000
Sytem overhead
Fetch class attribute can translate value0.00160.137140.0004
Fetch class attribute name0.00170.1480130.0001
XML
Image XML parsing0.00100.088040.0003
class_abstraction
Instantiating content class attribute0.00000.0023140.0000
General
dbfile0.00320.2762270.0001
String conversion0.00000.000630.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
12content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
19content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 42
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs