randomizing link display order

Posted on 2005-04-23
Last Modified: 2006-11-18

Right now, I have:

$leftNav = '
<a href="sample.html">Sample Link</a><br>
<a href="example.html">Example Link</a><br>
<a href="another.html">Another Link</a><br>

And I put those on my page where I want them with an <?echo $leftnav;?>

I would like to display those links in random order each time the page loads, so the link order is not the same each time. I assume I'll need to assign a value to each individual link (about 20 in all) and then...?

Thank you for assistance,     Chris

Question by:St_Aug_Beach_Bum
    LVL 5

    Accepted Solution

    Here's a function I've used before that I found:

      $links=array('Sample Link' => 'sample.html',
                   'Example Link' => 'example.html',
                   'Another Link' => 'another.html');

      foreach ($shuffled_links as $n=>$l)
        print "<a href=\"$l\">$n</a><br>";
    function shuffle_me($shuffle_me)
       $randomized_keys = array_rand($shuffle_me, count($shuffle_me));
       foreach($randomized_keys as $current_key)
           $shuffled_me[$current_key] = $shuffle_me[$current_key];

       return $shuffled_me;
    LVL 5

    Expert Comment

    Pressed "submit" too soon :(

    $links is your associative array of link titles=>links.  Just fill that out with as many as you need.  The shuffle_me function shuffles the array without losing the associativeness (ok, that's not really a word) of the array as the normal shuffle function does.  There may be a simpler way, but this one works.
    LVL 3

    Expert Comment

    try this.

    srand ((float) microtime() * 10000000);
    $input = array('
    <a href="sample.html">Sample Link</a><br>','<a href="example.html">Example Link</a><br>','<a href="another.html">Another Link</a><br>');



    while (list(, $nav) = each($input)) {
       echo "$nav ";

    hope this also helps.


    Author Comment


    Thank you for your comments, I will use a variation of ljw87505's answer,

    LVL 3

    Expert Comment


    I understand your concern about posting the comment.
    I will do my best in the future.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    get users IP address showing ::1 8 37
    Bing Maps 1 23
    Can't see php code problem 12 30
    check the Referer HTTP header? 16 14
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now