Jump to content

[SOLVED] IF statements won't work


php_beginner_83

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.