snoopy1992 Posted June 2, 2010 Share Posted June 2, 2010 I am saving to a text file using the fwright command, all the info saves ok but how can I get the data in the text file to have a space in-between each variable <table width="590" border="1"> <tr> <td width="129">Name </td> <td width="335"><?php echo $_POST["name"]; ?></td> </tr> <tr> <td>Account Number</td> <td><?php echo $_POST["accno"]; ?></td> </tr> <tr> <td>Date and Time</td> <td><?php echo $_POST["dt"]; ?></td> </tr> <tr> <td height="45">Lengh of Visit </td> <td><?php echo $_REQUEST["lvisit"]; ?></td> </tr> <td height="45">Reason For call</td> <td><?php echo $_REQUEST["rcall"]; ?></td> </tr> </tr> <td height="45">Cost of Extra Medication</td> <td><?php echo $_REQUEST["com"]; ?></td> </tr> </table> <br /> <?php $name=$_REQUEST['name']; $accno=$_REQUEST['accno']; $dt=$_REQUEST['dt']; $lvisit =$_REQUEST['lvisit']; $rcall =$_REQUEST['rcall']; $com =$_REQUEST['com']; $fp = fopen("data.txt", 'ab'); fwrite($fp,$name); fwrite($fp,$accno); fwrite($fp,$dt); fwrite($fp,$lvisit); fwrite($fp,$rcall); fwrite($fp,$com); fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/203593-php-to-file/ Share on other sites More sharing options...
kenrbnsn Posted June 2, 2010 Share Posted June 2, 2010 Put a space after each variable: <?php fwrite($fp,$name . ' '); fwrite($fp,$accno . ' '); fwrite($fp,$dt . ' '); fwrite($fp,$lvisit . ' '); fwrite($fp,$rcall . ' '); fwrite($fp,$com . ' '); ?> You can also use one fwrite: <?php fwrite($fp,"$name $accno $dt $lvisit $rcall $com"); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/203593-php-to-file/#findComment-1066464 Share on other sites More sharing options...
dabaR Posted June 2, 2010 Share Posted June 2, 2010 UGH, that looks super nasty. What if the user enters a space into one of the fields then? How about using fputcsv instead? http://php.net/manual/en/function.fputcsv.php You would then have something like: ... $fp = fopen("data.txt", 'ab'); fputcsv($fp, array($name, $accno, $dt, $lvisit, $rcall, $com)); fclose($fp); ?> However, if the user enters a double-quote ", then you are in the same predicament later when reading the data as if you had a space in the data, and a space was a delimiter. You could use http://ca3.php.net/manual/en/function.addslashes.php on each of the variables you are storing in the file, and http://ca3.php.net/manual/en/function.stripslashes.php on each field when you are reading the data. Hope that helps! -- Dan Bernardic http://dan.bernardic.ca Quote Link to comment https://forums.phpfreaks.com/topic/203593-php-to-file/#findComment-1066465 Share on other sites More sharing options...
snoopy1992 Posted June 2, 2010 Author Share Posted June 2, 2010 It works!! Thank You :D Quote Link to comment https://forums.phpfreaks.com/topic/203593-php-to-file/#findComment-1066535 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.