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
MarKeR
Forum Newbie
Posts: 9 Joined: Mon Aug 26, 2002 7:48 am
Post
by MarKeR » Thu Aug 29, 2002 5:47 am
Hi
I am writing a small test php script to apdate a text file i have on my server. I am having a problem with printing a success message or a failure message using else. Any advice on the code would be appreciated.
Even how the code looks or any advice on layout.
Something else, how would I make sure that the latest addition would go to the top of the txt file and not the bottom.
Thx again guys.
Code: Select all
<html>
<head>
<title>News Receive</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
function WriteToFile ($name, $email, $news) {
$TheFile = news.txt;
$OPEN = fopen ($news.txt, "a");
if ($Open) {
fWrite ($Open,"$name\t$email\t$name\n");
fclose ($open);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
return $Worked;
}
$Callfunction = WriteToFile ($Arrayї"name"], $Arrayї"email"], $Arrayї"news"]);
Print ("Successful Update -- <BR> $Array{news}\n");
} else {
Print ("Please Re-submit");
?>
</body>
</html>
phpPete
Forum Commoner
Posts: 97 Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey
Post
by phpPete » Thu Aug 29, 2002 6:02 am
Look at your spellings of $Open...your cases are mixed.
$OPEN is not $open or $Open which means you're never doing the write because you're not using the $OPEN variable.
Also, you're not using $TheFile, you're using $news.txt ???
Also...not sure why you're doing this...since WriteToFile() returns TRUE or FALSE
Code: Select all
$Callfunction = WriteToFile ($Arrayї"name"], $Arrayї"email"], $Arrayї"news"]);
Print ("Successful Update -- <BR> $Array{news}\n");
} else {
Print ("Please Re-submit");
?>
//+++++++++try this +++++++++++++
if( WriteToFile ($Arrayї"name"], $Arrayї"email"], $Arrayї"news"]))
{
Print ("Successful Update -- <BR> $Array{news}\n");
} else {
Print ("Please Re-submit");
}
?>
phpPete
Forum Commoner
Posts: 97 Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey
Post
by phpPete » Thu Aug 29, 2002 6:13 am
Something else, how would I make sure that the latest addition would go to the top of the txt file and not the bottom.
Read the entire file into a variable then concatenate that variable to the new entry then write the whole thing back to the disk.
MarKeR
Forum Newbie
Posts: 9 Joined: Mon Aug 26, 2002 7:48 am
Post
by MarKeR » Thu Aug 29, 2002 6:18 am
Hey Pete
Thx for the tips m8, sorry to bother you but i get this error when I input your code.
Parse error: parse error, unexpected $ in c:\phpdev\www\newsreceive.php on line 23
the only thing on that line is php's closing tag ?> , any suggestions.
I am quite new to php so I appreciate your patience.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Thu Aug 29, 2002 6:26 am
Try
instead of
Post the current version of the code so that we can help you with the parse error.
Mac
MarKeR
Forum Newbie
Posts: 9 Joined: Mon Aug 26, 2002 7:48 am
Post
by MarKeR » Thu Aug 29, 2002 6:42 am
Code: Select all
<?php
function WriteToFile ($name, $email, $news) {
$news = news.txt;
$OPEN = fopen ($news, "a");
if ($OPEN) {
fWrite ($OPEN,"$name\t$email\t$name\n");
fclose ($OPEN);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
return $Worked;
{
if( WriteToFile ($Arrayї"name"], $Arrayї"email"], $Arrayї"news"]))
{
Print ("Successful Update -- <BR> {$Arrayї'news']}");
} else {
Print ("Please Re-submit");
}
?>
Updated code. thx guys.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Thu Aug 29, 2002 6:46 am
Try
Code: Select all
<?php
function WriteToFile ($name, $email, $news) {
$news = 'news.txt';
$OPEN = fopen ($news, "a");
if ($OPEN) {
fWrite ($OPEN,"$name\t$email\t$name\n");
fclose ($OPEN);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
return $Worked;
}
if (WriteToFile($Arrayї'name'], $Arrayї'email'], $Arrayї'news'])) {
print 'Successful Update -- <BR> '.$Arrayї'news'];
} else {
print 'Please Re-submit';
}
?>
You had a string which wasn't enclosed by quotes:
and the parse error was being caused by the fact you didn't have a closing } for your function (it was a { instead).
Mac
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Thu Aug 29, 2002 6:48 am
btw, it might be an idea to keep all your variables lower case, it makes it easier to remember what they are and you'll make less mistakes to do with case-sensitivity.
Mac
MarKeR
Forum Newbie
Posts: 9 Joined: Mon Aug 26, 2002 7:48 am
Post
by MarKeR » Thu Aug 29, 2002 6:56 am
phpPete
Forum Commoner
Posts: 97 Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey
Post
by phpPete » Thu Aug 29, 2002 7:12 am
Glad to help....the devil is in the details!!