Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:43:26+00:00 2026-06-06T21:43:26+00:00

I have the following code that I am using to randomly display PHP widgets

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:43:29+00:00Added an answer on June 6, 2026 at 9:43 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that sends a message using the mail() function. Everything
I have the following code that I am using to send a message from
I have the following code that extracts urls from a given page using jsoup.
I have the following code that fetches some data from a php script. I'm
I have the following code that works without using throw however when I use
I am using Lazarus v0.9.30 (32 bit compiler). I have the following code that
I have the following block of jQuery code that I'm using to copy some
I have a combobox that is bound to an enum using the following code:
I'm using SDL with FASM, and have code that's minimally like the following: format
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.