Jump to content

[SOLVED] List all subdirectories in a folder, then list the contents of each one separate


DaveLinger

Recommended Posts

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>

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 />";

}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.