Jump to content

Archived

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

Tandem

text file drama :o

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

Share this post


Link to post
Share on other sites
You need to post more of your code, cause apparently you are writing to the file twice at some point and not realizing it.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
try that ok

$line = date("m.d.y") . "|" . $poster . "|" . $news . "|" . $newstitle;
fwrite($fp, $line);

Share this post


Link to post
Share on other sites
Yeah, it appears it wasn't doing it twice, i just didn't have it so that my posts where on new lines, and that new features were being added as, making it look like they were posted twice. Thanks for the help people!

Share this post


Link to post
Share on other sites

×

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.