ted_chou12 Posted December 18, 2006 Share Posted December 18, 2006 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... :'( Quote Link to comment Share on other sites More sharing options...
redbullmarky Posted December 18, 2006 Share Posted December 18, 2006 what have you tried so far? got any code to show? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 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 totalecho "$total\n";?>[/code]RegardsHuggie Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 thanks, but could you explain on the sub total please? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 OK, $subtotals is just an array of each of the numbers from each of the files.I place the number from each of the files into that array and then foreach one, add it to $total.RegardsHuggie Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 oh, i understand. thankyou Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 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.RegardsHuggie Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 18, 2006 Share Posted December 18, 2006 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] Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 18, 2006 Share Posted December 18, 2006 [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]<?phpwhile (($file = readdir($dh) !== false){ if ($file != '.' && $file != '..') { $fullfile = $dir . $file; $total += (int)trim(file_get_contents($fullfile)); }}?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 thanks, your assumption is correct, there are 'infinite' number of files and each one of them has only a interger in them. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 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. Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 18, 2006 Share Posted December 18, 2006 [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 ;) Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 Yes, flat file DB is definitely what you're after here.RegardsHuggie Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 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 anywayTed. :D Quote Link to comment Share on other sites More sharing options...
obsidian Posted December 18, 2006 Share Posted December 18, 2006 [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 anywayTed. :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|36about.php|8contact.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. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 yeap, i understand the conveniece to the database "is it what its called?", actually, its worth a try, do you know anywhere that has some further tutorials or instructions on that??Thanks Ted. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 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?RegardsHuggie Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 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 totalecho "$total\n";?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted December 18, 2006 Author Share Posted December 18, 2006 i still couldnt get it to work, may i ask what does this part:$file = readdir($dh) !== falseand$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 providedTed Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 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 totalecho "$total\n";?>[/code]RegardsHuggie 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.