Jump to content


Photo

Text file trouble


  • Please log in to reply
11 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 18 July 2006 - 05:47 PM

*PLEASE SKIP STRAIGHT REPLY 7! - IGNORE THE ORIGINAL POST*

I posted about this last night, but i seem to have run into more trouble with this.

I have a news input section on the admin page i am making. The news i submit is saved to a text file, and then the news page reads the news from it. I'm having a problem with inputting the news and the details of it, into the text file.

Here is my code for inputting into the text file:
$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'] . "|" . $_POST['newstitle'] . "\n";
fwrite($fp, $line);


and here is what i get when i submit the news:

07.18.06|Tandem||
07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE


What i want is:
The date, the posters name, the news, the title, and then a new line. Like this:

07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE


But i just can't figure it out. Any help would be appreciated.

Thanks in advance

-Tandem


#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 18 July 2006 - 05:53 PM

Problem: $_PST['newstitle']

While testing and debugging your code it's helpful to have error_reporting(E_ALL) set. You would get a notice stating that $_PST is an undefined variable giving you a clue to your problem.



#3 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 18 July 2006 - 05:58 PM

Ahhh, i've already fixed that, so the problem doesn't have anything to do with that. Thanks for the advice.

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 18 July 2006 - 06:07 PM

Ahhh, i've already fixed that, so the problem doesn't have anything to do with that. Thanks for the advice.

Whenever possible post the exact most complete or relevant code so members on this forum can help you better. You really should just have continued with your other existing topic. Anyway, state more precisely what's wrong - is it that sometimes you get the correct output and sometimes you don't or what?

It's not clear by the little code what's wrong since we don't see what the HTML form variable are exactly.

Also, I recommend while testing that you empty out the output file before each test or use 'w' in the fopen(). That way, you know what each test really produced in the file.



#5 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 18 July 2006 - 06:16 PM

I thought the line of code was the only relevant piece....
Here is everything that i think could possibly be relevant to this:
<?php
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'] . "|" . $_POST['newstitle'] . "\n";
fwrite($fp, $line);
       
if(!fclose($fp)) {
echo "Error closing file!";
exit;
}   

?>
<form action="administrator_tools.php" method="POST" name="newsentry">
<input type="hidden" name="poster" value="<?$poster?>"><br />
News Title:<br /><input type="text" name="newstitle" value=""><br />
The News:<br />
<textarea name="news" cols="40" rows="5"></textarea><br />
<input type="submit" name="submit" value="Post it!"><br />
</form>  
</td></tr>
</table>
</td></tr>
</table>
<br />
</td></tr>
</table>

When i correctly submit the form this is the output to the text file: (The text file is empty before the submittance)

07.18.06|Tandem||
07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

The date, the name of poster, an empty space then a newline, then the date and poster again, then the news, then the news title, then another newline.


What i want is:

07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

The date, the poster, the news, the news title and then a newline.

Sorry for not using the other topic, i'll try not to post twice about the same thing in future.

#6 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 18 July 2006 - 06:58 PM

You're getting two lines because the first time the page is loaded you're having it write to the file, and fields don't have a value and that's why they're blank in the file. Then when the form is posted, you write to the file again and this time the fields are populated with data.

So, you only should write to the file when the form has been submitted!

Also, you don't have proper MySQL error checking. Always check for any possibility of errors and don't assume data is being returned. Example:
<?php

$poster_check = $_POST['poster'];  // Not used

$sql = "SELECT `USERNAME` FROM `USERS` WHERE `USERID`='{$_SESSION['userid']}'";
$result = mysql_query($sql);
if (!$result) {
    // For debugging
    echo '<font face="verdana" size="1px" color="red">*Error on query! SQL: ', $sql,
         ' Error: ', mysql_error(), '<br/></font>', "\n";
    exit;
}
$row = mysql_fetch_assoc($result);
if (!$row) {
    // For debugging
    echo '<font face="verdana" size="1px" color="red">*Could not find userid: ',
         $_SESSION['userid'], '<br/></font>', "\n";
    exit;
}

$poster = $row['USERNAME'];  // At this point you know the data was retrieved

$fp = fopen('news.txt','a');
//...
?>

Change this line since you're not really echoing the value:
<input type="hidden" name="poster" value="<?$poster?>"><br />

to something like this:
<input type="hidden" name="poster" value="<?PHP echo empty($poster) ? '' : htmlentities($poster); ?>"><br />


#7 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 18 July 2006 - 07:00 PM

Awesome, thankyou very much.

#8 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 19 July 2006 - 02:06 AM

Ok, i've got a new problem. I'm trying to add the ability to delete news items to people of a certain user lever, eg. Admins or moderators.

I used a tutorial, but i edited it to suit my needs. Here's the all the relevant code:
Here's the code on the page i add the news from:
<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();
 }
if (!empty($_POST['submit'])){
$line = date("m.d.y") . "|" . $poster . "|" . $_POST['news'] . "|" . $_POST['newstitle'] . "\n";
fwrite($fp, $line);
}
       
if(!fclose($fp)) {
echo "Error closing file!";
exit;
} 

And here's the code on the page which the news is displayed from and that i want to delete from:
<?php
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $key => $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo <<<HERE
<table align="center" cellspacing="0" cellpadding="2%" width="100%">
<tr><td class="tablehead" cellpadding="0"> $pieces[3]
HERE;
if ($user_level_result > 1){
echo <<<HERE
<div align="right"><a href="$PHP_SELF?action=delete&id=$key">Delete</a>&nbsp;</div>
HERE;
}
echo <<<HERE
</td></tr> 
<tr><td class="news" align="left">$pieces[2]</td></tr>
<tr><td class="tablehead3" align="right"><b>Posted by   <a href="">$pieces[1]</a>   on   $pieces[0]  </b>&nbsp;
</td></tr></table><br /><br />
HERE;
}
if($action == "delete") {
        $data = file('news.txt');
        //this next line will remove the single news item from the array
        array_splice($data,$id,1);
        $fp = fopen('news.txt','w');
        foreach($data as $element) {
            fwrite($fp, $element);
        }
        fclose($fp);    
        echo '<font face="verdana" size="1px" color="red">*Item Deleted<br /></font>' . "\n";    
include 'news.php';
    }

?>

I want anyone with a user_level over 1 to be able to click the delete link, and that news item to be deleted.

Thanks in advance for any help

-Tandem



#9 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 July 2006 - 03:04 AM

if($action == "delete") {
        $data = file('news.txt');
        //this next line will remove the single news item from the array
        array_splice($data,$id,1);
        $fp = fopen('news.txt','w');
        foreach($data as $element) {
            fwrite($fp, $element);
        }
        fclose($fp);    
        echo '<font face="verdana" size="1px" color="red">*Item Deleted<br /></font>' . "\n";    
include 'news.php';
    }

Should be:

if($action == "delete") {
	$data = file('news.txt');
	foreach ($data as $line) {
		$line = explode("|",$line);
		if ($line[0] != $_GET['id']) {
			$newfile[] = implode("|",$line);
		}
	}
	
	$open = fopen("news.txt", 'w');
	fwrite($open, implode("", $newfile));
	fclose($open);
}

You also never stated what exactly the problem was...just what you wanted to do.

#10 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 19 July 2006 - 06:39 PM

Sorry about that, here goes again, since the problem still isn't fixed.

The problem is that when i click the delete link, nothing happens. The browser goes to the right place (i think)e.g /News.php?action=delete&id=0, but it just does nothing. Here's all the code:

<?php
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $key => $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo <<<HERE
<table align="center" cellspacing="0" cellpadding="2%" width="100%">
<tr><td class="tablehead" cellpadding="0"> $pieces[3]
HERE;
if ($user_level_result > 1){
echo <<<HERE
<div align="right"><a href="$PHP_SELF?action=delete&id=$key">Delete</a>&nbsp;</div>
HERE;
}
$pieces[2] = stripslashes($pieces[2]);
echo <<<HERE
</td></tr> 
<tr><td class="news" align="left">$pieces[2]</td></tr>
<tr><td class="tablehead3" align="right"><b>Posted by   <a href="">$pieces[1]</a>   on   $pieces[0]  </b>&nbsp;
</td></tr></table><br /><br />
HERE;
}
if($action == "delete") {
	$data = file('news.txt');
	foreach ($data as $line) {
		$line = explode("|",$line);
		if ($line[0] != $_GET['id']) {
			$newfile[] = implode("|",$line);
		}
	}
	
	$open = fopen("news.txt", 'w');
	fwrite($open, implode("", $newfile));
	fclose($open);
}

?>

Any help to get it working is appreciated. And if there's something i've forgot to mention or something, just say and i'll post it.

#11 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 19 July 2006 - 07:28 PM

Bump

#12 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 July 2006 - 11:00 PM

Try this:

<?php
if($action == "delete") {
	$data = file('news.txt');
	$id = $_GET['id'];
	unset($data[$id]);

	$open = fopen("news.txt", 'w');
	fwrite($open, implode("", $data));
	fclose($open);
}

$data = file('news.txt');

foreach($data as $key => $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
	echo '
	<table align="center" cellspacing="0" cellpadding="3px" width="50%" style="border: 1px solid black;">
		<tr>
			<td class="tablehead" cellpadding="0">' . $pieces[3] . '
				<div align="right"><a href="' . $PHP_SELF . '?action=delete&id=' . $key . '">Delete</a>&nbsp;</div>
			</td>
		</tr> 
		<tr>
			<td class="news" align="left">' . stripslashes($pieces[2]) . '</td>
		</tr>
		<tr>
			<td class="tablehead3" align="right"><b>Posted by   <a href="">' . $pieces[1] . '</a>   on   ' . $pieces[0] . '  </b>&nbsp;</td>
		</tr>
	</table>
	<br /><br />';
}
?>

I didn't notice you didn't have an id field, you were using the position in the array.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users