Jump to content

If Else Statement Not Functioning Properly


himynameismark

Recommended Posts

I have a client who wants a website done for her acting and modeling portfolio similar to a friend of hers. I am currently trying to make a mock layout for her until I can get more information from her like what pages she needs and if she likes any of my ideas or my partner's better than what her friend's site has. His site is pretty basic, so we are going to work on making hers looks a little more professional. However, I am very new to PHP. I am trying my best to get some things to work for her that I am learning as I go with the PHP scripts.I have a header, footer, and a vertical navigation menu that I am trying to set up in PHP for her, so I can easily go through and change pages once I get what she needs. I haven't set up the footer, but I know I will have no issues getting that to work. However, I am having some issues getting the header and navigation menu scripts to function properly.I will start with the header. The point of this script is to display the website name (which will be static once we get her domain name set up - but this part works anyway), then the name of the page beside it in a smaller font and in a different color. Here is the script, and I will explain what does and does not work.

<div id="headerInfo">	<?php		//Get Domain Name		$domain = $_SERVER['SERVER_NAME'];			//Get PHP File Name		$page = $_SERVER['PHP_SELF'];	?>		<!-- Domain Name -->	<span id="title">		<?php 			echo $domain;		?>	</span>		<!-- Page Name -->	<span id="page">		<?php			if ($page = "index.php" || $page = "index")				echo "HOME PAGE";			elseif ($page = "headshots.php" || $page = "headshots")				echo "HEADSHOTS";			elseif ($page = "resume.php" || $page = "resume")				echo "RESUME";			elseif ($page = "demoreel.php" || $page = "demoreel")				echo "DEMO REEL";			elseif ($page = "news.php" || $page = "news")				echo "NEWS";			elseif ($page = "photos.php" || $page = "photos")				echo "PHOTOS";			elseif ($page = "contact.php" || $page = "contact")				echo "CONTACT ME";			elseif ($page = "performance.php" || $page = "performance")				echo "PERFORMANCE PHOTOS";						else								echo "";				 ?>		  </span></div>

What works: This script displays the website name, and initially displays the HOME PAGE name.What doesn't work: Once you click another link, and it reloads the page, the new page name does not display. Instead, it continues to say HOME PAGE up at the top. Pretty much, once the site initially loads, it is a static header.Now, the navigation menu. Here is the code I have for it.

<!-- Find Current Page --><?php $page = $_SERVER['PHP_SELF']; ?><!-- Show $page --><?php echo $page; ?><ul>	<li>		<?php			if ($page == "/TestSite/index.php" || $page == "/TestSite/index")				echo "<span class=current>HOME PAGE</span>";			else				echo "<a href=index.php>HOME PAGE</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/headshots.php" || $page == "/TestSite/headshots")				echo "<span class=current>HEADSHOTS</span>";			else				echo "<a href=headshots.php>HEADSHOTS</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/resume.php" || $page = "/TestSite/resume")				echo "<span class=current>RESUME</span>";			else				echo "<a href=resume.php>RESUME</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/demoreel.php" || $page == "/TestSite/demoreel")				echo "<span class=current>DEMO REEL</span>";			else				echo "<a href=demoreel.php>DEMO REEL</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/news.php" || $page == "/TestSite/news")				echo "<span class=current>NEWS</span>";			else				echo "<a href=news.php>NEWS</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/photos.php" || $page == "/TestSite/photos")				echo "<span class=current>PHOTOS</span>";			else				echo "<a href=photos.php>PHOTOS</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/contact.php" || $page == "/TestSite/contact")				echo "<span class=current>CONTACT ME</span>";			else				echo "<a href=contact.php>CONTACT ME</a>";		?>	</li>		<li>		<?php			if ($page == "/TestSite/perfomance.php" || $page == "/TestSite/perfomance")				echo "<span class=current>PERFOMANCE PHOTOS</span>";			else				echo "<a href=performance.php>PERFORMANCE PHOTOS</a>";		?>	</li></ul>

First of all, for this code, the only reason I have it displaying the $page is to make sure it recognizes what page I am on. This works.What works: From the initial site load, the HOME PAGE is set to the span class, which is black with no underline (temporarily until I figure out something I like better once this code is functioning properly). This also works for the HEADSHOTS page.What doesn't work: The resume link is always set to the span class rather than the link. Also, every other page on the rest of the site stays as the link rather than switching over to the span class.Any help in resolving these issues would be greatly appreciated!Thanks in advance,Mark

Link to comment
Share on other sites

if ($page = "index.php" || $page = "index") echo "HOME PAGE"; elseif ($page = "headshots.php" || $page = "headshots") echo "HEADSHOTS";I'd use the following approach:if (strpos($page,'.php')===false) $page.='.php';if (file_exists($page)) echo strtoupper(substr($page,0,strlen($page)-4);It checks to see if there's a .php in the requested page, if not, it adds one. Then, it tests to see if the file exists, and finally echos the name if the file exists.

Link to comment
Share on other sites

One of your problems is here:

if ($page = "index.php" || $page = "index")

In PHP, comparisons are done with a double equals, so this should be:

if ($page == "index.php" || $page == "index")

With similar updates required to the other if statements.What is happening at the moment is $page is being set to the value "index.php", which is then evaluating as true (because in PHP any non-empty string is evaluated as true in if statements), so your code will always go into the first case.

Link to comment
Share on other sites

Trevelluk, thank you for that! It worked like a charm on my header. However, I am still struggling with my navigation menu. Wirehopper, I tried to put your bit of code into the navigation menu script, but there seems to be an issue with the last line. It says there is an unexpected ";" on that line. I don't know much about PHP, but I know that the ; needs to be there. Can you guys help me out with the navigation code? I'm just curious as to why the resume page is always set as the span rather than the link, and why none of the links following the resume page function properly with the script. Would it be better to use a different kind of statement for this?

Link to comment
Share on other sites

Sorry - it needed another )echo strtoupper(substr($page,0,strlen($page)-4));You have a lot of repeated code. If you create an array of titles and filenames, you can put it all in a loop, and it will be much easier to maintain.

$aPages=array(array('title'=>'Home','file'=>'home.php'),  array('title'=>'Photos','file'=>'photos.php'), ... (add additional pages) ... );echo '<ul>';foreach ($aPages as $k => $v){  echo '<li>';  if (strpos($page,'.php')===false)	$page.='.php';  if ($page==$v['file'])	echo ' <span class=current>'.$v['title'].'</span>";  else	echo "<a href="'.$v['file'].'">'.$v['title'].'</a>";  echo '</li>';}echo '</ul>';

Link to comment
Share on other sites

You guys are awesome! Thank you so much for the help, now my header and navigation codes work beautifully!I have one question just to clarify. Is the line, $page.=.php; saying that it takes what comes up as $page if the .php extension is not found, and then adds .php using .=?For the rest of the code, I have for the most part made sense of the rest of it, and I think I will be able to use this as a reference to work on the final version of this site and hopefully a good majority of other sites I do in the future. Thank you both so much for all the help!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...