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
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.