Reputation: 780
I am using this code to display the total files in different directories, all good but for example cats=50 then dogs=30 and fish=10. how can I make something so that I get fish on top then dogs and finally cats..
function scan_dir($path){
$ite=new RecursiveDirectoryIterator($path);
$nbfiles=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
$nbfiles++;
}
if($nbfiles > 2) $nbfiles = $nbfiles - 2;
return $nbfiles;
}
$dogs = scan_dir('/home/dogs/');
$cats = scan_dir('/home/cats/');
$fish = scan_dir('/home/fish/');
<table border="1">
<tr><td><b>Animal</b></></></td><td><b>Total Files</b></></></td></tr>
<tr><td>Dogs</td><td><?=$dogs?></td></tr>
<tr><td>Cats</td><td><?=$cats?></td></tr>
<tr><td>Fish</td><td><?=$fish?></td></tr>
</table>
Upvotes: 0
Views: 59
Reputation: 1675
$array = array($dogs => 'dogs', $cats => 'cats', $fish => 'fish');
ksort($array); //sorts array the way you want
This should work.
Upvotes: 1
Reputation: 5754
<?php
function scan_dir($path){
$count = 0;
$it = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
foreach (new RecursiveIteratorIterator($it) as $dummy) {
++$count;
}
return $count;
}
$animals = array(
'Dogs' => '/home/dogs/',
'Cats' => '/home/cats/',
'Fish' => '/home/fish/',
);
foreach ($animals as $animal => $path) {
$animals[$animal] = scan_dir($path);
}
arsort($animals);
?>
<table border="1">
<tr><td><b>Animal</b></td><td><b>Total Files</b></td></tr>
<?php foreach ($animals as $animal => $count): ?>
<tr><td><?=$animal?></td><td><?=$count?></td></tr>
<?php endforeach; ?>
</table>
Upvotes: 0
Reputation: 9190
Quick and dirty:
$dogs = scan_dir('/home/dogs/');
$cats = scan_dir('/home/cats/');
$fish = scan_dir('/home/fish/');
$arr = array("Dogs" => $dogs, "Cats" => $cats, "Fish" => $fish);
asort($arr);
echo '<table border="1">';
echo "<tr><td><b>Animal</b></></></td><td><b>Total Files</b></></></td></tr>";
foreach ($arr as $key=>$value) {
echo "<tr><td>$key</td><td>$value</td></tr>";
}
echo "</table>";
With a working example.
Upvotes: 3