Jump to content

Text file trouble


Tandem

Recommended Posts

*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:
[code]$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);
[/code]

and here is what i get when i submit the news:
[quote]07.18.06|Tandem||
07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

[/quote]

What i want is:
The date, the posters name, the news, the title, and then a new line. Like this:
[quote]07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

[/quote]

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

Thanks in advance

-Tandem
Link to comment
Share on other sites

[quote author=Tandem link=topic=101013.msg399331#msg399331 date=1153245494]
Ahhh, i've already fixed that, so the problem doesn't have anything to do with that. Thanks for the advice.
[/quote]
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.

Link to comment
Share on other sites

I thought the line of code was the only relevant piece....
Here is everything that i think could possibly be relevant to this:
[code]<?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>
[/code]

When i correctly submit the form this is the output to the text file: (The text file is empty before the submittance)
[quote]07.18.06|Tandem||
07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

[/quote]
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:
[quote]07.18.06|Tandem|This is the actual news story/update.|NEWS TITLE

[/quote]
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.
Link to comment
Share on other sites

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 [color=red]submitted[/color]!

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:
[code]
<?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');
//...
?>
[/code]

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 />
Link to comment
Share on other sites

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:
[code]<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;
}  [/code]

And here's the code on the page which the news is displayed from and that i want to delete from:
[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;
}
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';
    }

?>[/code]

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

Link to comment
Share on other sites

[code]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';
    }[/code]

Should be:

[code]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);
}[/code]

You also never stated what exactly the problem was...just what you wanted to do.
Link to comment
Share on other sites

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:

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

?>[/code]

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.
Link to comment
Share on other sites

Try this:

[code]<?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 />';
}
?>[/code]

I didn't notice you didn't have an id field, you were using the position in the array.
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.