Edit CSV File in PHP

kalevra

Well-Known Member
Joined
Apr 2, 2009
Messages
478
Reaction score
3
Got a side project I'm playing with...

What's the easiest way to edit a CSV file (in the same DIR as the php file), take the 2nd column convert to numbers, manipulate (multiply by 1.05) and save after doing this for every row except the first?

Not a php programmer I do Java so it's a bit beyond my scope...
 
Probably not the answer you are looking for, but I would dump the CSV into a database and just do the appropriate queries on it.
 
Probably not the answer you are looking for, but I would dump the CSV into a database and just do the appropriate queries on it.

Yip you're right - seriously not the answer ;) hahaha

Less code to edit the CSV file, convert to doubles, multiply and get out.... rather than read csv, open db, write db, edit DB with SQL, export db back into CSV
 
I'd just do it in excel and save the lot again... (this is easiest, unless you're a PHP guru)

Thanks - I would do this but it needs to be done at 12AM every day - not crazy keen on doing it so automating it is what is needed here - work smart not hard :P
 
I could help you if you needed the .Net code.. but not PHP... sorry.
 
Yip you're right - seriously not the answer ;) hahaha

Less code to edit the CSV file, convert to doubles, multiply and get out.... rather than read csv, open db, write db, edit DB with SQL, export db back into CSV

Not sure I agree though ...

Import procedure: Write once, run many times.
Export procedure: Write once, run many times, i.e. just write 'n php script that pulls the data and returns it as CSV -- you don't physically have to generate a CSV again.
Convert / Multiply / etc, etc: Still only one query to write.

So instead of allowing some random editing on your website you would have one upload. After the upload you can just do the php-get whenever you want to.

Obviously don't really know your requirement, but personally think it's way easier and lot less work.
 
Not sure I agree though ...

Import procedure: Write once, run many times.
Export procedure: Write once, run many times, i.e. just write 'n php script that pulls the data and returns it as CSV -- you don't physically have to generate a CSV again.
Convert / Multiply / etc, etc: Still only one query to write.

So instead of allowing some random editing on your website you would have one upload. After the upload you can just do the php-get whenever you want to.

Obviously don't really know your requirement, but personally think it's way easier and lot less work.

Well I have a csv file in a certain directory created by an application - so this is part of the spec it cannot be changed ... working from this point I need data changed in the csv that cannot be done before the csv file is created (it's just the situation so can't change that)

Going from that stand point editing the csv file directly seems to make more sense to me...
 
PHP has CSV manipulation functions, there no reason to use MySQL. See fgetcsv and fputcsv.

Use fgetcsv to loop over each line of the CSV as an array, multiply the second column ($row[1] = $row[1] * 1.05) and write the altered row to a new CSV using fputcsv.
 
op, do yourself a favour and download the php chm.
it'll make finding those little functions a whole lot easier.
 
PHP has CSV manipulation functions, there no reason to use MySQL. See fgetcsv and fputcsv.

Use fgetcsv to loop over each line of the CSV as an array, multiply the second column ($row[1] = $row[1] * 1.05) and write the altered row to a new CSV using fputcsv.

Thanks will do!
 
Top
Sign up to the MyBroadband newsletter
X