Jump to content


Photo

Trouble using if and switch statements


  • This topic is locked This topic is locked
2 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 20 October 2006 - 10:03 AM

I am tiding up my code and i realised i could use the same script to do 2 functions.

On the main page..
$IsLoggedIn = 0

When the user logs in, this is set to 1
$IsLoggedIn = 1

I know this works because the post comment area appears and disappears depending if the user is logged in.
if ($IsLoggedIn == 1) {	
						/* add a form where users can enter new comments */
						echo "<FORM action=\"{$_SERVER['PHP_SELF']}" .
							 "?action=addcomment&id=$id\" method=POST>\n";
						echo "<p>You are posting as " .$username. "<br />";
						echo "<TEXTAREA cols=\"40\" rows=\"5\" " .
							 "name=\"comment\"></TEXTAREA><BR>\n";
						echo "<input type=\"submit\" name=\"submit\" " .
							 "value=\"Add Comment\"\n";
						echo "</FORM>\n";
					} else {
						echo 'Please log-in to post a comment';
					}
			}

So why doesn't my switch statement work? No matter whether the user is logged in or not, the "else" part is always displayed. Could someone help me fix this please. The switch statement is below.

/* this is where the script decides what do do */
			if ($IsLoggedIn == 1) {	
			
					echo "<CENTER>\n";
					switch($_GET['action']) {
						case 'show':
							displayOneItem($_GET['id']);
							break;
						case 'all':
							displayNews(1);
							break;
						case 'addcomment':
							addComment($_GET['id']);
							break;
						default:
							displayNews();
					}
					
			} else {
			
					echo "<CENTER>\n";
					switch($_GET['action']) {
						case 'show':
							displayOneItem($_GET['id']);
							break;
						case 'all':
							displayNews(1);
							break;
						default:
							displayNews();
					}
					
			}

thx

Perad

#2 vbnullchar

vbnullchar
  • Members
  • PipPipPip
  • Advanced Member
  • 428 posts
  • LocationPasig City, Philippines

Posted 20 October 2006 - 10:45 AM

try this

switch ($_POST['action']) {
...
...
}
Registered Linux User #399942
Ubuntu User #14134
--
my blog

#3 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 20 October 2006 - 11:12 AM

Gah, hadn't defined the $IsLoggedIn variable in the function..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users