Jump to content

[SOLVED] prioritize foreach display


legohead6

Recommended Posts

well, i have a code that manages folders and files, it works perfectly, but the folders appear wherever they lie, i would like the folders to appear at the top, so my question, how do i tell the foreach which to display first... heres the snip of code

 

$dir = "members/$user";
$dh  = opendir($dir);

while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}
$re = array_search('..', $files);
$rem = array_search('.', $files);
unset($files[$rem], $files[$re]);
$totalp = count($files);
echo "<div align=center><table><tr><td colspan=2>$uploader</td></tr><tr><td colspan=2>$foldermaker<tr><td></td></tr><tr><td colspan=2><p align=center><u>Current Folders/Files</u></p></td></tr>";
foreach($files as $id => $file){
$folder=explode('.',"$file");
if(!isset($folder[1])){
$doc=base64_encode($file);
echo "<tr><td><p align=center><a href=doc.php?f=$doc><u><b>$file</b></u></a></p></td></tr>";
}else{
$file1 = str_replace(" ", "_", "$file");
$file2 = str_replace(".", ".", "$file1");
$doc=base64_encode($file);
echo "<tr><td><p align=center><a href=doc.php?f=$doc>$file2</p></td><td>Select Folder</td></tr>";
$p++;
}
}
echo "</table></div>";

Link to comment
https://forums.phpfreaks.com/topic/87330-solved-prioritize-foreach-display/
Share on other sites

that'll give you two arrays... one of $dirs... one of $files

while (false !== ($filename = readdir($dh))) {
if(isdir($filename)) $dirs[] = $filename;
else $files[] = $filename;
}

 

thenyou just

 

$files=array_merge($dirs,$files);

 

and your array has dirs at the top, files at the bottom  :D

This will give you the directory list

<?php
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			if (is_dir($directory. "/" . $file)) {
				if($recursive) {
					$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
				}
				$file = $directory . "/" . $file;
				$array_items[] = preg_replace("/\/\//si", "/", $file);
			}
		}
	} 
	closedir($handle);
}
return $array_items;
} ?>

Regards

SHarad

This will give you the directory list

<?php
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			if (is_dir($directory. "/" . $file)) {
				if($recursive) {
					$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
				}
				$file = $directory . "/" . $file;
				$array_items[] = preg_replace("/\/\//si", "/", $file);
			}
		}
	} 
	closedir($handle);
}
return $array_items;
} ?>

Regards

SHarad

 

just as a note... glob() does the same job FAR faster :D

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.