Guest MrLeN Posted August 25, 2015 Share Posted August 25, 2015 (edited) I have a directory that has files in it like this: 1.txt 2.txt 3.txt etc and inside each file is a word: subscribed unsubscribed ie: So inside 1.txt is subscribed Then inside 2.txt is unsubscribed etc I want to read the directory, get the files and display some code like this: There are 20 members (ie: 20 text files that say either subscribed or unsubscribed). 15 of the members are not subscried. 5 of the members are subscribed. I've been working on this for hours.. here's my best effort. The code doesn't result in errors, but it is spitting out the wrong information. So I have come here to ask for help if (isset($_GET['id'])) { $current_user = wp_get_current_user(); $dir = '/home/promoter/public_html/referrals/' . $_GET['id']; if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "role.txt") { //echo "$entry\n"; $dir = '/home/promoter/public_html/referrals/' . $_GET['id'] . "/" . $entry; $result = file_get_contents($dir ); echo $result . "<br />"; $str = $result; echo substr_count($str,'subscribe') . "<br />"; } } closedir($handle); } } Edited August 25, 2015 by MrLeN Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted August 25, 2015 Share Posted August 25, 2015 Note each file 1.txt, 2.txt, etc represents a member (whether they are subscribed or not) -- in case that wasn't clear in my explanation Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 25, 2015 Share Posted August 25, 2015 (edited) http://php.net/glob EDIT: Nevermind, I see you want to count the content within files and not the files themselves. EDIT Again: In pseudo code, this should be what you want, ya? numSubscribed = 0 numUnsubscribed = 0 while loop through files content = file_get_contents(file) if (content == 'subscribed') numSubscribed++ else if (content == 'unsubscribed') numUnsubscribed++ endwhile Edited August 25, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 25, 2015 Solution Share Posted August 25, 2015 (edited) You will need to use separate counters if (isset($_GET['id'])) { // initialize counters to zero $SubscribedMembers = 0; $UnSubscribedMembers = 0; $current_user = wp_get_current_user(); $dir = '/home/promoter/public_html/referrals/' . basename($_GET['id']); if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && $entry != "role.txt") { //echo "$entry\n"; $path = "$dir/$entry"; $result = file_get_contents($path); switch(trim($result)) { // increment subscribed couter, if the contents of the file is "subscribed" case 'subscribed': $SubscribedMembers++; break; // increment unsubscribed couter, if the contents of the file is "unsubscribed" case 'unsubscribed': default: $UnSubscribedMembers++; break; } } } closedir($handle); } // calculate total members $totalMembers = $SubscribedMembers + $UnSubscribedMembers; // output results echo " There are $totalMembers members.<br /> $UnSubscribedMembers of the members are not subscried.<br /> $SubscribedMembers of the members are subscribed."; } However storing whether each member is subscribed or not in separate text files is very efficient. It will be better if you used a database of some kind, be it a csv/xml file or SQL database. Edited August 25, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Guest MrLeN Posted August 25, 2015 Share Posted August 25, 2015 That worked like a charm! Thanks heaps Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 25, 2015 Share Posted August 25, 2015 However storing whether each member is subscribed or not in separate text files is very efficient. It will be better if you used a database of some kind, be it a csv/xml file or SQL database. Ch0cu3r meant to say "storing whether each member is subscribed or not in separate text files is very inefficient." Quote Link to comment 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.