Just wondering if anyone has done somthing similar and would like to share their code?
I need to be able to display a random 5 (Or whatever number I pass in) child nodes from a specific Parent Node (And make sure the child nodes shown are not the same). Basically there are about 30+ child nodes and on each page load a different set of the child nodes need to be shown? Is this an XSLT thing or should I try ASP.NET?
Take a look into this post where I used the random function for medias, but this can also be used for content nodes. The only thing you have to do is to limit the output via position or so.
Thanks Thomas thats great - Only slight hiccup I can see is making sure the same image isn't displayed twice? As its generating a random number it generate the same number twice, thus showing the same image? Any ideas on how to get round that?
As I remember is the random function initialized by umbraco and stored in the umbraco cache. So the functions shouldn't return doubled values. But I could be wrong. Take a look into the GetRandom function of umbraco.library:
@thomas: this won't help if he needs more random pics on the same page, using that function it's still possible to get two identical values...
@Lee: i've done similar things (using new Random().Next()), store any number of values in a string (using an xslt extension), which could look like '-15--56--89-' and use this string to compare against the node being processed. Don't have the example at hand, but shout if you need more info.
Displaying Specific Number Of Random Child Nodes?
Hi All :)
Just wondering if anyone has done somthing similar and would like to share their code?
I need to be able to display a random 5 (Or whatever number I pass in) child nodes from a specific Parent Node (And make sure the child nodes shown are not the same). Basically there are about 30+ child nodes and on each page load a different set of the child nodes need to be shown? Is this an XSLT thing or should I try ASP.NET?
Take a look into this post where I used the random function for medias, but this can also be used for content nodes. The only thing you have to do is to limit the output via position or so.
Thomas
Thanks Thomas thats great - Only slight hiccup I can see is making sure the same image isn't displayed twice? As its generating a random number it generate the same number twice, thus showing the same image? Any ideas on how to get round that?
As I remember is the random function initialized by umbraco and stored in the umbraco cache. So the functions shouldn't return doubled values. But I could be wrong. Take a look into the GetRandom function of umbraco.library:
Anyone else with experience for the random function?
Take a look here: http://msdn.microsoft.com/en-us/library/system.random%28VS.80%29.aspx
@thomas: this won't help if he needs more random pics on the same page, using that function it's still possible to get two identical values...
@Lee: i've done similar things (using new Random().Next()), store any number of values in a string (using an xslt extension), which could look like '-15--56--89-' and use this string to compare against the node being processed. Don't have the example at hand, but shout if you need more info.
Cheers,
/Dirk
Hi Dirk - Excellent thanks! If you have got an example it would be great, I'm slowly trying to get my head round this XSLT stuff :(
Did anyone get anywhere with this? I have the same requirement and I can't find a decent solution.
Hi David
Could this be of any use? http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers
/Jan
David -
Have a look at http://umbraco.miketaylor.eu/2010/08/26/random-sequence/
This uses an XSLT extension to create an XML fragment containing a series of numbers in a random order.
You can then loop through this sequence in your XSLT like this:
Hope that helps,
Mike
is working on a reply...