hellonoko Posted October 12, 2008 Share Posted October 12, 2008 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 More sharing options...
zq29 Posted October 12, 2008 Share Posted October 12, 2008 <?php foreach($files as $f) echo "$f[name]: $f[count]<br />"; ?> Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663438 Share on other sites More sharing options...
hellonoko Posted October 12, 2008 Author Share Posted October 12, 2008 Returns r: r r I have tried similar for each loops with no success. Am I entering my data into my array correctly? In the code above that part where I echo each piece of data as it is entered it displays correctly. -ian Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663441 Share on other sites More sharing options...
kenrbnsn Posted October 12, 2008 Share Posted October 12, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663457 Share on other sites More sharing options...
hellonoko Posted October 12, 2008 Author Share Posted October 12, 2008 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. ? Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663464 Share on other sites More sharing options...
wildteen88 Posted October 12, 2008 Share Posted October 12, 2008 $files['$filename'] should be $files[$filename] Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663465 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 Yeah, you weren't supposed to clean up those quotes. Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663466 Share on other sites More sharing options...
hellonoko Posted October 12, 2008 Author Share Posted October 12, 2008 DarkWater: What? No it would/did fail. Wildteen88: WORKS! Thanks so much. Single quotes = don't parse right? Ken: thanks also! Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663470 Share on other sites More sharing options...
DarkWater Posted October 12, 2008 Share Posted October 12, 2008 I meant the quotes on $files[$filename], sorry. Should have been more specific. Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663471 Share on other sites More sharing options...
hellonoko Posted October 12, 2008 Author Share Posted October 12, 2008 Haha thought you might have meant those. Thanks. Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663473 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.