jayfcf Posted June 18, 2010 Share Posted June 18, 2010 Hi Everyone, I am having some trouble with getting my form to write the contents to csv correctly. Everything is going in to the CSV but not in the correct place. The Name, Email and Mobile fields are being displayed in a row (a1, b1, c1) but the $checkmsg options are creating new rows for every option ticked. (a2, a3, a4) I would like everything from the form to be on the one row (a1, a2, a3, a4, a5, a6) etc. Any help with this would be very appreciated. if(isset($_POST['Submit'])){ $name = $_POST['name']; $email = $_POST['email']; $mobile = $_POST['mobile']; $err = ''; foreach($_POST['check'] as $value) { $check_msg .= "Yes: $value\n"; } if(trim($name)==''){ $err .= '-Please enter a name<br>'; } if(empty($email)){ $err .= '-Please enter an email address'; } if(empty($mobile)){ $err .= '-Please enter amobile number'; } if($err!=''){ echo $err; } else{ $filename = 'file.csv'; $somecontent = $name . ',' . $email . ',' . $mobile . ',' . $check_msg . "\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } } } Many thanks in advance Link to comment https://forums.phpfreaks.com/topic/205178-php-to-csv-help-coding-included/ Share on other sites More sharing options...
jayfcf Posted June 18, 2010 Author Share Posted June 18, 2010 I managed to find the error. Link to comment https://forums.phpfreaks.com/topic/205178-php-to-csv-help-coding-included/#findComment-1073979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.