JREAM Posted January 3, 2009 Share Posted January 3, 2009 hey i keep crashing my browser with this and it floods it like 100000 times, but i only got like 40 m3ps <script type="text/javascript" src="public/apps/mp3_player/swfobject.js"></script> <?php function list_mp3s () { $directory = 'public/mp3s/'; $appdir = 'public/apps/mp3_player/'; $results = array(); // I dont see why i have this ehre anymore $handler = opendir($directory); $i = 0; // this is to count unique instances while ($file = readdir($handler)) { if ($file != '.' && $file != '..') // look for only filenames { $data .= '<script type="text/javascript">'; $data .= 'var so = new SWFObject("public/apps/mp3_player/playerMini.swf", "mymovie", "75", "30", "7", "#FFFFFF");'; $data .= 'so.addVariable("autoPlay", "no");'; $data .= 'so.addVariable("soundPath", "'.$directory . $file . '");'; $data .= 'so.write("flashPlayer'.$i.'");'; $data .= '</script>'; $data .= '<div id="flashPlayer'.$i.'">'; $data .= 'This text will be replaced by the flash music player.'; $data .= '</div>'; $i++; // increase unique instances echo $data; // return the stuff. } } closedir($handler); } // do it list_mp3s(); ?> Link to comment https://forums.phpfreaks.com/topic/139365-why-does-my-loop-never-end/ Share on other sites More sharing options...
leequalls Posted January 3, 2009 Share Posted January 3, 2009 are the file names listed in a database? I would recommend using a for loop Link to comment https://forums.phpfreaks.com/topic/139365-why-does-my-loop-never-end/#findComment-728925 Share on other sites More sharing options...
DarkSuperHero Posted January 3, 2009 Share Posted January 3, 2009 php.net recommends reading a dir like this: <?php // Note that !== did not exist until 4.0.0-RC2 if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { echo "$file\n"; } /* This is the WRONG way to loop over the directory. */ while ($file = readdir($handle)) { echo "$file\n"; } closedir($handle); } ?> http://us3.php.net/readdir I believe your just not reading the dir the correct way so your loop never ends... Link to comment https://forums.phpfreaks.com/topic/139365-why-does-my-loop-never-end/#findComment-728933 Share on other sites More sharing options...
kenrbnsn Posted January 3, 2009 Share Posted January 3, 2009 I would recommend you look at the glob functions. It makes getting the contents of a directory much easier (IMHO). <?php function list_mp3s () { $directory = 'public/mp3s/'; $appdir = 'public/apps/mp3_player/'; $results = glob($directory . '*.mp3'); $i = 0; foreach($results as $file) { $data = ''; $data .= '<script type="text/javascript">'; $data .= 'var so = new SWFObject("public/apps/mp3_player/playerMini.swf", "mymovie", "75", "30", "7", "#FFFFFF");'; $data .= 'so.addVariable("autoPlay", "no");'; $data .= 'so.addVariable("soundPath", "'. $file . '");'; $data .= 'so.write("flashPlayer'.$i.'");'; $data .= '</script>'; $data .= '<div id="flashPlayer'.$i.'">'; $data .= 'This text will be replaced by the flash music player.'; $data .= '</div>'; $i++; // increase unique instances echo $data; // return the stuff. } } } // do it list_mp3s(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/139365-why-does-my-loop-never-end/#findComment-728949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.