变量会丢失(可能是因为ajax?)

时间:2010-09-07 08:08:00

标签: php

我有一个相当简单的脚本,但出了点问题,我似乎无法弄明白。我的脚本从文件夹中收集文件,选择随机文件。将针对另一个数组检查随机选择的文件,以检查它是否不在该数组中。那个checked数组实际上是一个文本流,通过在#-char上展开它来放入一个数组。

该脚本用于播放集合中的随机声音,如果声音尚未出现在文本文件中,则可播放该声音。如果声音已经在文本文件中,则必须选择另一个随机文件(依此类推..递归)。奇怪的是,在递归函数中它一切顺利,但是当我显示该函数的结果时,有时它只显示空白,而函数肯定是返回要播放的文件(并存储到文本文件中)。

通过XMLHTTPObject调用PHP脚本。这可能是变量丢失的原因吗?或者我在脚本中遗漏了其他内容?

脚本:

<?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 个答案:

答案 0 :(得分:3)

一个相当盲目的猜测:

变化:

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

为:

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

看看它是否有效。

顺便说一句:使用递归可能不是过滤掉所有已播放声音的最佳方法(如果所有人都在玩你进入无限深度等等,而且看起来并不是全部必要。

我宁愿选择while(!in_array....或从array_diff( $playedSounds, $allSounds)中选择随机密钥