Page 1 of 1
no extra pipe symbols written to file
Posted: Wed Oct 01, 2003 9:41 am
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
Posted: Wed Oct 01, 2003 10:16 am
by scorphus
Maybe
trim() and
nl2br() help you!
Cheers,
Scorphus.
Posted: Wed Oct 01, 2003 10:31 am
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.
||
||
||
||
||
||
||
||
||
Posted: Wed Oct 01, 2003 10:40 am
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.

Posted: Wed Oct 01, 2003 10:41 am
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..
Posted: Wed Oct 01, 2003 10:52 am
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.
Posted: Wed Oct 01, 2003 11:00 am
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);
}
?>
?>

Posted: Wed Oct 01, 2003 11:09 am
by mesz
cheers...keep watching this topic and I'll tell you tommorrow if I get it to work.
Posted: Wed Oct 01, 2003 11:16 am
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.
Posted: Wed Oct 01, 2003 11:55 am
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.
Posted: Wed Oct 01, 2003 6:43 pm
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 ""
Posted: Thu Oct 02, 2003 2:16 am
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);
}
?>
?>

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.
Posted: Thu Oct 02, 2003 3:30 am
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!