Click to See Complete Forum and Search --> : writing to a log file
hammerslane
12-01-2003, 05:42 AM
hi there.
i know a few of my recent threads have been very confusing, so i will explain my problem very thoroughly, so apologies if it seems very long winded.
my company
we are a web design company who have about 40 clients. as a method for counting how many hours an employee has worked on a clients job every day, every employee fills out a little piece of paper which is like this:
===========================
employee name: _______
date: ________
client | job | time spent
____ ____ _____________
____ ____ _____________
____ ____ _____________
____ ____ _____________
===========================
there are 13 employees are our company, so as you can guess, there are around 60 pieces of paper handed to me every week, that every one fills in.
our company has an intranet system set up, so i used this to my advantage, and set up a PHP based job-sheet system, so instead of writing on a job sheet (illustrated above), people just fill in an online form, press submit, and i am emailed with the contents of the form.
this works fine and well.
the problem lies in the fact that i am not at work all the time, and if i take 3 weeks holiday, there will be 3 weeks where no one can retrieve the emails that i have been sent.
so, i think, the easiest way to get around this problem is when people press SUBMIT on the form, aswell as the form being emailed to me, it is also written to a log file for archive/emergency purposes.
i have no idea how to go about this- can anyone point me in the vague direction of a function that might help me? or just any other suggestions, like telling me how stupid i am, or an easier way to do it... or a code snippet, or a link... you get the idea...
all help is much appreciated as usual
many thanks
Pittimann
12-01-2003, 07:08 AM
Hi!
Trying to keep it short, I just made an example for one single job per employee and form submit. As you already built up the form and everything for the mailing business it will be easy to modify the following by adding loops for the other jobs per submission.
The (ugly) form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form method="post" action="PathToYour/script.php"><br>
employee: <input type="text" name="employee"><br>
client: <input type="text" name="client"><br>
job: <input type="text" name="job"><br>
timeSpent: <input type="text" name="timeSpent"><br>
<input type="submit" value="Submit"></form>
</body>
</html>
The code in your script.php for writing data (could e.g. follow the code, where you already coded for the mails to be sent to you):
<?php
//$employee, $client, $job and $timeSpent have to be the names of
//the respective formfields in this example
$Logfile = "PathToLogfile/NameOfLogfile";//Your path to logfile here
if (!file_exists($Logfile)){//create logfile if it doesn't exist yet
fclose(fopen($Logfile,"w+"));
}
$fp=fopen($Logfile,"a");//open file for adding content at its' end
fwrite($fp,$employee."µ".time()."µ".$client."µ".$job."µ".$timeSpent."µ"."\n");
//"µ" is separator for one employee's entries
//"\n" (linebreak) is separator between the entries of different employes
fclose($fp);//close file
?>
The code for reading the data:
<?php
$Logfile = "PathToLogfile/NameOfLogfile";//Your path to logfile here
$Logfile = file($Logfile);
$CountEntries = count($Logfile);
for ($j=0;$j< $CountEntries;$j++){
$entry = explode("µ", $Logfile[$j]);
$employee=$entry[0];
$timestamp=$entry[1];
$client=$entry[2];
$job=$entry[3];
$timeSpent = $entry[4];
$DateOfEntry = date("l, d.m.Y",$timestamp);
echo 'Record of employee '.$employee.' for '.$DateOfEntry.'<br>';
echo 'For client: '.$client.' - job done: '.$job.' - time spent on that job: '.$timeSpent.'<br><br>';
}
?>
Once you adjusted everything for your needs you can use a thing like that for filtering data as well, like totalling time spent for a certain client or the time a certain employee worked last month...
Cheers - Pit
P.S. I think you better should design that for a database instead of a logfile. Anyway - it also works like that :rolleyes:
hammerslane
12-01-2003, 08:02 AM
thanks very much pit, i'll let you know how i get on in a couple of hours...
Pittimann
12-01-2003, 08:08 AM
You're welcome!
Cheers - Pit
hammerslane
12-01-2003, 08:32 AM
whoa this is the first time that a php script has worked so well for me from a forum... i guess explaining the quesiton properly does help...
one thing that i didn't mention though.
the form that i illustrated in my first post had about 5 rows or something? in my online form, i have 8 rows, for 8 different jobs. i pass the variables from the first form page, over to the mail script. so the 'information to write to log' code goes like this...
fwrite($fp,$user."µ".time()."µ".$date."µ".$client1."µ".$job1."µ".$time1."µ".
"µ".$client2."µ".$job2."µ".$time2."µ".
"µ".$client3."µ".$job3."µ".$time3."µ".
"µ".$client4."µ".$job4."µ".$time4."µ".
"µ".$client5."µ".$job5."µ".$time5."µ".
"µ".$client6."µ".$job6."µ".$time6."µ".
"µ".$client7."µ".$job7."µ".$time7."µ".
"µ".$client8."µ".$job8."µ".$time8."µ"."\n");
[you'll notice i changed the variable names to match my form].
there's probably an easier way to get around having to pass millions of variables like this, but i'm too stupid to do it an easier/another way.
so - the log.log file (that's what i called the log file) code goes like this;
echo 'Record of employee '.$user.' for '.$date.'<br>';
echo 'For client: '.$client1.' - job done: '.$job1.' - time spent on that job: '.$time1.'<br><br>';this code will only return the client row entry won't it? is there a way to do some kind of while loop to display all the row entries?
huge amounts of thanks to you
regards
Pittimann
12-01-2003, 09:06 AM
Hi!
My code was just an example shortened to one client. As far as writing to the logfile is concerned, check out this modification:
$Logfile = "PathToLogfile/NameOfLogfile";//Your path to logfile here
if (!file_exists($Logfile)){
fclose(fopen($Logfile,"w+"));
}
foreach($HTTP_POST_VARS as $name=> $value) {
$WriteThisToFile .= $value."µ";//collect all postvars in one string
}
$WriteThisToFile = time()."µ".$WriteThisToFile."\n";//add time as entry[0] and linebrak at the end
$fp=fopen($Logfile,"a");
fwrite($fp,$WriteThisToFile);//write the whole bunch to logfile
fclose($fp);
So you will collect the contents of all the form fields.
To read them out, just add all your clientxy, jobxy etc. to the "reading" code (you know better than me how many they actually are). Actually you can add a $date and read that out later - if an employee submits the form on a different day than the one to which the data belong. In addition to that you have 3 fields for every job (client, job, time spent). In your example that would be 24 (3 * 8) for the jobs, plus one for the user and one for the date...
Please not that the code above will give you the timestamp as entry[0] in the reading code, the sequence of all the variables saved to the file corresponds to the layout of your form.
If you use the above code you would have 27 values written to the file. (Timestamp + 24 + 1 + 1). So you have to read from $entry[0] to $entry[26]. It is up to you then, where to add a double <br> (e.g. between the single jobs...).
Hope I made myself clear - sometimes it is difficult for me to express myself in a foreign language. :rolleyes:
Cheers - Pit
hammerslane
12-08-2003, 11:03 AM
i'm afraid that i'm very lost on how to display multiple log entries on the viewlog.php
it's not down to your english (it is brilliant) - it's down to my lack of php knowledge...
what exactly do i have to change about this code? $user=$entry[0];
$date=$entry[1];
$client1=$entry[2];
$job1=$entry[3];
$time1 = $entry[4];
Pittimann
12-08-2003, 11:41 AM
Hi!
Hope that helps (the code for reading the data):
$jobCount=3;//put your number of jobs in the form here
$Logfile = "PathToLogfile/NameOfLogfile";
$Logfile = file($Logfile);
$CountEntries = count($Logfile);
for ($j=0;$j< $CountEntries;$j++){
$entry = explode("µ", $Logfile[$j]);
$timeSpent = $entry[4];
$DateOfEntry = date("l, d.m.Y",$timestamp);
$timestamp=$entry[0];
$employee=$entry[2];
$date=$entry[1];
$DateOfEntry = date("l, d.m.Y",$timestamp);
echo 'Record of employee '.$employee.' for '.$date.' (saved: '.$DateOfEntry.')<br>';
for ($i=0;$i< $jobCount;$i++){
echo 'For client: '.$entry[$i*3+3].' - job done: '.$entry[$i*3+4].' - time spent on that job: '.$entry[$i*3+5].'<br><br>';
}
}
Cheers - Pit