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 1059639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:12:15+00:00 2026-05-16T18:12:15+00:00

I have a fairly simple script but something is going wrong and I can’t

  • 0

I have a fairly simple script but something is going wrong and I can’t seem to figure it out. My script collects files from a folder, selects a random file. The random selected file is checked against another array to check if it’s not in that array. That checked array is in fact a textstream put into an array by exploding it on the #-char.

The script is used to play a random sound from a collection, if the sound is not yet in a textfile it can be played. If the sound is allready in the textfile another random file must be selected (and so on.. recursion). The strange thing is that in the recursive function it all goes well but when I display the outcome from that function sometimes it just shows blank while the function for sure is returning a file to be played (and stored into the textfile).

The PHP script is called via the XMLHTTPObject. Could this be a reason why the variable gets lost? Or am I missing something else in my script?

The script:

<?php
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

    $soundList      = get_folder_entries("playlist", array("wma", "wav", "mp3"));
    $playedFile     = "played.txt";
    $playedContents = file_get_contents($playedFile);
    $playedSounds   = explode("#", $playedContents);

    // if all files are played just select a new file and reset played.txt  
    if(count($playedSounds) == count($soundList))
    {
        $fileHandle = fopen($playedFile, "w");  
        $file       = $soundList[mt_rand(0, count($soundList) - 1)];

        fwrite($fileHandle, $file."#");
        fclose($fileHandle);

        echo "<input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/>";
    }
    // pick a random file which has not yet been played
    else
    {
        $file       = pick_random_file($soundList, $playedSounds);
        $fileHandle = fopen($playedFile, "w");

        fwrite($fileHandle, $playedContents.$file."#");
        fclose($fileHandle);

        echo 'INPUT = '.$file;

        echo "<input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/>";
    }

    // some debug/output info
    echo "<table width=\"100%\">";
    echo "<tr>";
    echo "<td class=\"header\"><h2>[Array] Playlist</h2></td>";
    echo "<td class=\"header\"><h2>[Array] Played</h2></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td><pre>";
    print_r($soundList);
    echo "</pre></td>";
    echo "<td><pre>";
    print_r($playedSounds);
    echo "</td>";
    echo "</tr>";
    echo "</table>";    

    // collect the files from a folder with a specific extension
    function get_folder_entries($dir, array $extensions)
    {
        $fileList = array();

        if($fileHandle  = opendir($dir))
        {
            while(false !== ($file = readdir($fileHandle)))
            {
                if($file != "." && $file != ".." && in_array(end(explode(".", $file)), $extensions))
                {
                    array_push($fileList, $file);
                }
            }
        }

        return $fileList;
    }

    // recursive function to pick a random sound
    function pick_random_file(array $soundList, array $playedSounds)
    {
        $sound = $soundList[mt_rand(0, count($soundList) - 1)];

        if(!in_array($sound, $playedSounds))
        {
            return $sound;  
        }
        else
        {
            pick_random_file($soundList, $playedSounds);    
        }
    }
  • 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-05-16T18:12:16+00:00Added an answer on May 16, 2026 at 6:12 pm

    A rather blind guess:

    Change:

    else
    {
        pick_random_file($soundList, $playedSounds);    
    }
    

    to:

    else
    {
        return pick_random_file($soundList, $playedSounds);    
    }
    

    and see if it works.

    Btw: Using recursion might not be the best way to filter out all the already played sounds (if all are playing you run into infinite depth and so on and it doesn’t seem all the necessary.

    I’d rather go with a while(!in_array.... or select a random key from array_diff($playedSounds, $allSounds)

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

Sidebar

Related Questions

I am trying to do something I think should be fairly simple, but I
I have used InfoBox successfully in a fairly simple application but I am struggling
I have a fairly simple web2py form with a few validators like, IS_FLOAT_IN_RANGE(...) on
I have a fairly simple question, however the answer seems to elude me. If
I have a fairly simple query: SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id
I have a fairly simple block of code. Sub Run(Name) on error resume next
I have a fairly simple python loop that calls a few functions, and writes
I have a fairly simple cell in a table with an inline style: <td
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple Linq query (simplified code): dim x = From Product

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.