php_beginner_83 Posted June 14, 2009 Share Posted June 14, 2009 Hi All PHP is refusing to cooperate with me and I have no clue why I'm trying to do a simple comparison using an IF statement and display an html page is the IF statement is true. the first part of my code is (this comes from America.php).. <td><img src="images/sanFranThumb.jpg" align="middle"/></td> <td><?php echo "<a href=scriptA.php?place=1>"; ?> <h3>San Francisco</h3></a> </td> This is then scriptA.php... <?php session_start(); $temp = $_GET['place']; $_SESSION['placeValue'] = $temp; header('Location: http://localhost/Website Templates/America1.php'); ?> And the following in America1.php is where the problem is.. <?php $placeValue = $_SESSION['placeValue']; if ($placeValue == 1) { $content = 'SanFranContent.htm'; } if ($placeValue == 2) { $content = 'NewYorkContent.htm'; } if ($placeValue == 3) { $content = 'MemphisContent.htm'; } ?> I do use session_start(); at the start of the page. However, none of these IF statments work. I've printed out the value of $placeValue to screen and it is the correct value but I don't understand why the IF's dont catch the value. Depending on the value of $placeValue, a different HTML page is displayed. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2009 Share Posted June 14, 2009 Have you echoed $content after that point to see what is in it and how is $content being used to cause the different HTML page to be displayed? Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855762 Share on other sites More sharing options...
papaface Posted June 14, 2009 Share Posted June 14, 2009 I can't see session_start(); in America1.php even though you say you have it in the pages... Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855765 Share on other sites More sharing options...
wildteen88 Posted June 14, 2009 Share Posted June 14, 2009 Is anything being displayed in America1.php? Have you made sure session_start() is the first line in Americal1.php Your if statements are fine, however it'll be better for to use an if/elseif statement or even a switch Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855766 Share on other sites More sharing options...
ToonMariner Posted June 14, 2009 Share Posted June 14, 2009 just before your 1st if statement echo out the value of $placeValue - echoing out this kind of stuff really helps in debugging... For future reference in cases where you are looking at one parameter having many different values and do something different in various cases have a look at the switch statement in php. Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855767 Share on other sites More sharing options...
php_beginner_83 Posted June 14, 2009 Author Share Posted June 14, 2009 Yeah I've definitely used session_start(); its at the start of the code. I just showed the code I thought was relevant. I've echoed $content and its blank. the rest of the webpage is displayed fine it's just the content section. This is the code for America1.php. <?php //$getValue = $_GET['place']; session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Language" content="zh"> <title>My Travel Website</title> <link href="americanStyle.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div id="wrapper"> <div id="americaHeader"> <div id="logo"> <h1><a href="#">My American Adventures</a></h1> </div> </div> <!-- end #header --> <div id="menu"> <ul> <li><a href="#">Home</a></li> <li><?php echo"<a href=America1.php?page=photo>"; ?>Photos</a></li> <li><a href="#">Sights</a></li> <li><a href="#">Dos & Donts</a></li> </ul> </div> <div id="page"> <div id="content"> <?php $placeValue = $_SESSION['placeValue']; if ($placeValue == 1) { $content = 'SanFranContent.htm'; } elseif ($placeValue == 2) { $content = 'NewYorkContent.htm'; } elseif ($placeValue === 3) { $content = 'MemphisContent.htm'; } elseif ($placeValue == 4) { $content = 'NashvilleContent.htm'; } else { echo "no content </br>"; } // you build an array for the pages you have. $index = array( "main" => $content,"photo" => "DisplayAlbums.php" ); if ( !empty( $_GET["page"] ) ) { $page = $_GET["page"]; } if ( array_key_exists( $page , $index ) ) { include( $index[$page] ); } else{ include( $index["main"] ); //if there isn't a selected page... } ?> </div> </div> </div> </body> </html> Right now it'll display SanFranContent.htm, when the user clicks the San Francisco link on America.php. I also have another link on America.php, Memphis. When this link is clicked, the value that is passed to scriptA.php changes... e.g. <?php echo "<a href=scriptA.php?place=3>"; ?><h3>Memphis</h3></a> In America1.php, I've echoed this value (it's passed to scriptA.php and stored in $_SESSION['placeValue']) and it prints out fine. However, it isn't caught by the IF statements. When the user clicks the San Francisco link it works, it doesn't work for the Memphis link. I'm well confused! Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855829 Share on other sites More sharing options...
php_beginner_83 Posted June 14, 2009 Author Share Posted June 14, 2009 I posted that last reply before I tried the switch() statement...and would you believe it....ITS WORKED!!! Thank you everyone that offered some help, much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855830 Share on other sites More sharing options...
ToonMariner Posted June 14, 2009 Share Posted June 14, 2009 if the value is blank then it means the session variable used is blank/not set. Quote Link to comment https://forums.phpfreaks.com/topic/162162-solved-if-statements-wont-work/#findComment-855833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.