Jump to content

text file drama :o


Tandem

Recommended Posts

The following lines are writing entries into a text file.

[code]$line = date("m.d.y") . "|" . $poster . "|" . $_POST['news'];
fwrite($fp, $line);
[/code]

The problem is i'm getting this output in the text file:
[quote]
07.18.06|Tandem|07.18.06|Tandem|Some sort of message
[/quote]

The date and my name are being written into the text file twice and i don't know why.

I guess it doesn't really matter that much as i can work around it if need be, but for the sake of cleanliness i'd like to fix it, so i'd be grateful if somebody could point out the reason.

Thanks in advance

-Tandem
Link to comment
Share on other sites

[code]if (empty($_POST['news'])){
if (!empty($_POST['submit'])){
echo '<font face="verdana" size="1px" color="red">*You did not enter any news into the form!<br /></font>' . "\n";
exit;
}
}
if(strstr($_POST['news'],"|")) {
echo '<font face="verdana" size="1px" color="red">*News cannot contain the pipe symbol - " | "<br /></font>' . "\n";
exit();       
}
?>
<table width="100%" height="100%" cellspacing="10%">
<tr><td align="left">
<table cellspacing="0">
<tr><td class="tablehead" align="center">NEWS</td></tr>
<tr><td class="main" align="center">
<?php
$poster_check = $_POST['poster'];
$poster_check = mysql_query("SELECT USERNAME FROM USERS WHERE USERID='$_SESSION[userid]'");
$poster = mysql_result($poster_check,0);

$fp = fopen('news.txt','a');
if(!$fp) {
echo '<font face="verdana" size="1px" color="red">*Error Opening file!<br /></font>' . "\n";
    exit();
}
       
$line = date("m.d.y") . "|" . $poster . "|" . $_POST['news'] . "|" . $newstitle;
fwrite($fp, $line);
     
if(!fclose($fp)) {
echo "Error closing file!";
exit;


?>
[/code]

This is eveything that corresponds to the process.
Link to comment
Share on other sites

You don't show your complete code related to this.

I assume you're writing this out in a loop and not accidentally calling this twice. This might be valid data and it just happens to be two lines of data!

Depending on your operating system, you need to add a carriage returns (\r) or newlines (\n) or both (\r\n) to the end of the line to separate each lines output. Example:

$line = date("m.d.y") . "|" . $poster . "|" . $_POST['news'] . "\n";


EDIT:

I notice now you posted your code. Anyway, since you're opening the file with "a" (appending), my response still applies about adding a line ending to each line of data.
Link to comment
Share on other sites

Your code appears fine there.

However, you are not seperating the news stories from eachother...you may want to change your $line to be:

[code]$line = date("m.d.y") . "|" . $poster . "|" . $_POST['news'] . "|" . $newstitle . "\n";[/code]

So that each story will be on a new line.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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