Jump to content

[SOLVED] displaying array contents


hellonoko

Recommended Posts

My script below enters the name of a directory and the count of its contents into an array.

 

I think I captures the information correctly.

 

However I am not sure how to get it to echo it back out.

 

<?php

$dir = "/home2/sharingi/public_html/subdomains";

$dh = opendir($dir);


while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{

		$files['name'] = $filename;

		$files['count'] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));	

		echo $files['name'];
		echo '<br>';
		echo $files['count'];
		echo '<br>';

	}
}

 

Basically I am trying display a sorted list of the directories and their count.

 

Folder1 10

Folder2 6

Folder3 2

 

Thanks,

ian

Link to comment
https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/
Share on other sites

You're not storing the information correctly. The way you're doing it, you're overwriting the same entry every time.

 

Try it this way:

<?php
$dir = "/home2/sharingi/public_html/subdomains";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{
		$files[$filename] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));	
	}
}
        ksort($files);
        foreach($files as $file => $count)
               echo $file . ': ' . $count . "<br>\n";
?>

 

Ken

After I cleaned up some quotes...

 

<?php
   $dir = "/home2/sharingi/public_html/subdomains";
   $dh = opendir($dir);
   while (false !== ($filename = readdir($dh)))
   {
      if ($filename !== "." && $filename !== "..")
      {
         $files['$filename'] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));   
      }
   }
        ksort($files);
        
	foreach($files as $file => $count) echo $file . ":"  . $count . "<br>";
?>

 

Returns

 

 

 

$filename:2

 

 

 

Thats all.

?

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.