DaveLinger Posted May 13, 2009 Share Posted May 13, 2009 So what I'm trying to do is scan a directory "maps" for subdirectories (which will be used as categories). At the top of the page, I want to list the "categories", with anchor links/bookmark links (#these) to the part of the page (below) that lists that category's contents. I've got it all working EXCEPT that it only lists the contents of the FIRST category, then stops. echo "<div class=\"box\"> <div class=\"box1\"><h2>Map Categories</h2></div> <div class=\"box2\">"; $dirpath = "games/{$gameid}/maps/"; $dir = opendir($dirpath); while (($filename = readdir($dir)) !== false){ if($filename != "" && $filename != "." && $filename != ".."){ if(strpos($filename, ".") === false){ echo "<a href=\"#{$filename}\">{$filename}</a> "; } } } echo "</div></div><br />"; $dirpath = "games/{$gameid}/maps/"; $dir = opendir($dirpath); while (($filename = readdir($dir)) !== false){ if($filename != "" && $filename != "." && $filename != ".."){ if(strpos($filename, ".") === false){ echo "<div class=\"box\"> <div class=\"box1\"><h2>{$filename}</h2><a name=\"{$filename}\"></a></div> <div class=\"box2\"> <ul>"; $dirpath = "games/{$gameid}/maps/{$filename}/"; $dir = opendir($dirpath); while (($filename1 = readdir($dir)) !== false){ if($filename1 != "" && $filename1 != "." && $filename1 != ".."){ if(strpos($filename1, ".") === false){ echo "<li><a href=\"{$filename1}\">{$filename1}</a></li>"; } } } echo "</ul> </div></div>"; } } } Example output: <div class="box1"><h2>Map Categories</h2></div> <div class="box2"><a href="#World 8">World 8</a> <a href="#World 2">World 2</a> <a href="#Miscellaneous">Miscellaneous</a> <a href="#World 1">World 1</a> </div></div><br /><div class="box"> <div class="box1"><h2>World 8</h2><a name="World 8"></a></div> <div class="box2"> <ul></ul> </div></div> Link to comment https://forums.phpfreaks.com/topic/158007-solved-list-all-subdirectories-in-a-folder-then-list-the-contents-of-each-one-separate/ Share on other sites More sharing options...
mikr Posted May 13, 2009 Share Posted May 13, 2009 While you are looping through $dir (while (($filename1 = readdir($dir)) !== false) ), you cannot reassign $dir. The inner loop needs a new variable ($dir2?). You are, in essence, stepping on your own variables. Link to comment https://forums.phpfreaks.com/topic/158007-solved-list-all-subdirectories-in-a-folder-then-list-the-contents-of-each-one-separate/#findComment-833527 Share on other sites More sharing options...
DaveLinger Posted May 13, 2009 Author Share Posted May 13, 2009 Ah, indeed. Good call. That fixed that. But apparently it's also not listing the files in each category, as if the folder is empty. My new code: echo "<div class=\"box\"> <div class=\"box1\"><h2>Map Categories</h2></div> <div class=\"box2\">"; $dirpath = "games/{$gameid}/maps/"; $dir = opendir($dirpath); while (($filename = readdir($dir)) !== false){ if($filename != "" && $filename != "." && $filename != ".."){ if(strpos($filename, ".") === false){ echo "<a href=\"#{$filename}\">{$filename}</a> "; } } } echo "</div></div><br />"; $dirpath = "games/{$gameid}/maps/"; $dir = opendir($dirpath); while (($filename = readdir($dir)) !== false){ if($filename != "" && $filename != "." && $filename != ".."){ if(strpos($filename, ".") === false){ echo "<div class=\"box\"> <div class=\"box1\"><h2>{$filename}</h2><a name=\"{$filename}\"></a></div> <div class=\"box2\"> <ul>"; $dirpath1 = "games/{$gameid}/maps/{$filename}/"; $dir1 = opendir($dirpath1); while (($filename1 = readdir($dir1)) !== false){ if($filename1 != "" && $filename1 != "." && $filename1 != ".."){ if(strpos($filename1, ".") === false){ echo "<li><a href=\"{$filename1}\">{$filename1}</a></li>"; } } } echo "</ul> </div></div><br />"; } } } Link to comment https://forums.phpfreaks.com/topic/158007-solved-list-all-subdirectories-in-a-folder-then-list-the-contents-of-each-one-separate/#findComment-833638 Share on other sites More sharing options...
DaveLinger Posted May 13, 2009 Author Share Posted May 13, 2009 Whoops, nevermind, I found the error. It was looking for directories again, instead of files. (was looking for files without a "." in their name) Link to comment https://forums.phpfreaks.com/topic/158007-solved-list-all-subdirectories-in-a-folder-then-list-the-contents-of-each-one-separate/#findComment-833641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.