Jump to content

Archived

This topic is now archived and is closed to further replies.

Perad

Trouble using if and switch statements

Recommended Posts

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

On the main page..
[code]$IsLoggedIn = 0[/code]

When the user logs in, this is set to 1
[code]$IsLoggedIn = 1[/code]

I know this works because the post comment area appears and disappears depending if the user is logged in.
[code]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';
}
}[/code]

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.

[code] /* 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();
}

}[/code]

thx

Perad

Share this post


Link to post
Share on other sites
Gah, hadn't defined the $IsLoggedIn variable in the function..

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.