Jump to content

php to file


snoopy1992

Recommended Posts

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);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/203593-php-to-file/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/203593-php-to-file/#findComment-1066464
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/203593-php-to-file/#findComment-1066465
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.