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 Quote 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 />"; ?> Quote 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 Quote 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 Quote 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. ? Quote 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] Quote 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. Quote 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! Quote 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. Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/#findComment-663473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.