I want to randomize link order on a webpage

Discussion in 'JavaScript' started by UncleP, Aug 30, 2011.

  1. #1
    Can anyone help with this?

    I have a webpage with links that I want to re-order at random, so that on every pageload the links are in a different order. There will be a set number of links (it may vary) and I want to display them all everytime, just re-orered, so they won't want to be repeated. I have tried a few codes found on the web but none will do what I want, they either show only one link or don't randomize. The links themselves contain table attributes (<td>, <tr> etc.) as well as div/class ID's for layout and I think that's were the problem lies, but don't know for sure.

    It would be perfectly alright to have them in a string array and randomize the displaying of the array, but there doesn't want to be any part of the string that will affect the javascript code. It must be javascript as it's basicaly an html page, php won't work at all. For an idea of what I'd like there's something similar here: http://www.javascriptkit.com/script/script2/imageorder.shtml but that won't work when I use the string I want to randomize, for whatever reason. It's the closest though. If there's different sections that randomize independently like the example (above) that would be a bonus.

    I would also like to be able to randomize a menu links list as well, if it can be used that way, so that 'partner links' can be re-ordered in a sidebar for instance, if that's possible too, but the first is the most important.

    Just to give some more info here's an example of the kind of string I wish to randomize:
    
    <!-- Link #1 -->
    <tr class="featured">
    	<td class="url"><a href="hits.php?id=1" target="_blank" rel="nofollow" title="http://www.domain-1.com">http://domain-1.com</a></td>
    	<th class="type">{type}</th>
    	<th class="speed" align="center"><img src="{speed}.jpg" title="Rating {speed}"></th>
    	<td class="country" align="center"><img src="images/{flag}.gif" title="{flag}"></td>
    	<td class="hits">{count}</td>
    	<td class="time">{date}</td>
    </tr>
    <!-- End Link #1 -->
    
    <!-- Link #2 -->
    <tr class="featured">
    	<td class="url"><a href="hits.php?id=2" target="_blank" rel="nofollow" title="http://www.domain-2.com">http://domain-2.com</a></td>
    	<th class="type">{type}</th>
    	<th class="speed" align="center"><img src="{speed}.jpg" title="Rating {speed}"></th>
    	<td class="country" align="center"><img src="images/{flag}.gif" title="{flag}"></td>
    	<td class="hits">{count}</td>
    	<td class="time">{date}</td>
    </tr>
    <!-- End Link #2 -->
    
    <!-- Link #3 -->
    <tr class="featured">
    	<td class="url"><a href="hits.php?id=3" target="_blank" rel="nofollow" title="http://www.domain-3.com">http://domain-3.com</a></td>
    	<th class="type">{type}</th>
    	<th class="speed" align="center"><img src="{speed}.jpg" title="Rating {speed}"></th>
    	<td class="country" align="center"><img src="images/{flag}.gif" title="{flag}"></td>
    	<td class="hits">{count}</td>
    	<td class="time">{date}</td>
    </tr>
    <!-- End Link #3 -->
    
    <!-- more links etc. -->
    
    Code (markup):
    I'm not a coder or programmer so I can't make my own and I really don't know were to look except on google. I hope this is an interesting problem for someone that likes a challenge, or it could be so simple it's an easy job for somebody :)

    TIA.
     
    Last edited: Aug 30, 2011
    UncleP, Aug 30, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Put the text for the links (including the tags) into your database. SELECT the links ordering by random.

    SELECT * FROM links ORDER BY RAND() LIMIT 5

    would give you 5 random links from that table.
     
    Rukbat, Aug 31, 2011 IP
  3. UncleP

    UncleP Well-Known Member

    Messages:
    138
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    135
    #3
    I already know how to randomize the database output but that's not what I want to do. I want a 'featured' section above all the other links, so there will only be a limited number and they will be specific url's, not changing links only link order. I can do and have done featured, I just want to randomize the order of them to give all links a fair chance to be at the top, randomly.
     
    UncleP, Aug 31, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Letting MySQL do it is a lot easier (as far as writing the page) than letting Javascript do it. And it makes more sense than writing PHP to do it.
     
    Rukbat, Aug 31, 2011 IP
  5. UncleP

    UncleP Well-Known Member

    Messages:
    138
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Still looking for the JavaScript solution to this. Anyone else care to try? $5 (via PayPal) for a fix?
     
    UncleP, Aug 31, 2011 IP