Problems with else.

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
MarKeR
Forum Newbie
Posts: 9
Joined: Mon Aug 26, 2002 7:48 am

Problems with else.

Post 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>
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post 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; 
?>
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post 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.
MarKeR
Forum Newbie
Posts: 9
Joined: Mon Aug 26, 2002 7:48 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
MarKeR
Forum Newbie
Posts: 9
Joined: Mon Aug 26, 2002 7:48 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
MarKeR
Forum Newbie
Posts: 9
Joined: Mon Aug 26, 2002 7:48 am

Thx

Post 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
User avatar
phpPete
Forum Commoner
Posts: 97
Joined: Sun Aug 18, 2002 4:40 pm
Location: New Jersey

Post by phpPete »

Glad to help....the devil is in the details!!
Post Reply