Azaqi
Azaqi

Reputation: 125

When writing to file, not moving to new line PHP

I have 2 files called sellcar.php and interest.php. Users can submit their car details for sale in sellcar.php, and it will get written to a txt file. If buyers are interested, they can submit their details in interest.php, and it will get sent to another txt file. For sellcar.php, everytime there is a new entry, it goes to a new line, but for interest.php, it does not. The code is very similar, so I'm not sure what's happening.

sellcar.php

<?php
if(isset($_POST['submit']))
{
    $fname =$_POST['fname'];
    $lname=$_POST['lname'];
    $phone=$_POST['phone'];
    $email=$_POST['email'];
    $platenum=$_POST['platenum'];
    $model=$_POST['model'];
    $caryear=$_POST['caryear'];
    $desc=$_POST['desc'];
    $travel=$_POST['travel'];
    $owner=$_POST['owner'];
    $repair=$_POST['repair'];
    $file = fopen("CarDirectory.txt","a+");
    fwrite($file,$fname . ',');
    fwrite($file,$lname . ',');
    fwrite($file,$phone . ',');
    fwrite($file,$email . ',');
    fwrite($file,$platenum . ',');
    fwrite($file,$model . ',');
    fwrite($file,$caryear . ',');
    fwrite($file, $desc . ',');
    fwrite($file,$travel . ',');
    fwrite($file,$owner . ',');
    fwrite($file,$repair . "\n");
    print_r(error_get_last());
    fclose($file);
    header("Location:mainmenu.php");
}
?>

interest.php

    <?php
if(isset($_POST['submit']))
{
    $fname =$_POST['fname'];
    $lname=$_POST['lname'];
    $phone=$_POST['phone'];
    $platenum=$_POST['platenum'];
    $price=$_POST['price'];
    $file = fopen("BuyerInterest.txt","a+");
    fwrite($file,$fname . ',');
    fwrite($file,$lname . ',');
    fwrite($file,$phone . ',');
    fwrite($file,$platenum . ',');
    fwrite($file,$price . "\n");
    print_r(error_get_last());
    fclose($file);
}
?>

Upvotes: 0

Views: 82

Answers (2)

prod3v3loper
prod3v3loper

Reputation: 124

PHP has defined constants like DIRECTORY_SEPERATOR or PHP_EOL (End Of Line - cross platform)

http://php.net/manual/en/dir.constants.php

http://php.net/manual/en/reserved.constants.php

My first hit in google

Line break not working when writing to text file in PHP

This is called Invariance and Covariance. If you Google it, you’ll find tutorials that can explain it much better than we can in an answer here.

Upvotes: 0

Srdjan
Srdjan

Reputation: 582

Try using PHP_EOL isntead of "\n".

Example: fwrite($file,$price . PHP_EOL);

The purpose of PHP_EOL is to automatically choose the correct new line character for the platform you are using.

Upvotes: 4

Related Questions