Delete rows from csv file if a column is empty

Hi,

New here (and new to php :frowning: ), and have a basic php script to post formdata to a csv. Data is (up to) 20 lines, with first 9 columns (of 14) containing same data. Columns 10-14 are optional inputs.

My $_post posts all 20 rows to csv (with cols 1 to 9 generic / same data from form - eg posters name etc) - what I am struggling with now is to write a script to delete any rows in the csv file that contain no data in columns 10 to 14. (eg where poster does not need to post / enter all 20 entries)

Apologies if this query seems simple, but any help would be greatly appreciated - as I said, I am new to php, still learning but after a couple of days scratching my head, I thought it time to ask for assistance.

Cheers.

Please post your code used to write the CSV file and an example of the output (written CSV file)

As I said, it is pretty basic and I’ve no doubt that there is a much simpler solution. Apologies if this seems way too simple and shows me for what I am - a mere beginner in the world of php :(.

[php]<?PHP

{$ent = $_POST[‘Location’];
$dec = $_POST[‘Report’];
$month = $_POST[‘Month’];
$curr = $_POST[‘Currency’];
$exch = $_POST[‘Exch_Rate’];
$prep = $_POST[‘Prepared_By’];
$phone = $_POST[‘Phone’];
$skype = $_POST[‘Skype’];
$email = $_POST[‘Email’];
$fn001 = $_POST[‘FirstName0001’];
$ln001 = $_POST[‘FamilyName0001’];
$dob001 = $_POST[‘DOB0001’];
$add001 = $_POST[‘ADDValue0001’];
$com001 = $_POST[‘Comments0001’];
$fn002 = $_POST[‘FirstName0002’];
$ln002 = $_POST[‘FamilyName0002’];
$dob002 = $_POST[‘DOB0002’];
$add002 = $_POST[‘ADDValue0002’];
$com002 = $_POST[‘Comments0002’];
$fn003 = $_POST[‘FirstName0003’];
$ln003 = $_POST[‘FamilyName0003’];
$dob003 = $_POST[‘DOB0003’];
$add003 = $_POST[‘ADDValue0003’];
$com003 = $_POST[‘Comments0003’];
$fn004 = $_POST[‘FirstName0004’];
$ln004 = $_POST[‘FamilyName0004’];
$dob004 = $_POST[‘DOB0004’];
$add004 = $_POST[‘ADDValue0004’];
$com004 = $_POST[‘Comments0004’];
$fn005 = $_POST[‘FirstName0005’];
$ln005 = $_POST[‘FamilyName0005’];
$dob005 = $_POST[‘DOB0005’];
$add005 = $_POST[‘ADDValue0005’];
$com005 = $_POST[‘Comments0005’];
$fn006 = $_POST[‘FirstName0006’];
$ln006 = $_POST[‘FamilyName0006’];
$dob006 = $_POST[‘DOB0006’];
$add006 = $_POST[‘ADDValue0006’];
$com006 = $_POST[‘Comments0006’];
$fn007 = $_POST[‘FirstName0007’];
$ln007 = $_POST[‘FamilyName0007’];
$dob007 = $_POST[‘DOB0007’];
$add007 = $_POST[‘ADDValue0007’];
$com007 = $_POST[‘Comments0007’];
$fn008 = $_POST[‘FirstName0008’];
$ln008 = $_POST[‘FamilyName0008’];
$dob008 = $_POST[‘DOB0008’];
$add008 = $_POST[‘ADDValue0008’];
$com008 = $_POST[‘Comments0008’];
$fn009 = $_POST[‘FirstName0009’];
$ln009 = $_POST[‘FamilyName0009’];
$dob009 = $_POST[‘DOB0009’];
$add009 = $_POST[‘ADDValue0009’];
$com009 = $_POST[‘Comments0009’];
$fn010 = $_POST[‘FirstName0010’];
$ln003 = $_POST[‘FamilyName0010’];
$dob010 = $_POST[‘DOB0010’];
$add010 = $_POST[‘ADDValue0010’];
$com010 = $_POST[‘Comments0010’];
$fn011 = $_POST[‘FirstName0011’];
$ln011 = $_POST[‘FamilyName0011’];
$dob011 = $_POST[‘DOB0011’];
$add011 = $_POST[‘ADDValue0011’];
$com011 = $_POST[‘Comments0011’];
$fn012 = $_POST[‘FirstName0012’];
$ln012 = $_POST[‘FamilyName0012’];
$dob012 = $_POST[‘DOB0012’];
$add012 = $_POST[‘ADDValue0012’];
$com012 = $_POST[‘Comments0012’];
$fn013 = $_POST[‘FirstName0013’];
$ln013 = $_POST[‘FamilyName0013’];
$dob013 = $_POST[‘DOB0013’];
$add013 = $_POST[‘ADDValue0013’];
$com013 = $_POST[‘Comments0013’];
$fn014 = $_POST[‘FirstName0014’];
$ln014 = $_POST[‘FamilyName0014’];
$dob014 = $_POST[‘DOB0014’];
$add014 = $_POST[‘ADDValue0014’];
$com014 = $_POST[‘Comments0014’];
$fn015 = $_POST[‘FirstName0015’];
$ln015 = $_POST[‘FamilyName0015’];
$dob015 = $_POST[‘DOB0015’];
$add015 = $_POST[‘ADDValue0015’];
$com015 = $_POST[‘Comments0015’];
$fn015 = $_POST[‘FirstName0016’];
$ln016 = $_POST[‘FamilyName0016’];
$dob016 = $_POST[‘DOB0016’];
$add016 = $_POST[‘ADDValue0016’];
$com016 = $_POST[‘Comments0016’];
$fn017 = $_POST[‘FirstName0017’];
$ln017 = $_POST[‘FamilyName0017’];
$dob017 = $_POST[‘DOB0017’];
$add017 = $_POST[‘ADDValue0017’];
$com017 = $_POST[‘Comments0017’];
$fn018 = $_POST[‘FirstName0018’];
$ln018 = $_POST[‘FamilyName0018’];
$dob018 = $_POST[‘DOB0018’];
$add018 = $_POST[‘ADDValue0018’];
$com018 = $_POST[‘Comments0018’];
$fn019 = $_POST[‘FirstName0019’];
$ln019 = $_POST[‘FamilyName0019’];
$dob019 = $_POST[‘DOB0019’];
$add019 = $_POST[‘ADDValue0019’];
$com019 = $_POST[‘Comments0019’];
$fn020 = $_POST[‘FirstName0020’];
$ln020 = $_POST[‘FamilyName0020’];
$dob020 = $_POST[‘DOB0020’];
$add020 = $_POST[‘ADDValue0020’];
$com020 = $_POST[‘Comments0020’];

//this is where the creating of the csv takes place
$cvsData = $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn001 ."," . $ln001 . “,” . $dob001 . “,” . $add001 . “,” . $com001 . “\n” . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn002 ."," . $ln002 . “,” . $dob002 . “,” . $add002 . “,” . $com002 . “\n” . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn003 ."," . $ln003 . “,” . $dob003 . “,” . $add003 . “,” . $com003 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn004 ."," . $ln004 . “,” . $dob004 . “,” . $add004 . “,” . $com004 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn005 ."," . $ln005 . “,” . $dob005 . “,” . $add005 . “,” . $com005 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn006 ."," . $ln006 . “,” . $dob006 . “,” . $add006 . “,” . $com006 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn007 ."," . $ln007 . “,” . $dob007 . “,” . $add007 . “,” . $com007 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn008 ."," . $ln008 . “,” . $dob008 . “,” . $add008 . “,” . $com008 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn009 ."," . $ln009 . “,” . $dob009 . “,” . $add009 . “,” . $com009 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn010 ."," . $ln010 . “,” . $dob010 . “,” . $add010 . “,” . $com010 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn011 ."," . $ln011 . “,” . $dob011 . “,” . $add011 . “,” . $com011 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn012 ."," . $ln012 . “,” . $dob012 . “,” . $add012 . “,” . $com012 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn013 ."," . $ln013 . “,” . $dob013 . “,” . $add013 . “,” . $com013 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn014 ."," . $ln014 . “,” . $dob014 . “,” . $add014 . “,” . $com014 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn015 ."," . $ln015 . “,” . $dob015 . “,” . $add015 . “,” . $com015 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn016 ."," . $ln016 . “,” . $dob016 . “,” . $add016 . “,” . $com016 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn017 ."," . $ln017 . “,” . $dob017 . “,” . $add017 . “,” . $com017 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn018 ."," . $ln018 . “,” . $dob018 . “,” . $add018 . “,” . $com018 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn019 ."," . $ln019 . “,” . $dob019 . “,” . $add019 . “,” . $com019 ."\n" . $ent . “,” . $dec . “,” . $month . “,” . $curr . “,” . $exch . “,” . $prep . “,” . $phone . “,” . $skype . “,” .$email . “,” . $fn020 ."," . $ln020 . “,” . $dob020 . “,” . $add020 . “,” . $com020 ."\n";

$fp = fopen(“Add2012New2.csv”,“a”);

if($fp){
fwrite($fp,$cvsData);

fclose($fp);

}}}?>[/php]

Example Output CSV :

Location Report Month Currency Exch_Rate Prepared_By Phone Skype Email First_Name Last_Name DOB ADD_Value Comments Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] John Doe 26-07-85 1000 2nd Test Row 1 Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Jane Doe 21-04-83 2000 2nd Test Row 2 Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected] Hometown AD&D Oct-12 $ US 1 hirsty 12345678910 25863598 [email protected]

Poster may input 1 or more entries for columns 10-14. Webform captures columns 1-9 in one entry. In above example, I’d like to delete rows 3-21 (row 1 is headers) if column 10 is empty/null.

Many Thanks, and apologies again if I’m missing something obvious !

H.

Could you also post the HTML for your form? That does look highly inefficient :slight_smile:

:). I suspected as much !. Here’s the basic code (NB : Form designed using Simfatic Forms - I’m just trying to process form input separately). There are field validations after this and before processing, too.

I’ve cut it down some, as it was too long. Just removed refs to Rows 5-20 :slight_smile:

[code]






Reporting 2012



Location


Report


Month


Currency


Exch Rate



Select Entity From List
Australia
Canada
Denmark
France
Italy
Poland
Other







Select Month
January 2012
February 2012
March 2012
April 2012
May 2012
June 2012
July 2012
August 2012
September 2012
October 2012
November 2012
December 2012




Select Currency
$ US
$ AUS
€ Euro






Phone No


Skype Phone No


Email Address


Prepared By














Date of Birth
(DD/MM/YYYY)


Family Name


First Name


AD&D Value


Comments





























































//above codes repeated for rows 5 to 20




[/code]

H.

Let me just try to put you on the right path with the data fields and see what you can come up with.

On your form, you could set your field names as arrays. For example:

<input type="text" name="Data[1][FirstName]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[1][FamilyName]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[1][DOB]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[1][ADDValue]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[1][Comments]" value="" size="20" class="sfm_textbox"/>

<input type="text" name="Data[2][FirstName]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[2][FamilyName]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[2][DOB]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[2][ADDValue]" value="" size="20" class="sfm_textbox"/>
<input type="text" name="Data[2][Comments]" value="" size="20" class="sfm_textbox"/>

etc...

It will give you something like this:

[Data] => Array
	(
		[1] => Array
			(
				[FirstName] => A1
				[FamilyName] => A2
				[DOB] => A3
				[ADDValue] => A4
				[Comments] => A5
			)

		[2] => Array
			(
				[FirstName] => B1
				[FamilyName] => B2
				[DOB] => B3
				[ADDValue] => B4
				[Comments] => B5
			)
	)

Now that you have an array to work with, it is much easier to remove empty rows. You could do something like this for example:

[php]
// Loop posted ‘Data’ array
$data = $_POST[‘Data’];
foreach($data as $row => $fields) {
$data[$row] = array_filter($fields); // array_filter will remove all empty fields
if (empty($data[$row])) {
unset($data[$row]); // remove this row from $data if array_filter results in an empty array
}
}
print_r($data);
[/php]

Now, $data contains only what you wish to write to the CSV. Then you could also iterate this array to build your CSV string.

Sponsor our Newsletter | Privacy Policy | Terms of Service