Jump to content

Counting Files


Guest MrLeN
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by MrLeN
Link to comment
Share on other sites

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 by scootstah
Link to comment
Share on other sites

  • Solution

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 by Ch0cu3r
Link to comment
Share on other sites

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."

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.