shaunie Posted June 4, 2013 Share Posted June 4, 2013 Hi, I have a CSV file with the following columns; id, name, email How can I loop through the file and just update the fields in the row that matches the particular id? Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/ Share on other sites More sharing options...
Eddy_J1 Posted June 4, 2013 Share Posted June 4, 2013 Sorry if the answer isn't brilliant, it's my first post Try using fopen to get the file contents and then use fgetcsv to parse the fields into an array, you can then loop through the array and change the rows for your particular id. http://php.net/manual/en/function.fopen.php http://www.php.net/manual/en/function.fgetcsv.php It may be really resource hungry but i'm an amateur myself so I'm not sure, this is just how I would go about it, the PHP manual is very handy. Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1433956 Share on other sites More sharing options...
shaunie Posted June 4, 2013 Author Share Posted June 4, 2013 Thanks I've got this so far but for some reason I am unable to open the file for reading and writing at the same time? if(isset($_POST)){ $row = 1; if (($handle = fopen("customers.csv", "r+")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++; for ($c=0; $c < $num; $c++) { echo '$data[0] = ' . $data[0]; if($data[0] == $_POST['customerId']){ echo 'true'; $data[1] = $_POST['name']; $data[2] = $_POST['email']; } } fputcsv($handle, $data); } fclose($handle); } } Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1433972 Share on other sites More sharing options...
mac_gyver Posted June 4, 2013 Share Posted June 4, 2013 because the length of the data is likely different, you would need to write the output to a different/temporary file so that you don't corrupt the input file. Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434002 Share on other sites More sharing options...
shaunie Posted June 4, 2013 Author Share Posted June 4, 2013 Thank you, I have updated it so the file is closed for reading and then opened for writing, but it's still not editing the file... if(isset($_POST)){ $row = 1; if (($handle = fopen("customers.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); $row++; for ($c=0; $c < $num; $c++) { // Customer ID exists so edit the record if($data[0] == $_POST['customerId']){ $data[1] = $_POST['name']; $data[2] = $_POST['email']; } } } fclose($handle); } if (($handle = fopen("customers.csv", "w")) !== FALSE) { fputcsv($handle, $data); } } Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434027 Share on other sites More sharing options...
Eiseth Posted June 4, 2013 Share Posted June 4, 2013 Try fputscsv http://php.net/manual/en/function.fputcsv.php Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434052 Share on other sites More sharing options...
shaunie Posted June 4, 2013 Author Share Posted June 4, 2013 Thank you, I am using fputcsv() but am having problems writing to the file, I am able to read from it... Could it be a permissions problem? Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434059 Share on other sites More sharing options...
mac_gyver Posted June 4, 2013 Share Posted June 4, 2013 the problem is your LOGIC. you must open the input file for reading, open a separate new output file for writing. then as you loop through the lines from the input file, you replace/update the data if it matches the submitted post data, and you write each line (both the unchanged and the updated ones) to the output file. Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434061 Share on other sites More sharing options...
DavidAM Posted June 4, 2013 Share Posted June 4, 2013 the problem is your LOGIC. you must open the input file for reading, open a separate new output file for writing. then as you loop through the lines from the input file, you replace/update the data if it matches the submitted post data, and you write each line (both the unchanged and the updated ones) to the output file. Then rename the two files, so the next time the process is run, you get the "new" file as input. By the way: if there is any possibility that the page will be submitted from two different users, or even browser tabs, at the same time, you will experience data loss. This is one of the reasons that a database is recommended for data processing. Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434074 Share on other sites More sharing options...
mac_gyver Posted June 4, 2013 Share Posted June 4, 2013 starting with the logic in this thread - http://forums.phpfreaks.com/topic/278189-csv-class-need-help-updating-a-csv-file/?hl=%2Btempnam you would end up with something that looks like - $csvfile = 'customers.csv'; $tempfile = tempnam(".", "tmp"); // produce a temporary file name, in the current directory if(!$input = fopen($csvfile,'r')){ die('could not open existing csv file'); } if(!$output = fopen($tempfile,'w')){ die('could not open temporary output file'); } while(($data = fgetcsv($input)) !== FALSE){ if($data[0] == $_POST['customerId']){ $data[1] = $_POST['name']; $data[2] = $_POST['email']; } fputcsv($output,$data); } fclose($input); fclose($output); unlink($csvfile); rename($tempfile,$csvfile); echo 'done'; Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434076 Share on other sites More sharing options...
shaunie Posted June 4, 2013 Author Share Posted June 4, 2013 Thanks guys, unfortunately the above code doesn't work, which leads me to believe there is a permissions issue. I have even commented out the lines: //unlink($csvfile); //rename($tempfile,$csvfile); and the temp file is not present in that directory... Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434098 Share on other sites More sharing options...
mac_gyver Posted June 4, 2013 Share Posted June 4, 2013 do you have php's error_reporting set to E_ALL and display_errors set to ON so that you would see all the php errors? if there's a permission problem, there would be a php error about it. Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434102 Share on other sites More sharing options...
shaunie Posted June 4, 2013 Author Share Posted June 4, 2013 thanks! I have included error reporting and I am indeed having a permissions issue! Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434111 Share on other sites More sharing options...
phong_ux Posted June 5, 2013 Share Posted June 5, 2013 (edited) Other way, You should try this: http://handsontable.com/ by copy and paste to table. i mean you open csv from excel and paste to handsontable form. they use PDO to control Mysql but i don't know to convert to standard Php Mysql Edited June 5, 2013 by phong_ux Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434145 Share on other sites More sharing options...
litebearer Posted June 5, 2013 Share Posted June 5, 2013 Perhaps... if(isset($_POST)){ $needle = $_POST['customerId']; $new_line = $needle . "," . $_POST['name'] . "," . $_POST['email']; $lines = file("customers.csv"); $c = count($lines); $i = 0; while($i<$c) { $t_array = explode(",", $lines[$i]); if($t_array[0] == $needle) { $lines[$i] = $new_line; } $new_data = $new_data . $lines[$i] . "\n"; $i ++; } $file_handle = fopen("customers.csv", 'w'); fwrite($file_handle, $new_data); fclose($file_handle); } Quote Link to comment https://forums.phpfreaks.com/topic/278749-updating-a-row-in-a-csv-file/#findComment-1434153 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.