mreish Posted April 3, 2012 Share Posted April 3, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/260301-_post-to-csv-with-null-values/ Share on other sites More sharing options...
requinix Posted April 3, 2012 Share Posted April 3, 2012 Depends on your code. Which is what, exactly? Quote Link to comment https://forums.phpfreaks.com/topic/260301-_post-to-csv-with-null-values/#findComment-1334185 Share on other sites More sharing options...
mreish Posted April 4, 2012 Author Share Posted April 4, 2012 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, ","); Quote Link to comment https://forums.phpfreaks.com/topic/260301-_post-to-csv-with-null-values/#findComment-1334194 Share on other sites More sharing options...
samshel Posted April 4, 2012 Share Posted April 4, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/260301-_post-to-csv-with-null-values/#findComment-1334202 Share on other sites More sharing options...
mreish Posted April 4, 2012 Author Share Posted April 4, 2012 Awesome. Thank you. Learned some thing new! Quote Link to comment https://forums.phpfreaks.com/topic/260301-_post-to-csv-with-null-values/#findComment-1334407 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.