Re: Menu from Directory

From: Date: Fri, 04 Apr 2003 15:31:57 +0000
Subject: Re: Menu from Directory
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-142356@lists.php.net to get a copy of this message
Ben Whitehead wrote:
Having looked at it, it seems to be along the lines of what I was thinking of... I would be very grateful if you could post the code, since I do think it will be very useful. The main difference I was thinking of was to have a menu in the form of a page of links, rather than a drop-down menu, but having seen that, I would much prefer the drop-down menu approach!!! Thanks very much, Ben.
Sorry I didn't get to this in a while -- had to fix my filters :) Anyhow, the functions that I used to create my drop down box : function getSubDir($rootdirpath) { if ($dir = @opendir($rootdirpath)) { $array[] = $rootdirpath; while (($file = readdir($dir)) !== false) {
	      if (is_dir($rootdirpath."/".$file)
                  && $file != "." && $file != "..")
	      {
$array = array_merge($array,getSubDir($rootdirpath."/".$file)); } } closedir($dir); } return $array; } function getFiles($dirname) { $handle=opendir($dirname); while ($file = readdir($handle)) { if($file=='.'||$file=='..') continue; else $result_array[]=$file; } closedir($handle); return $result_array; } The first function returns all the subdirs in a directory (given as $rootdirpath). The second functions returns an array of all files in a directory. Simply combine these two functions to get a listing of all files in subdirectories : $subdirs = getSubDir("/path/to/main"); //no trailing / array_shift($subdirs); //gets rid of the parent dir $count = 0; while (list(,$val) = each($subdirs)) { $files[substr(strrchr($val,"/"),1,strlen(strrchr($val,"/")))] = getFiles($val); $count += count(getFiles($val)); } This while loop gets a list of all the files and counts the total number of files in the subdirs. The list of files is in an array called $files. Now, to build the drop down : while(list($key,$val) = each($files)) { echo "<option value=\"NULL\" >Section $key :</option>\n"; while(list(,$name) = each ($val)) { echo "<option value=\"$key/$name\" > -=> "; echo substr($name,0,strlen($name)-4); echo "</option>\n"; } echo "<option value=\"NULL\" ></option>\n"; // space } I hope this helps you out. Let me know if you can't understand anything :) -- Burhan Khalid phplist[at]meidomus[dot]com

« previous php.general (#142356) next »