tags:

views:

23

answers:

2

I know I know, a database would be the better idea with what I want to do, but I don't want the hassle of dealing with a database and learning stuff.

I have a CSV that has something similar to

TestUser,2010/07/27 13:26:25,2010/07/27 13:26:29,0.0011,0.0006,0.06
TestUser,2010/07/27 14:22:12,2010/07/27 14:22:24,0.0033,0.0027,0.27
TestUser,2010/07/27 14:22:41,2010/07/27 14:22:53,0.0033,0.0028,0.28
TestUser,2010/07/27 14:25:26,2010/07/27 14:25:33,0.0019,0.0015,0.30

the 2nd column is what I am using as a UID (unique ID), so it will never be the same in the CSV. The PHP code I use to write to the csv is:

$myFile = "data.csv";
$fh = fopen($myFile, 'a') or die("can't open file");

$stringData = $_POST["Name"].",".$_POST["DateStart"].",".$_POST["DateStop"].",".$_POST["TotalHours"].",".$_POST["Hours"].",".$_POST["Pay"]."\n";
fwrite($fh, $stringData);
fclose($fh);

What would need to be done to have it so if someone with the same DateStart (the UID) runs the PHP, it will overwrite whatever has that same UID. Basically I am thinking it would probably have to go through an file array with each line and then when it reaches a line that has the same UID, it would instead intercept what was POSTed and not what it has.

I am not sure if that is the best way of going about what I want.

+6  A: 

Learn databases. You'll be better off in the long run...

ircmaxell
Maybe on my next project, don't really feel like rewriting 10 PHP files to support databases when I already have a CSV thing in place. Plus I am using the CSVs (which have only 10-20 entries) and then the user can back them up and download them when they want. It's just a lot more work at this point to switch now.
BHare
Well, there's an old saying: `Anything worth doing, is worth doing right...`. And databases are the right thing based upon what you said in your question. If you're concerned about them being able to download them whenever they want, you can write a program to export the database into CSV... But the *right* solution is to use a database. Anything else will either be FAR more work than learning how to use them, or will be buggy and unreliable (and hence will be much more work in the long run). So, like I said before, just use databases.
ircmaxell
+1  A: 

While I completely agree with ircmaxell (learn to use a database), you still deserve a proper response to the question.

I do believe you would have to read line by line and do the comparison. No, it's not very efficient, but your setup doesn't really allow for efficiency.

"I am not sure if that is the best way of going about what I want." As ircmaxell suggested, using a database would be incredibly simple. mysql is free and is a piece of cake to pick up. Something as simple as this will be able to be hammered out in a couple hours tops.

Eclyps19