0

I have the following code that I am using to randomly display PHP widgets from a folder:

<?php 
function random_widget($dir = 'wp-content/themes/zonza/elements')
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}
?>

<?php include random_widget();?>
<?php include random_widget();?>
<?php include random_widget();?>

random_widget(); outputs a URL, which I then use in the include function to display the widget.

The code randomly chooses between 6 php files and displays one randomly. I include it 3 times to get 3 widgets. However, I get the same widget displayed more than once sometimes.

What can I do to modify the code to prevent this from happening?

3 Answers 3

2

Try this:

<?php 
function random_widget($dir = 'wp-content/themes/zonza/elements')
{
    static $files = false;
    if(!$files) $files=glob($dir . '/*.*');
    $key = array_rand($files);
    $file=$files[$key];
    unset($files[$key]);
    return $file;
}
?>

It works by removing the file returned from $files, and maintaining $files over multiple function calls (it only globs() on the first time you call the function)

Sign up to request clarification or add additional context in comments.

2 Comments

This code reports an error. Parse error: syntax error, unexpected '(', expecting ',' or ';' on line 4
updated the code, I forgot that static variables in PHP cannot be initialized with an expression. Should work fine now.
0

Declare files at the beginning of the page $files = glob($dir . '/*.*'); In random_widget, do this unset($files[$file]) after you pull the value.

2 Comments

don't forget to pull $files into the namespace of the function with global $files; if you try this method
Yes, although a static variable actually would be better.
0

array_rand takes a number $num_req as an optional second parameter which specifies the number of entries you want to pick. So add this parameter to random_widget, pass it to array_rand to get an array of keys instead of a single key, return the array of files, and then iterate over this array to include the widgets (instead of calling random_widget three times).

<?php 
function random_widget($num_req, $dir = 'wp-content/themes/zonza/elements')
{
    $files = glob($dir . '/*.*');
    $keys = array_rand($files, $num_req);

    $chosen = array();

    foreach($keys as $key) {
        $chosen[] = $files[$key];
    }

    return $chosen;
}

$widgets = random_widget(3);
foreach($widgets as $widget) {
    include $widget;
}

?>

An advantage of this solution over the ones proposed in the other answers is that it is stateless: you can reuse the function in different contexts as much as you want.

Source: http://php.net/manual/en/function.array-rand.php

1 Comment

I like the idea of one function call to get as many widgets as you want - however this really only works well if all of the widgets are together in one spot. My solution allows for random_widget() to be called from anywhere without much fuss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.