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>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.