Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lee 1130 posts 3088 karma points
    Sep 03, 2009 @ 16:03
    Lee
    0

    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?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 16:11
    Thomas Höhler
    0

    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

  • Lee 1130 posts 3088 karma points
    Sep 03, 2009 @ 16:28
    Lee
    0

    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?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 16:45
    Thomas Höhler
    0

    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:

    public static Random GetRandom(int seed)
    {
    Random random = (Random) HttpContext.Current.Cache.Get("RandomNumber");
    if (random == null)
    {
    if (seed == 0)
    {
    random = new Random();
    }
    else
    {
    random = new Random(seed);
    }
    HttpContext.Current.Cache.Insert("RandomNumber", random);
    }
    return random;
    }

    Anyone else with experience for the random function?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Sep 03, 2009 @ 16:54
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Sep 03, 2009 @ 17:27
    Dirk De Grave
    0

    @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

  • Lee 1130 posts 3088 karma points
    Sep 03, 2009 @ 17:30
    Lee
    0

    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 :(

  • David 13 posts 33 karma points
    Feb 02, 2011 @ 17:25
    David
    0

    Did anyone get anywhere with this? I have the same requirement and I can't find a decent solution.

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 02, 2011 @ 19:46
  • Mike Taylor 155 posts 353 karma points
    Feb 05, 2011 @ 18:22
    Mike Taylor
    0

    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:

    ...
    <xsl:variable name="allNodes" select="{{ select statement here }}" />
    <xsl:for-each select="randSeq:RandomSequence(count($allNodes))">
       <xsl:variable name="thisNum" select="text()" />
       <xsl:variable name="thisNode" select="$allNodes[position() = $thisNum]" />
    
       <!-- do something with $thisNode -->
    
    </xsl:for-each>

    Hope that helps,

    Mike

Please Sign in or register to post replies

Write your reply to:

Draft