[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

randomizing link display order


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



0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 2
  • 2
1 Solution
 
ljw87505Commented:
Here's a function I've used before that I found:

<?php
  $links=array('Sample Link' => 'sample.html',
               'Example Link' => 'example.html',
               'Another Link' => 'another.html');
  $shuffled_links=shuffle_me($links);

  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;
}
?>
0
 
ljw87505Commented:
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.
0
 
designbaiCommented:
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>');

srand((float)microtime()*1000000);

shuffle($input);

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

hope this also helps.



0
 
St_Aug_Beach_BumAuthor Commented:

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

Chris
0
 
designbaiCommented:
Diablo84

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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now