Jump to content

[SOLVED] adding up numbers from each text files.


ted_chou12

Recommended Posts

I have a number in each of the text files, and what I want to do, is to add them up,
these text files are all in one directory, but the amount of text files are not fixed, by not fixed, I mean they are keeping on increasing.
However, my server doesn't allow me to use the glob() function, so i am having some trouble with this... :'(
Link to comment
Share on other sites

Try something like this should work in it's simplest form, although it's not tested.  It assumes that the only thing in the file is the number that you mentioned.  If it's not then you'll want to perform a regular expression on the information returned.

[code]<?php
// Directory path
$dir = "/path/to/all_of_your/textfiles/";

// Open the directory
$dh = opendir($dir);
while (($file = readdir($dh) !== false){
  $fullfile = $dir . $file;
  $subtotals[] = file_get_contents($fullfile);
}

// Add them all together
$total = 0;
foreach ($subtotals as $st){
  $total = $total + $st;
}

// Echo the total
echo "$total\n";
?>[/code]

Regards
Huggie
Link to comment
Share on other sites

First question: is the value in each of the files the [b]only[/b] text in the file? If so, and I know this may be questioning application design a tad, but why not have one file that holds all the values and simply add a new line to that file whenever you need something done?

Another question: are the values always integers, or can they be floats? If integers, then you could use Huggie's example and simply add them up as you go like this, too:
[code]
<?php
// Open the directory
$dh = opendir($dir);
$total = 0;
if (!$dh) {
  // couldn't open directory
} else {
  while (($file = readdir($dh) !== false){
    $fullfile = $dir . $file;
    $total += (int)trim(file_get_contents($fullfile));
  }
  closedir($dh);
}
?>
[/code]
Link to comment
Share on other sites

[quote author=HuggieBear link=topic=119094.msg487331#msg487331 date=1166453978]
No problem, I also forgot to mention that it assumes that you only have the 'number' files in the directory.  Other files will cause problems.
[/quote]

With that in mind, you DO have to account for '.' and '..' as every directory will have those. So, you'd want to put a simple check in your while loop something like this:
[code]
<?php
while (($file = readdir($dh) !== false){
  if ($file != '.' && $file != '..') {
    $fullfile = $dir . $file;
    $total += (int)trim(file_get_contents($fullfile));
  }
}
?>
[/code]
Link to comment
Share on other sites

i cant just have a file because each number represents a figure for the counter. (they act as a counter text file)
each file for one page.
so if they are all in new lines and added to previous, it would cause difficulties in reading them.
added: yeah, and thanks for reminding me about the '.' and '..'.
unfortunately, I also have some php files in the directory, however, "all" txt extension files in the directory are the ones i wanted to count.
what I have to add is just to tell the script to only count txt files.
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=119094.msg487337#msg487337 date=1166454437]
i cant just have a file because each number represents a figure for the counter. (they act as a counter text file)
each file for one page.
so if they are all in new lines and added to previous, it would cause difficulties in reading them.
added: yeah, and thanks for reminding me about the '.' and '..'
[/quote]

So, every time you add a page to your site, you add a new text file? With that being said, why not run a true flat file DB to handle it? For instance, instead of simply having an integer stored, you could have basically a two column table in one flat file. One column would store the page name (or reference of some sort) and the other column would have your count. That way, you could always get any info you wanted simply by accessing the one file.

Just a thought. Definitely a preference thing at this level, but it's something to consider ;)
Link to comment
Share on other sites

hey, thanks for the suggestion, yeah, maybe I should start to learn using db and mysql those that are complement to php. In fact, I tried once, using mysql login, following online instructions, but I cant get them right, and it was very confusing to me, so I only used php and write my data into text files, often times, i do find them very insecure, but I had some bad experiences with db and sql, so I wish to stick with what I do now.
Thanks for your suggestions anyway
Ted. :D
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=119094.msg487351#msg487351 date=1166455283]
hey, thanks for the suggestion, yeah, maybe I should start to learn using db and mysql those that are complement to php. In fact, I tried once, using mysql login, following online instructions, but I cant get them right, and it was very confusing to me, so I only used php and write my data into text files, often times, i do find them very insecure, but I had some bad experiences with db and sql, so I wish to stick with what I do now.
Thanks for your suggestions anyway
Ted. :D
[/quote]

We weren't suggesting using SQL at all. We're suggesting simply changing the method in which your information is stored in a flat file (text file). Instead of using a separate file for all your counts, you would store all your information within a single file (CVS or the like). So, I may choose to store my counts in a file that is pipe delimited, and it would look something like this:
[code]
index.php|36
about.php|8
contact.php|12
[/code]

Each file of your site would be stored on a line with the actual counts for that file. Then, you'd simply grab the count for the individual files as you need them, or else you could total up the counts just as easily. In addition, as you add pages to your site, you simply add a new line to the file, and you can start counting that one, too.

Hope this makes a little more sense as to what we're suggesting.
Link to comment
Share on other sites

hey, by the way, I couldnt get it to work, it always shows up 0 , however, I do have txt files with single digits stored in the specified directory, as looking trough the script, i find that
foreach ($subtotals as $st){
  $total = $total + $st;
}
is put after where you set the $total = 0, and you have not set $subtotals before this, would it work like this? by that I mean dont you need to set $subtotal =(equal) to  something before functions including it(the subtotal string)
Ted
Link to comment
Share on other sites

I've tested that code now and it works fine.  It's adding them up correctly.

You don't need to define the array first, the foreach loop should be happy with assigning each new value to [code=php:0]$subtotals[][/code]

You do have the [code=php:0]$total = 0;[/code] outside the loop don't you?

Regards
Huggie
Link to comment
Share on other sites

By loop, you mean the while function, (correct me if I am wrong), but i really do have it outside, here, i will let you see how it looks like:
[code]
<?php
// Directory path
$dir = "/others/";//this is the path to my txt files, I have three, they each have "2","3", and "3",

// Open the directory
$dh = opendir($dir);
while ($file = readdir($dh) !== false){
  if ($file != '.' && $file != '..') {
    $fullfile = $dir . $file;
    $total += (int)trim(file_get_contents($fullfile));
  }
}

// Add them all together
$total = 0;
foreach ($subtotals as $st){
  $total = $total + $st;
}

// Echo the total
echo "$total\n";
?>
[/code]
Link to comment
Share on other sites

i still couldnt get it to work, may i ask what does this part:
$file = readdir($dh) !== false
and
$total += (int)trim(file_get_contents($fullfile));
part of the code mean? can anyone explain it to me? maybe I will have a better chance to get it working if I understand it better.
Thanks for any help provided
Ted
Link to comment
Share on other sites

Ted,

You've taken the code from two different posts there.  My code should work with your details like this:

[code]<?php
// Directory path
$dir = '/others/'; //this is the path to my txt files, I have three, they each have "2","3", and "3",

// Open the directory
$dh = opendir($dir);
while (($file = readdir($dh)) !== false){
  if ($file != '.' && $file != '..'){
    $fullfile = $dir . $file;
    $subtotals[] = file_get_contents($fullfile);
  }
}

// Add them all together
$total = 0;
foreach ($subtotals as $st){
  $total = $total + $st;
}

// Echo the total
echo "$total\n";
?>[/code]

Regards
Huggie
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.