digitalgod Posted July 3, 2006 Share Posted July 3, 2006 hey guys,I'm trying to get a script to read the contents of a directory, store the file names in an array and then displaying those files. So far I got the 2 first parts to work like a charm but I'm having trouble displaying the elements of the array.Here's what I got so far.[code]//this part works perfectly$dir = "img/events/" . $name . "/thumbnails/";$aNames = array();if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file_names = readdir($dh)) !== false) { if ($file_names != "." && $file_names != "..") { array_push($aNames,$file_names); } } closedir($dh); }}//this part doesn'tfor ($i ='0'; $i <= $numFiles; $i++) { echo array_values($aNames); echo '<tr><td> <img src="' . $dir . '' . $aNames[i] . '" alt="' . $aNames[i] .'" /> </td></tr>';}[/code]the second part only displays the path but the array returns nothing, If echo just the array for instance echo $aNames[0]; it does return a value. Any ideas what I can do? Quote Link to comment https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/ Share on other sites More sharing options...
trq Posted July 3, 2006 Share Posted July 3, 2006 You need to initialy set $i to 0 not '0' in your for(). Also... where do you declare $numFiles? Also, this $aNames[i] should be $aNames[$i].Sorry, but the second part of the code is a bit of a mess.To be honest, looping through a directory and then again looping through an array is a waste of resources. If you really dont need to, why use more than one loop? Quote Link to comment https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/#findComment-52266 Share on other sites More sharing options...
digitalgod Posted July 3, 2006 Author Share Posted July 3, 2006 so what would you suggest ;)$numFiles is declared right under the first part, just forgot to copy paste it...[code]$numFiles = count($aNames);[/code]and the first echo was just a test, forgot to use the [] :P Quote Link to comment https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/#findComment-52267 Share on other sites More sharing options...
litebearer Posted July 3, 2006 Share Posted July 3, 2006 Is this close to what you are looking to do....http://www.nstoia.com/toh/technical/listdir/index.php(click on the example to see an... well example)Lite... Quote Link to comment https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/#findComment-52272 Share on other sites More sharing options...
digitalgod Posted July 7, 2006 Author Share Posted July 7, 2006 thanks for the link llitebearer works perfectly now.I got another question though, does anyone know how I can add a delete button so that when it gets clicked the pic gets deleted? This is what I tried so far[code]<? echo '<script>function del(){ var delimgfinal = confirm("Are you sure you want to delete, ' . $aNames[$i] . '?") if(delimgfinal == true) { unlink($path.$aNames[$i]); unlink($dir.$aNames[$i]); } else { }}</script>'?>//......<? for ($i =0; $i <= $numFiles--; $i++) {echo '<tr><td> <a href="' . $path . ''. $aNames[$i].'" target="_blank"> <img src="' . $dir . '' . $aNames[$i] . '" alt="' . $aNames[$i] .'" /></a> </td></tr><input type="button" onclick="del()" value="Delete Image" />'; } ?>[/code]but that's not working.... any ideas why it's not working? Quote Link to comment https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/#findComment-54501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.