no extra pipe symbols written to file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

no extra pipe symbols written to file

Post by mesz »

addnews.php

Code: Select all

<?php
<? 
$fp = fopen('./news.txt','a+'); 
if (!$fp) { 
echo "Sorry could'n open file!"; 
} else { 
if($HTTP_POST_VARS['submit']) 
        $fp = fopen('./news.txt','a+'); 
        $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name']; 
        $line .= "|" . $HTTP_POST_VARS['news']; 
        $line = str_replace("\r\n","<br>",$line); 
        $line .= "\r\n"; 
        fwrite($fp, $line); 
       } 
?>
?>
view.php

Code: Select all

<?php
<?PHP 
$data = file('./news.txt'); 
$data = array_reverse($data); 
foreach($data as $element) { 
    $element = trim($element); 
    $pieces = explode("|", $element); 
    echo "<form class="form"><b>". $pieces[1] . "</b>" . $pieces[0] ; 
echo $pieces[2] .  "</form>"  ; 
} 
?> 
?>
What do I need to do to addnews.php so that extra pipe symbols are not written to my textfile ( news.txt ) every time the page is visited and exited
( whether an update has been made or not.... )

This was previously discussed with a less specific question at -
http://www.devnetwork.net/forums/viewto ... 2282#62282
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Maybe trim() and nl2br() help you!

Cheers,
Scorphus.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

cheers for the reply....
I need some more help though...
I have looked at these options before, Ireally do need a sledgehammer subtle hint:

the extra line breaks keep writing to my textfile... it ends up looking like this:

Code: Select all

||
||
01 10 03|test post|test post body contents.
<br>test contents also.
||
||
||
||
||
||
||
||
||
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Edit: I deleted the contents of this posting, because it was wrong. I seem not to be able to delete this posting, so I'll do it like this.

:roll:
Last edited by Derfel Cadarn on Wed Oct 01, 2003 10:46 am, edited 2 times in total.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Why don't you just NOT run the script unless the user has filled out the input form completely?
Like:

Code: Select all

<?php
if($HTTP_POST_VARS['submit'])
        $fp = fopen('./news.txt','a+');

       if (isset($HTTP_POST_VARS['date']) &&
             isset($HTTP_POST_VARS['name'] &&
             isset($HTTP_POST_VARS['name'])) {
                  $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
                  $line .= "|" . $HTTP_POST_VARS['news'];
                  $line = str_replace("\r\n","<br>",$line);
                  $line .= "\r\n";
                 fwrite($fp, $line);
            }
       } 
?>
I admit, it's ugly, but something like that should do it!

Edit: sorry for the posting above, but I can't delete it, it seems..
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

that's exactly what I have been after....your script has an error somewhere ( Parse error: parse error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/virtual/site130/fst/var/www/html/internal/view.php on line 19 ) but cheers for posting it. I'll have to continue thsi tommorrow- too much other work to do, but cheers.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Ah, me again...missing brackett. It should be like:

Code: Select all

<?php

if($HTTP_POST_VARS['submit'])
        $fp = fopen('./news.txt','a+');

       if (isset($HTTP_POST_VARS['date']) &&
             isset($HTTP_POST_VARS['name']) &&
             isset($HTTP_POST_VARS['name'])) {
                  $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
                  $line .= "|" . $HTTP_POST_VARS['news'];
                  $line = str_replace("\r\n","<br>",$line);
                  $line .= "\r\n";
                 fwrite($fp, $line);
            }

?>

?>
8)
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

cheers...keep watching this topic and I'll tell you tommorrow if I get it to work.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Maybe take a look to Regular Expressions on the web and Regular Expression Functions section of the PHP Manual.

I also have to take the time to learn Regular Expressions, it is very powerful and I'm sure it will solve your problem (and my own too).

Regards,
Scorphus.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Tja, regex....that would definitely be prettier than my proposal. But they're not my strongest point... actually I'm wrestling myself through them at the moment.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

If I understood correctly...

Code: Select all

if(isset($_SERVER['submit']))
// or if prefered...
if(isset($HTTP_POST_VARS['submit']))

// or if you want the submit button to be some sort of Update-button..

if(isset($_SERVER['submit']) and !empty($_SERVER['news']))
// or if prefered...
if(isset($HTTP_POST_VARS['submit']) and !empty($HTTP_POST_VARS['news']))
isset = is it set (pressed)
!empty = is it not set or ""
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Derfel Cadarn wrote:Ah, me again...missing brackett. It should be like:

Code: Select all

<?php

if($HTTP_POST_VARS['submit'])
        $fp = fopen('./news.txt','a+');

       if (isset($HTTP_POST_VARS['date']) &&
             isset($HTTP_POST_VARS['name']) &&
             isset($HTTP_POST_VARS['name'])) {
                  $line = $HTTP_POST_VARS['date'] . "|" . $HTTP_POST_VARS['name'];
                  $line .= "|" . $HTTP_POST_VARS['news'];
                  $line = str_replace("\r\n","<br>",$line);
                  $line .= "\r\n";
                 fwrite($fp, $line);
            }

?>

?>
8)
Cheers Derfel Cadarn I have used this and it works.
Cheers also Jam - I have looked at regular expressions with fear and terror before, so I think that before I slap a bit of code around, I will print off some thorough tutorials and really learn what they are about.
Cheers for all your help everybody, it is very much appreciated.
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

JAM wrote:If I understood correctly...

Code: Select all

if(isset($_SERVER['submit']))
// or if prefered...
if(isset($HTTP_POST_VARS['submit']))
Hi Jam, Actually I include the following code in all my codes to get rid of the $HTTP_POST_VARS[] etcetera:

Code: Select all

<?php

// Backward compatible array creation. After this point, the
// PHP 4.1.0+ arrays can be used to access variables coming
// from outside PHP. But it should be noted that these variables
// are not necessarily superglobals, so they need to be global-ed!
if (!isset($_SERVER)) {
	$_GET	= &$HTTP_GET_VARS;
	$_POST	= &$HTTP_POST_VARS;
	$_ENV		= &$HTTP_ENV_VARS;
	$_SERVER	= &$HTTP_SERVER_VARS;
	$_COOKIE	= &$HTTP_COOKIE_VARS;
	$_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
}

?>
This way I can use the "new" code, whish is shorter and easier to me and I don't have to think about what PHP-version is on the server. But I didn't want to bother mesz with it...

Cheers!
Post Reply