Variable gets lost (maybe because of ajax?)

tarm

New Member
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:\[code\]<?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=http://stackoverflow.com/"".$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=http://stackoverflow.com/"".$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); } }\[/code\]
 
Back
Top