/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] add numbers together and display results

Hello all,

I have a few different text files in different directories. Each text file contains a different number. I would like to be able to open a web page and see the result of the addition of the numbers in the text files. Is this possible to do?

Thanks so much

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiNov 23.2008 — Yes it is perfectly possible. You would need to have something like an array of filenames, then loop through reading the line in question, then just add the values. Assuming the number is the only item on the first line of each file:

[code=php]
$files = array('file1.txt', 'file2.txt', 'file3.txt'); //etc

$result = 0;
foreach ($files as $file) {
$lines = file($file);
$result += intval($lines[0]); // assuming your number is an integer
}

echo $result;
[/code]
Copy linkTweet thisAlerts:
@NogDogNov 23.2008 — Instead of breaking each file into an array of lines, I'd just do:
[code=php]
foreach ($files as $file) {
$result += (int)trim(file_get_contents($file));
}
[/code]
Copy linkTweet thisAlerts:
@scragarNov 23.2008 — Instead of breaking each file into an array of lines, I'd just do:
[code=php]
foreach ($files as $file) {
$result += (int)trim(file_get_contents($file));
}
[/code]
[/QUOTE]


foreach works in 1 of 2 ways:
foreach($array as $key)
foreach($array as $key=>$value)

I think you need to make an edit ?

[code=php]foreach ($files as $index) {
$result += (int)trim(file_get_contents($files[$index]));
}

//OR

foreach ($files as $key=>$file) {
$result += (int)trim(file_get_contents($file));
}[/code]
Copy linkTweet thisAlerts:
@MindzaiNov 23.2008 — Nope, it works in the following 2 ways:

foreach ($array as $value);
foreach ($array as $key=>$value);


Consider the following:

[code=php]$files = array('file1.txt', 'file2.txt', 'file3.txt');

foreach ($files as $file) {
echo $file . '<br />';
}[/code]


Produces:

file1.txt
file2.txt
file3.txt


NogDog's code works fine (assuming that this number is the only item in the file - I assumed it wasn't which is why I broke it down in into lines)
Copy linkTweet thisAlerts:
@NogDogNov 23.2008 — ...

NogDog's code works fine (assuming that this number is the only item in the file - I assumed it wasn't which is why I broke it down in into lines)[/QUOTE]


And ultimately only the original poster knows what the [I]real [/I]functional requirement is. Hopefully between our answers and his/her knowledge there is a usable solution. ?
Copy linkTweet thisAlerts:
@scragarNov 23.2008 — Nope, it works in the following 2 ways:

foreach ($array as $value);
foreach ($array as $key=&gt;$value);


Consider the following:

[code=php]$files = array('file1.txt', 'file2.txt', 'file3.txt');

foreach ($files as $file) {
echo $file . '<br />';
}[/code]


Produces:

file1.txt
file2.txt
file3.txt


NogDog's code works fine (assuming that this number is the only item in the file - I assumed it wasn't which is why I broke it down in into lines)[/QUOTE]


Your right, I have no idea what I was thinking of when I wrote that.
Copy linkTweet thisAlerts:
@cedric813authorNov 23.2008 — Hello everyone,

Thank so much for your help. It seems that what I am doinf is a little more complex than i originally stated. This is what i would like to do. I have the code below which belong to the file index.php. When index.php is loaded is shows counter hits for each day of the month, the monthly total of hits, and the grand total of hit. Right now it only takes the information from one place in order to display the counter hit for the day, month or total. I would like it to grab numerous files, which only contain a number in each file, from multiple locations (with the same file name) and add the them together and display the results for each day, month or total.

Basically, when I open index.php, and i look beside 2008-11-22, I want the number displayed there to be the total addition of the numbers contained in the following files: logs/2008-11-22_raw, logs2/2008-11-22_raw, logs3/2008-11-22_raw. Also, when I look beside 2008-11-23, I want the number displayed there to be the total addition of the numbers contained in the following files: logs/2008-11-23_raw, logs2/2008-11-23_raw, logs3/2008-11-23_raw. This will continue for each day of the month. When I look beside monthly total, in index.php, I would like it to display the total for the entire month. The same goes for the grand total which will just be the additon of all the months and years compined.


My file structure is as follows:

logs

|_ 2008-11-22_raw

|_
2008-11-23_raw

|_ alltotal_raw

logs2

|_
2008-11-22_raw

|_ 2008-11-23_raw

|_
alltotal_raw

logs3

|_ 2008-11-22_raw

|_
2008-11-23_raw

|_ alltotal_raw

index.php


2008-11-23_raw and 2008-11-22_raw will not be the only files in each directory. There will be one file for each day...formatted the same as these 2 files. These two files only contain a number (which is the counter number).

Here is the contents of index.php:

<i>
</i>&lt;?php putenv("TZ=America/Los_Angeles"); ?&gt;
&lt;?

$step=$HTTP_GET_VARS["step"];


if($step == "showrefs") {

$process=$HTTP_GET_VARS["process"];

$date=$HTTP_GET_VARS["date"];

print "&lt;u&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Referers for $date&lt;/b&gt;&lt;/font&gt;&lt;/u&gt;&lt;br&gt;&lt;br&gt;";

if($process == "getrawreferers") {
$filenamereferers="logs/".$date."_referers_raw";
}

if($process == "getuniquereferers") {
$filenamereferers="logs/".$date."_referers_unique";
}

if($process == "getuniquereferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_unique";
}

if($process == "getrawreferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_raw";
}





$filecontents=ParseFile("$filenamereferers");


$array = split("n", $filecontents);
$count = count($array);

for ($i=0; $i&lt;=$count; $i++)
{
echo "&lt;a href="$array[$i]" target="_blank"&gt;&lt;font face=verdana size=2&gt;$array[$i]&lt;/font&gt;&lt;/a&gt;&lt;br&gt;"; // add appropiate HTML tags here
}


exit;

}






$month=$HTTP_POST_VARS['monthdrop'];
$year=$HTTP_POST_VARS['yeardrop'];

if(empty($month) or empty($year)) {
$month=date("m");
$year=date("Y");
}


if($month == "04" or $month == "06" or $month == "09" or $month == "11") {
$datemax="30";
}

if($month == "01" or $month == "03" or $month == "05" or $month == "07" or $month == "08" or $month == "10" or $month == "12") {
$datemax="31";
}

if($month == "02") {

if($year == "2004" or $year == "2008" or $year == "2012" or $year == "2016" or $year == "2020" or $year == "2024" or $year == "2028" or $year == "2032") {
$datemax=29;
}
else {
$datemax=28;
}
}

$totaluniquehits=0;
$totalrawhits=0;

if(file_exists("logs/alltotal_unique")) {
$totaluniquesall=ParseFile("logs/alltotal_unique");
}
else {
$totaluniquesall=0;
}


if(file_exists("logs/alltotal_raw")) {
$totalrawsall=ParseFile("logs/alltotal_raw");
}
else {
$totalrawsall=0;
}

?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Stats&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor="#ffffff"&gt;
&lt;table width="400" border="0" align="center" cellpadding="3" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td height="45" bgcolor="black" border="0" bordercolor="black"&gt;&lt;div align="center"&gt;&lt;font color="#FFFFFF" size="4" face="Verdana, Arial, Helvetica, sans-serif"&gt;
Touched/Opened Statistics&lt;br&gt;Hotels - Page 1 - Ad 1&lt;/font&gt;&lt;br&gt;&lt;font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;Ad Locator Number: AAA00001&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;form method="POST" action="index.php"&gt;
&lt;p align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Year: &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;select size="1" name="yeardrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=2007;$i&lt;=$todaysyear;$i++) {

if($i==$year) {
echo "&lt;option selected&gt;$i&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$i&lt;/option&gt;";
}

}


?&gt;
&lt;/select&gt;
&lt;b&gt;&lt;font size="2" face="Verdana"&gt; &amp;nbsp;&amp;nbsp;Month: &lt;/font&gt;&lt;/b&gt;
&lt;select size="1" name="monthdrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=1;$i&lt;=12;$i++) {


if($i&lt;=9) {
$k='0'.$i;
}
else {
$k=$i;
}



if($k==$month) {
echo "&lt;option selected&gt;$k&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$k&lt;/option&gt;";
}

}
?&gt;
&lt;/select&gt;
&lt;/b&gt;&lt;/font&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;input type="submit" value="Go" name="B1"&gt;
&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;table border="0" align="center" cellspacing="0" &gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;font size="1" face="Verdana"&gt;12:00:00 am - 11:59:59 pm PST&lt;br&gt;(Pacific Standard Time)&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;div align="center"&gt;
&lt;center&gt;
&lt;table border="1" cellspacing="3" style="border-collapse: collapse" bordercolor="#ffffff" width="180" id="AutoNumber1" height="50"&gt;
&lt;tr&gt;
&lt;td width="115" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Date&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Hits&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;?

for($j=1;$j&lt;=$datemax;$j++) {

if($j&lt;=9) {
$m='0'.$j;
}
else {
$m=$j;
}

$dateeveryday=$year."-".$month."-".$m;
$datethismonth=$year."-".$month;

$filename_raw='logs/'.$dateeveryday."_raw";
$filename_unique='logs/'.$dateeveryday."_unique";


if(file_exists($filename_raw)) {
$rawhits=ParseFile($filename_raw);
}
else {
$rawhits="0";
}


if(file_exists($filename_unique)) {
$uniquehits=ParseFile($filename_unique);
}
else {
$uniquehits="0";
}

$totaluniquehits=$totaluniquehits+$uniquehits;
$totalrawhits=$totalrawhits+$rawhits;


echo " <br/>
&lt;tr&gt;
&lt;td width="115" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$dateeveryday&lt;/font&gt;&lt;/td&gt;
&lt;td width="65" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$rawhits&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;";


}



echo "


<i> </i>&lt;tr&gt;
<i> </i> &lt;td width="115" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Monthly Total:&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i> &lt;td width="65" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;$totalrawhits&lt;/a&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i>&lt;/tr&gt;";


?&gt;

&lt;/table&gt;
&lt;center&gt; &lt;table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="180" id="AutoNumber1"&gt;
&lt;tr&gt;
&lt;td width="115" align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Grand Total: &lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" align="center"&gt;&lt;font size="2" face="Verdana"&gt;&lt;b&gt;&lt;? echo $totalrawsall; ?&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;/center&gt;
&lt;/div&gt;

&lt;br&gt;
&lt;table width="350" border="0" align="center"&gt;
&lt;tr&gt;&lt;td&gt;&lt;font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;
We warrant that all airings and connections are valid and correct. &lt;br&gt;&lt;br&gt;&lt;img src="http://www.info-centres.com/images/sig.jpg"&gt;Management
&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;



&lt;?

##This function opens a file and returns its contents to the caller
function ParseFile ($filename)
{

if(!file_exists($filename)) {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;&lt;b&gt;Nothing found in the file&lt;/center&gt;&lt;/font&gt;";
exit;
}
else {

if(is_readable($filename)) {
$handle=fopen($filename,"r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

return $contents;
}
else {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;Unable to open the file: &lt;b&gt;$filename&lt;/b&gt;. &lt;br&gt;File is found in the specified directory but it has not been possible to open it. &lt;br&gt;This may be a permission problem. Please set permission of this file to : &lt;b&gt;644&lt;/b&gt;";
exit;
}

}
}

?&gt;
Copy linkTweet thisAlerts:
@MindzaiNov 23.2008 — I'm off to bed so I don't have time to give you a full answer now (although I will tomorrow if none of the php gurus have been along by then), but I will say just as an observation that you will probably find it more efficient and straightforward to use single log file which you parse to get whatever data you need. For example log1.raw could be:

2008-11-22 35
2008-11-23 56
etc


You would then only need one file operation to get all the data you need. Of course you may know something I don't and have a good reason for doing it this way, but that is just a bleary eyed observation, feel free to ignore as you see fit!
Copy linkTweet thisAlerts:
@cedric813authorNov 23.2008 — Hello,

thank you for your input. This would definetly be easier doing it the way you stated. The only problem is that we have roughly 800 counters (and many more to come) to keep track of...and changing the code now would really make my boss mad :-(

Any help you can provide with this would be greatly appreciated. Thanks so much!!
Copy linkTweet thisAlerts:
@scragarNov 23.2008 — [code=php]$data = array();
$dir_con = opendir('.');// current dir
while(FALSE !== ($subDir = readdir($dir_con))){
if(substr($subDir, 0, 1) != '.' && is_dir($subDir)){// it's a dir we want
$subDirCon = opendir($subDir);
while(FALSE !== ($file = readdir($subDirCon))){// for every file
if( !is_dir($subDir.'/'.$file)){
list($date) = explode('_', $file);// get the date
if( ! isset($data[$date]))
$data[$date] = 0;// init to 0
$data[$date] += (int) trim(file_get_contents($subDir.'/'.$file));
}
}
closedir($subDirCon);
}
}
closedir($dir_con);



// then to display the info:

foreach($data as $date=>$total){
echo "<p>{$date} : {$total}</p>";
}
[/code]
Copy linkTweet thisAlerts:
@NogDogNov 23.2008 — Untested:
[code=php]
<?php
/**
* Get log totals for given date
* @return integer
* @param string $date "yyyy-mm-dd"
*/
function getCountForDate($date)
{
$total = 0;
$logs = glob('/path/to/log/files/logs*/' . $date . '_raw');
foreach($logs as $log)
{
$total += (int) trim(file_get_contents($log));
}
return $total;
}

// sample usage:
echo "<p>Total for 2008-11-22: " . getCountForDate('2008-11-22') . "</p>n";
[/code]
Copy linkTweet thisAlerts:
@cedric813authorNov 23.2008 — Hello,

Thanks for your help. How would I implement your code into the code below?

thanks so much


<i>
</i>&lt;?php putenv("TZ=America/Los_Angeles"); ?&gt;
&lt;?

$step=$HTTP_GET_VARS["step"];


if($step == "showrefs") {

$process=$HTTP_GET_VARS["process"];

$date=$HTTP_GET_VARS["date"];

print "&lt;u&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Referers for $date&lt;/b&gt;&lt;/font&gt;&lt;/u&gt;&lt;br&gt;&lt;br&gt;";

if($process == "getrawreferers") {
$filenamereferers="logs/".$date."_referers_raw";
}

if($process == "getuniquereferers") {
$filenamereferers="logs/".$date."_referers_unique";
}

if($process == "getuniquereferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_unique";
}

if($process == "getrawreferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_raw";
}





$filecontents=ParseFile("$filenamereferers");


$array = split("n", $filecontents);
$count = count($array);

for ($i=0; $i&lt;=$count; $i++)
{
echo "&lt;a href="$array[$i]" target="_blank"&gt;&lt;font face=verdana size=2&gt;$array[$i]&lt;/font&gt;&lt;/a&gt;&lt;br&gt;"; // add appropiate HTML tags here
}


exit;

}






$month=$HTTP_POST_VARS['monthdrop'];
$year=$HTTP_POST_VARS['yeardrop'];

if(empty($month) or empty($year)) {
$month=date("m");
$year=date("Y");
}


if($month == "04" or $month == "06" or $month == "09" or $month == "11") {
$datemax="30";
}

if($month == "01" or $month == "03" or $month == "05" or $month == "07" or $month == "08" or $month == "10" or $month == "12") {
$datemax="31";
}

if($month == "02") {

if($year == "2004" or $year == "2008" or $year == "2012" or $year == "2016" or $year == "2020" or $year == "2024" or $year == "2028" or $year == "2032") {
$datemax=29;
}
else {
$datemax=28;
}
}

$totaluniquehits=0;
$totalrawhits=0;

if(file_exists("logs/alltotal_unique")) {
$totaluniquesall=ParseFile("logs/alltotal_unique");
}
else {
$totaluniquesall=0;
}


if(file_exists("logs/alltotal_raw")) {
$totalrawsall=ParseFile("logs/alltotal_raw");
}
else {
$totalrawsall=0;
}

?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Stats&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor="#ffffff"&gt;
&lt;table width="400" border="0" align="center" cellpadding="3" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td height="45" bgcolor="black" border="0" bordercolor="black"&gt;&lt;div align="center"&gt;&lt;font color="#FFFFFF" size="4" face="Verdana, Arial, Helvetica, sans-serif"&gt;
Touched/Opened Statistics&lt;br&gt;Hotels - Page 1 - Ad 1&lt;/font&gt;&lt;br&gt;&lt;font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;Ad Locator Number: AAA00001&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;form method="POST" action="index.php"&gt;
&lt;p align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Year: &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;select size="1" name="yeardrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=2007;$i&lt;=$todaysyear;$i++) {

if($i==$year) {
echo "&lt;option selected&gt;$i&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$i&lt;/option&gt;";
}

}


?&gt;
&lt;/select&gt;
&lt;b&gt;&lt;font size="2" face="Verdana"&gt; &amp;nbsp;&amp;nbsp;Month: &lt;/font&gt;&lt;/b&gt;
&lt;select size="1" name="monthdrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=1;$i&lt;=12;$i++) {


if($i&lt;=9) {
$k='0'.$i;
}
else {
$k=$i;
}



if($k==$month) {
echo "&lt;option selected&gt;$k&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$k&lt;/option&gt;";
}

}
?&gt;
&lt;/select&gt;
&lt;/b&gt;&lt;/font&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;input type="submit" value="Go" name="B1"&gt;
&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;table border="0" align="center" cellspacing="0" &gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;font size="1" face="Verdana"&gt;12:00:00 am - 11:59:59 pm PST&lt;br&gt;(Pacific Standard Time)&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;div align="center"&gt;
&lt;center&gt;
&lt;table border="1" cellspacing="3" style="border-collapse: collapse" bordercolor="#ffffff" width="180" id="AutoNumber1" height="50"&gt;
&lt;tr&gt;
&lt;td width="115" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Date&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Hits&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;?

for($j=1;$j&lt;=$datemax;$j++) {

if($j&lt;=9) {
$m='0'.$j;
}
else {
$m=$j;
}

$dateeveryday=$year."-".$month."-".$m;
$datethismonth=$year."-".$month;

$filename_raw='logs/'.$dateeveryday."_raw";
$filename_unique='logs/'.$dateeveryday."_unique";


if(file_exists($filename_raw)) {
$rawhits=ParseFile($filename_raw);
}
else {
$rawhits="0";
}


if(file_exists($filename_unique)) {
$uniquehits=ParseFile($filename_unique);
}
else {
$uniquehits="0";
}

$totaluniquehits=$totaluniquehits+$uniquehits;
$totalrawhits=$totalrawhits+$rawhits;


echo " <br/>
&lt;tr&gt;
&lt;td width="115" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$dateeveryday&lt;/font&gt;&lt;/td&gt;
&lt;td width="65" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$rawhits&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;";


}



echo "


<i> </i>&lt;tr&gt;
<i> </i> &lt;td width="115" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Monthly Total:&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i> &lt;td width="65" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;$totalrawhits&lt;/a&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i>&lt;/tr&gt;";


?&gt;

&lt;/table&gt;
&lt;center&gt; &lt;table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="180" id="AutoNumber1"&gt;
&lt;tr&gt;
&lt;td width="115" align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Grand Total: &lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" align="center"&gt;&lt;font size="2" face="Verdana"&gt;&lt;b&gt;&lt;? echo $totalrawsall; ?&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;/center&gt;
&lt;/div&gt;

&lt;br&gt;
&lt;table width="350" border="0" align="center"&gt;
&lt;tr&gt;&lt;td&gt;&lt;font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif"&gt;
We warrant that all airings and connections are valid and correct. &lt;br&gt;&lt;br&gt;&lt;img src="http://www.info-centres.com/images/sig.jpg"&gt;Management
&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;



&lt;?

##This function opens a file and returns its contents to the caller
function ParseFile ($filename)
{

if(!file_exists($filename)) {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;&lt;b&gt;Nothing found in the file&lt;/center&gt;&lt;/font&gt;";
exit;
}
else {

if(is_readable($filename)) {
$handle=fopen($filename,"r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

return $contents;
}
else {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;Unable to open the file: &lt;b&gt;$filename&lt;/b&gt;. &lt;br&gt;File is found in the specified directory but it has not been possible to open it. &lt;br&gt;This may be a permission problem. Please set permission of this file to : &lt;b&gt;644&lt;/b&gt;";
exit;
}

}
}

?&gt;
Copy linkTweet thisAlerts:
@MindzaiNov 24.2008 — The only problem is that we have roughly 800 counters (and many more to come) to keep track of...and changing the code now would really make my boss mad :-([/quote]

EDIT: just saw the above, priority #1 is not making the boss mad! The two solutions above are very neat and either will do the job nicely without making your boss angry!

Also the following may be of use if you do decide to use single log files:

[code=php]<?php
// parse file returning an array of info for day, month and total
function parse_log ($filename, $date) {

// ensure log file exists
if (!file_exists($filename)) return false;

// ensure valid date was passed
if (preg_match("/^(d{4})-(d{2})-(d{2})$/", $date, $matches)) {
if (!checkdate($matches[2], $matches[3], $matches[1])) {
return false;
}
} else {
return false;
}

$dateparts = explode('-', $date);
$start = mktime(0, 0, 0, $dateparts[1], 1, $dateparts[0]);
$end = mktime(0, 0, 0, $dateparts[1], date('t', $start), $dateparts[0]);

// read the file into an array line by line
$lines = file($filename);

// parse each line
foreach ($lines as $line) {

$parts = explode(' ', $line);
$parsed[$parts[0]] = (int)$parts[1];

// get an array containing just the months data too
$timestamp = strtotime($parts[0]);
if ($timestamp >= $start && $timestamp <= $end) {
$parsed_month[$parts[0]] = (int)$parts[1];
}

}

$results['day'] = $parsed[$date];
$results['total'] = array_sum($parsed);
$results['month'] = array_sum($parsed_month);

return $results;

}

$result = parse_log('log_raw', '2008-11-23');

print_r($result);

// results in Array ( [day] => 14 [total] => 93 [month] => 67 )

?>[/code]
Copy linkTweet thisAlerts:
@cedric813authorNov 24.2008 — hi there. thanks so much. how would i incorporate your code with mine?

thanks
Copy linkTweet thisAlerts:
@cedric813authorNov 25.2008 — This is what I have so far. I added some code mentioned above to my code. The added code is in blue text below. This part works and displays the Grand Total of all hits by adding the contents of all the alltotal_raw files and displaying a result as totalrawsall. I would like to do the same thing as what I did below, but would like the total hits for each day displayed beside the day. The files I will be working with all end in _raw but are in different directories all over the place. I have narrowed down the area that needs editing to somewhere in the red text below...but i'm not quite sure what do do now.

Any help would be great!!!




<i>
</i>&lt;?php putenv("TZ=America/Los_Angeles"); ?&gt;
&lt;?

$step=$HTTP_GET_VARS["step"];


if($step == "showrefs") {

$process=$HTTP_GET_VARS["process"];

$date=$HTTP_GET_VARS["date"];

print "&lt;u&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Referers for $date&lt;/b&gt;&lt;/font&gt;&lt;/u&gt;&lt;br&gt;&lt;br&gt;";

if($process == "getrawreferers") {
$filenamereferers="logs/".$date."_referers_raw";
}

if($process == "getuniquereferers") {
$filenamereferers="logs/".$date."_referers_unique";
}

if($process == "getuniquereferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_unique";
}

if($process == "getrawreferers_thismonth") {
$filenamereferers="logs/".$date."_allreferers_raw";
}





$filecontents=ParseFile("$filenamereferers");


$array = split("n", $filecontents);
$count = count($array);

for ($i=0; $i&lt;=$count; $i++)
{
echo "&lt;a href="$array[$i]" target="_blank"&gt;&lt;font face=verdana size=2&gt;$array[$i]&lt;/font&gt;&lt;/a&gt;&lt;br&gt;"; // add appropiate HTML tags here
}


exit;

}






$month=$HTTP_POST_VARS['monthdrop'];
$year=$HTTP_POST_VARS['yeardrop'];

if(empty($month) or empty($year)) {
$month=date("m");
$year=date("Y");
}


if($month == "04" or $month == "06" or $month == "09" or $month == "11") {
$datemax="30";
}

if($month == "01" or $month == "03" or $month == "05" or $month == "07" or $month == "08" or $month == "10" or $month == "12") {
$datemax="31";
}

if($month == "02") {

if($year == "2004" or $year == "2008" or $year == "2012" or $year == "2016" or $year == "2020" or $year == "2024" or $year == "2028" or $year == "2032") {
$datemax=29;
}
else {
$datemax=28;
}
}

$totaluniquehits=0;
$totalrawhits=0;

if(file_exists("logs/alltotal_unique")) {
$totaluniquesall=ParseFile("logs/alltotal_unique");
}
else {
$totaluniquesall=0;
}

[COLOR="DeepSkyBlue"]$files = array('http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_1/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_2/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_3/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_4/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_5/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_6/logs/alltotal_raw', 'http://www.something.com/admin/administrators/canada/bc/vancouver/via_rail/ad_counters/hotels/hotels_p1_7/logs/alltotal_raw');
$totalrawsall = 0;
foreach ($files as $file) {
$lines = file($file);
$totalrawsall += intval($lines[0]);
}[/COLOR]
?&gt;


&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Stats&lt;/title&gt;
&lt;/head&gt;

&lt;body bgcolor="#ffffff"&gt;
&lt;table width="400" border="0" align="center" cellpadding="3" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td height="35" bgcolor="black" border="0" bordercolor="black"&gt;&lt;div align="center"&gt;&lt;font color="#FFFFFF" size="4" face="Verdana, Arial, Helvetica, sans-serif"&gt;
Touched/Opened Totals&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;form method="POST" action="index.php"&gt;
&lt;p align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Year: &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;select size="1" name="yeardrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=2007;$i&lt;=$todaysyear;$i++) {

if($i==$year) {
echo "&lt;option selected&gt;$i&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$i&lt;/option&gt;";
}

}


?&gt;
&lt;/select&gt;
&lt;b&gt;&lt;font size="2" face="Verdana"&gt; &amp;nbsp;&amp;nbsp;Month: &lt;/font&gt;&lt;/b&gt;
&lt;select size="1" name="monthdrop"&gt;
&lt;?

$todaysyear=date("Y");


for($i=1;$i&lt;=12;$i++) {


if($i&lt;=9) {
$k='0'.$i;
}
else {
$k=$i;
}



if($k==$month) {
echo "&lt;option selected&gt;$k&lt;/option&gt;";
}
else {
echo "&lt;option&gt;$k&lt;/option&gt;";
}

}
?&gt;
&lt;/select&gt;
&lt;/b&gt;&lt;/font&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/b&gt;&lt;font face="Verdana"&gt;&lt;b&gt;
&lt;input type="submit" value="Go" name="B1"&gt;
&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/form&gt;
&lt;table border="0" align="center" cellspacing="0" &gt;
&lt;tr&gt;
&lt;td align="center"&gt;&lt;font size="1" face="Verdana"&gt;12:00:00 am - 11:59:59 pm PST&lt;br&gt;(Pacific Standard Time)&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;div align="center"&gt;
&lt;center&gt;
&lt;table border="1" cellspacing="3" style="border-collapse: collapse" bordercolor="#ffffff" width="180" id="AutoNumber1" height="50"&gt;
&lt;tr&gt;
&lt;td width="115" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Date&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" bgcolor="#E2E2E2" align="center" height="21"&gt;&lt;b&gt;
&lt;font size="1" face="Verdana"&gt;Hits&lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;?

for($j=1;$j&lt;=$datemax;$j++) {

if($j&lt;=9) {
$m='0'.$j;
}
else {
$m=$j;
}

[COLOR="Red"]$dateeveryday=$year."-".$month."-".$m;
$datethismonth=$year."-".$month;

$filename_raw='logs/'.$dateeveryday."_raw";


if(file_exists($filename_raw)) {
$rawhits=ParseFile($filename_raw);
}
else {
$rawhits="0";
}


$totalrawhits=$totalrawhits+$rawhits;[/COLOR]

echo " <br/>
&lt;tr&gt;
&lt;td width="115" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$dateeveryday&lt;/font&gt;&lt;/td&gt;
&lt;td width="65" height="15" align="center"&gt;&lt;font face=verdana size=2&gt;$rawhits&lt;/a&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;";


}



echo "


<i> </i>&lt;tr&gt;
<i> </i> &lt;td width="115" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;Monthly Total:&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i> &lt;td width="65" height="25" align="center"&gt;&lt;font face=verdana size=2&gt;&lt;b&gt;$totalrawhits&lt;/a&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
<i> </i>&lt;/tr&gt;";


?&gt;

&lt;/table&gt;
&lt;center&gt; &lt;table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="180" id="AutoNumber1"&gt;
&lt;tr&gt;
&lt;td width="115" align="center"&gt;&lt;b&gt;&lt;font size="2" face="Verdana"&gt;Grand Total: &lt;/font&gt;&lt;/b&gt;&lt;/td&gt;
&lt;td width="65" align="center"&gt;&lt;font size="2" face="Verdana"&gt;&lt;b&gt;&lt;? echo $totalrawsall; ?&gt;&lt;/b&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;/center&gt;
&lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;



&lt;?

##This function opens a file and returns its contents to the caller
function ParseFile ($filename)
{

if(!file_exists($filename)) {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;&lt;b&gt;Nothing found in the file&lt;/center&gt;&lt;/font&gt;";
exit;
}
else {

if(is_readable($filename)) {
$handle=fopen($filename,"r");
$contents = fread ($handle, filesize ($filename));
fclose ($handle);

return $contents;
}
else {
print "&lt;center&gt;&lt;font size=1 face=verdana&gt;Unable to open the file: &lt;b&gt;$filename&lt;/b&gt;. &lt;br&gt;File is found in the specified directory but it has not been possible to open it. &lt;br&gt;This may be a permission problem. Please set permission of this file to : &lt;b&gt;644&lt;/b&gt;";
exit;
}

}
}

?&gt;
×

Success!

Help @cedric813 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...