Jump to content

$_POST to CSV with NULL values


mreish

Recommended Posts

Greetings folks,

 

I've tried searching on this but couldn't find anything which I think is odd since I'm sure I'm not the first person with this question...

 

 

I have a project that is basically a big (really big) form.  When the user hits submit they get a CSV file. My client will then import the CSV into their database.  The CSV is made up of two lines, the first being field names and the second line being the field values.  Both the names and values are pulled from $_POST.

 

The CSV looks like this:

 

"Name","Age","Favorite Color"

"Bob","12","Purple"

 

The problem I having is if the user skips a question, let's say Age for this example, the CSV will look like this:

 

"Name","Age","Favorite Color"

"Bob","Purple"

 

The clients database then freaks when it sees the value for Age is Purple.  So my question is, how does one handle null values from from $_POST to avoid such an issue?

 

Thank you!  Thank you!  Thank you! 

Link to comment
Share on other sites

Here's the code that assembles the array which is later tuned into an email CSV attachment. 

 


// this builds a list of field names
foreach ($_POST  as $key => $value) {
	$key= '"'.$key.'",';
	$placeHolderData .= $key;
}

// pull the last extra comma off
$placeHolderData = rtrim($placeHolderData, ",");

// add a line feed and new line 
$placeHolderData .= "\r\n";

// this builds the list of field values
foreach ($_POST  as $key => $value) {
	$value= '"'.$value.'",';
	$placeHolderData .= $value;
}

// pull the last comma off
$placeHolderData = rtrim($placeHolderData, ",");

 

Link to comment
Share on other sites

There seems nothing wrong with this code on the first look...except you can simplify it but doing it one loop.

 

Code is not tested but should give you a pretty good idea. This will ensure the field and data length match.

 

        $placeHolderFields = '';
        $placeHolderData = '';
        // this builds the list of field values
foreach ($_POST  as $key => $value) {
           if(trim($value) && trim($key)) {
                $key= '"'.$key.'",';
	$placeHolderFields .= $key;
	$value= '"'.$value.'",';
	$placeHolderData .= $value;
           }
}
         // pull the last extra comma off
$placeHolderData = rtrim($placeHolderData, ",");
        $placeHolderFields = rtrim($placeHolderFields , ",");

// add a line feed and new line 
$placeHolderData = $placeHolderFields."\r\n".$placeHolderData;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.