开发者

Variable gets lost (maybe because of ajax?)

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 wh开发者_开发技巧ich 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);    
        }
    }


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜