I tried a lot of things, but none of them seems to work properly... This is the csv file:
and this is my code:
$file_handle = fopen('file.csv', 'r ');
$positionID = 47901392;
while ($data = fgetcsv ($file_handle, 1000, ";")) {
$prod_id = $data[0];
if($prod_id ==$positionID){
echo "prod_id $prod_id ";
echo "I have to delete row";
}
}
I have to delete row with specific $positionID.
CodePudding user response:
Open another file to hold the new file with the lines deleted. Then have your loop write everything that isn't deleted to that file.
$file_handle = fopen('file.csv', 'r');
$out_handle = fopen('new_file.csv', 'w');
$positionID = 47901392;
while ($data = fgetcsv ($file_handle, 1000, ";")) {
$prod_id = $data[0];
if($prod_id ==$positionID){
echo "prod_id $prod_id ";
echo "I have to delete row";
} else {
fputcsv($out_handle, $data);
}
}
fclose($file_handle);
fclose($out_handle);
rename('new_file.csv', 'file.csv');