A

How to show Random Items out of a list

Picking up the scenario from my project NAP, that I faced was I needed to show some books with their respective links but I have to pick up only 4 out of the 5 books I had and the picking should be random on each page load. You might say that php random function is […]

Picking up the scenario from my project NAP, that I faced was I needed to show some books with their respective links but I have to pick up only 4 out of the 5 books I had and the picking should be random on each page load.

You might say that php random function is the answer to the problem but I have a few things to talk about.

Using PHP Random function to show 1 random item

If you don’t know how to use php random function to show different images every time with a link then here is how it is done. We will deal with the limitations as we proceed.

We have a list of links in an array $link and the images are renamed with a constant suffixed with the key of the array to which it holds the respective link.

For example :

$link[1] => image1.jpg
$link[2] => image2.jpg
$link[3] => image3.jpg
$link[4] => image4.jpg
$link[5] => image5.jpg

We pick up a random number between 1 & 5 by

<?php $n = rand(1,5); ?>

and then display it as

<a href="<?php echo $link[$n]; >"><img src="image<?php echo $n; ?>.jpg" alt="" /></a>

Showing 4 out of 5 items

This was the scenario in my project that I needed to show up 4 selections out of 5 options. The simple trick failed to show distinct items as everytime it selects a random number between 1 & 5 irrespective of whether it’s already selected or not. To avoid this either we store the number selected on each function call in an array and every time we check the selected number with the array. If the number was in the array then we recall the function till we get a new number. We store it and then proceed.

For example :

<?php
$arr[0] = 0;
for($i=0; $i<4; $i++)
{
$go = 0;
while(!$go)
{
$flag = 0;
$n = rand(1,5);
foreach($arr as $k => $v)
{
if($v == $n)
{
$flag = 1;
break;
}
}
if(!$flag)
{
$arr[$i] = $n;
$go = 1;
}
}
}
for($i=0; $i<4; $i++)
{
?>
<a href="<?php echo $link[$i]; ?>"><img src="image<?php echo $i; ?/>.jpg" alt="" /></a>
<?php
}
?>

Better Alternative

The algorithm would become heavy when a large number of selections have to be made as then every new selection would have to be compared with every element of array. In  my scenario I have to made selection of only 4 out of 5.

So instead of randomly selected the numbers of which items are to be displayed, i randomly selected the number of which item is not to be displayed. This was pretty much good in terms of performance.

My Code :

<?php
$kill = mt_rand(1,5);
for($i=1;$i<=5;$i++)
{
if ( $kill == $i )
continue;
?>
<a href="<?php echo $link[$i]; ?>"><img src="image<?php echo $i; ?/>.jpg" alt="" /></a>
<?php } ?>

I use mt_rand function instead of rand function because it is better in terms of performance.

That’s all for this tutorial. Hope you got what I wanted to say. Have any doubts? Feel free to ask in the comments. Make sure you check out the whole WordPress as CMS series.

3 responses to “How to show Random Items out of a list”

  1. […] Showing random items from a list […]

  2. Gabriel Collignon says:

    Hi I want to show a directory of 5 places in my site, something simple, like this, but for 5 businesses:

    Business Name

    Anaheim Best Roofing provides the best roofing services through out the Anaheim, Ca. area. If you would like to learn more about our services,
    click here or go to our website.

    Phone:(213) 973-1537Webiste: http://www.something.com
    Address

    But I would like that everytime that the page reloads that the listings appear in a different position.
    You have written the only post I could find that has something like this (i guess)
    I would really appreciate if you can help me with this…. thanks