View Single Post
Old 03-11-2011, 08:21 PM   PM User | #1
KaliK
New Coder

 
Join Date: Mar 2011
Posts: 21
Thanks: 4
Thanked 1 Time in 1 Post
KaliK is an unknown quantity at this point
Question Creating active links with PHP using .inc file resulting in errors

Hi Everyone,

I'm semi-new to php and trying to create a site where I have only one includes file for my navigation (main_nav.inc) and 2-3 includes files (main_template.inc) that act as templates. I then call the the nav and the template in my index.php file.

Everything was working fine until I decided I should use php have my links appear active depending on which page the user is on. Everything works totally fine if I insert the code for my nav (including both the html and php) into my template but when I make it into it's own includes file, I get an error.

I believe the issue is with single and double quotes.

Here is the code from my main_nav.inc file:

Code:
	<?php
	
	$main_nav = '
		<div id="mainNav"><ul id="mainNav">
<li><a href="index.php" <?php if ($page=='index.php') { ?> class="active" <?php } ?>>Welcome</a></li> 
<li><a href="healthcare.php" <?php if ($page=='healthcare.php') { ?> class="active" <?php } ?>>Healthcare</a></li> 
		</ul></div>
	'; // end main_nav
	
	?>
I think that since the code is wrapped in single quotes, I can only use double quotes in my code. This is the only difference I can figure out between inserting the code into the template directly (which works perfectly fine) and inserting it as a separate inc file I get the following error:
Parse error: syntax error, unexpected T_STRING in /home/content/k/a/l/kalilaks3/html/includes/main_nav.inc on line 11

I tried changing the quotes around ($page=='index.php') to ($page=="index.php") but that causes the browser to literally read the name of the link a class="active">Welcome

I also tried variations of escaping quotes such as ($page==\"index.php\") and even escaping the other quotes and wrapping everything in double quotes as opposed to single quotes.


Here is my code for my main_template.inc file

Code:
<?php

	$page = basename($_SERVER['SCRIPT_NAME']); 
	$page_title = "Welcome";

	include_once ("includes/main_nav.inc"); // sets variable
	include_once ("home_template.inc"); // sets all variables

	?>
And here is my index.php file:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>	
	<meta content="healthcare" name="keywords"></meta>
	<meta content="healthcare" name="description"></meta>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><?php echo $page_title; ?></title>
	<link rel = "stylesheet" href = "_css/layout.css"></link>
	<script type="text/javascript" src="scripts/script.js"></script>
</head>
<body>	
<div id = "header"><div id = "globalHeader">
		
		<?php echo $main_nav; ?>

</div></div>
</body>
</html>
Any help or suggestions would be greatly appreciated!!! I would love to get this figured out so that I only have to create my navigation once for all my sites!
KaliK is offline   Reply With Quote