Page 1 of 1

Problems with else.

Posted: Thu Aug 29, 2002 5:47 am
by MarKeR
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) &#123;
	 $TheFile = news.txt;
	 $OPEN = fopen ($news.txt, "a");
              if ($Open) &#123;
			  fWrite ($Open,"$name\t$email\t$name\n");
			  fclose ($open);
			  
			  $Worked = TRUE;
    &#125; else &#123;
			  $Worked = FALSE;
			  &#125;
			  return $Worked;
	&#125;
	$Callfunction = WriteToFile ($Array&#1111;"name"], $Array&#1111;"email"], $Array&#1111;"news"]);
			  Print ("Successful Update -- <BR> $Array&#123;news&#125;\n"); 
	    		  
	&#125; else &#123;
	 
			  Print ("Please Re-submit"); 
?>		  
</body>
</html>

Posted: Thu Aug 29, 2002 6:02 am
by phpPete
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&#1111;"name"], $Array&#1111;"email"], $Array&#1111;"news"]); 
           Print ("Successful Update -- <BR> $Array&#123;news&#125;\n"); 
               
   &#125; else &#123; 
    
           Print ("Please Re-submit"); 
?>        


//+++++++++try this  +++++++++++++
if( WriteToFile ($Array&#1111;"name"], $Array&#1111;"email"], $Array&#1111;"news"])) 
  &#123;
     Print ("Successful Update -- <BR> $Array&#123;news&#125;\n"); 
               
   &#125; else &#123; 
    
           Print ("Please Re-submit");
&#125; 
?>

Posted: Thu Aug 29, 2002 6:13 am
by phpPete
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.

Posted: Thu Aug 29, 2002 6:18 am
by MarKeR
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.

Posted: Thu Aug 29, 2002 6:26 am
by twigletmac
Try

Code: Select all

&#123;$Array&#1111;'news']&#125;
instead of

Code: Select all

$Array&#123;news&#125;
Post the current version of the code so that we can help you with the parse error.

Mac

Posted: Thu Aug 29, 2002 6:42 am
by MarKeR

Code: Select all

<?php
   
   function  WriteToFile ($name, $email, $news) &#123;
	 $news = news.txt;
	 $OPEN = fopen ($news, "a");
              if ($OPEN) &#123;
			  fWrite ($OPEN,"$name\t$email\t$name\n");
			  fclose ($OPEN);
			  
			  $Worked = TRUE;
    &#125; else &#123;
			  $Worked = FALSE;
			  &#125;
			  return $Worked;
	&#123;
	if( WriteToFile ($Array&#1111;"name"], $Array&#1111;"email"], $Array&#1111;"news"])) 
  &#123; 
     Print ("Successful Update -- <BR> &#123;$Array&#1111;'news']&#125;"); 
                
   &#125; else &#123; 
    
           Print ("Please Re-submit"); 
&#125; 
 
?>
Updated code. thx guys.

Posted: Thu Aug 29, 2002 6:46 am
by twigletmac
Try

Code: Select all

<?php 
    
function  WriteToFile ($name, $email, $news) &#123; 
	$news = 'news.txt'; 
	$OPEN = fopen ($news, "a"); 
	if ($OPEN) &#123; 
		fWrite ($OPEN,"$name\t$email\t$name\n"); 
		fclose ($OPEN); 
		$Worked = TRUE; 
	&#125; else &#123; 
		$Worked = FALSE; 
	&#125; 
	return $Worked;
&#125;
 
if (WriteToFile($Array&#1111;'name'], $Array&#1111;'email'], $Array&#1111;'news'])) &#123; 
	print 'Successful Update -- <BR> '.$Array&#1111;'news'];
&#125; else &#123; 
	print 'Please Re-submit'; 
&#125; 

?>
You had a string which wasn't enclosed by quotes:

Code: Select all

$news = news.txt;
and the parse error was being caused by the fact you didn't have a closing } for your function (it was a { instead).

Mac

Posted: Thu Aug 29, 2002 6:48 am
by twigletmac
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

Thx

Posted: Thu Aug 29, 2002 6:56 am
by MarKeR
Just wanted to say thx for your help guys, amazing how one stupid thing just messe's you up, starting to see the bigger picture now thx toyou all.

:D :D :D

Posted: Thu Aug 29, 2002 7:12 am
by phpPete
Glad to help....the devil is in the details!!