Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 03-11-2011, 08:35 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,229
Thanks: 2
Thanked 190 Times in 188 Posts
djm0219 is on a distinguished road
PHP Code:
<div id="mainNav">
<ul id="mainNav">
<li>
<?php
if ($page=='index.php') {?>
    <a href="index.php" class="active">Welcome</a>
<?php
} elseif ($page=='healthcare.php') {?>
    <a href="healthcare.php" class="active">Healthcare</a>
<?php
}
?>
</li>
</ul></div>
If you have more pages simply expand the use of elseif to test for them.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 03-11-2011, 09:10 PM   PM User | #3
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
Hi Dave,

Thank you for your response. I tried the code you gave me but all I'm getting is the same error.

I then tried to replace the single quotes around index.php and healthcare.php with double quotes (just to see if that made any difference) and, while I don't get an error, all of the links now have the active class.

I'm totally baffled about what could be going on.
Any help would be great! Thanks!
KaliK is offline   Reply With Quote
Old 03-11-2011, 10:25 PM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
try this way:
PHP Code:

    <?php
    
    $main_nav 
'
        <div id="mainNav"><ul id="mainNav">
<li><a href="index.php" ' 
. (($page=='index.php') ? 'class="active"' '') . '>Welcome</a></li> 
<li><a href="healthcare.php" ' 
. (($page=='healthcare.php') ? 'class="active"' '') . '>Healthcare</a></li> 
        </ul></div>
    '
// end main_nav
    
    
?>
best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
KaliK (03-28-2011)
Old 03-11-2011, 10:44 PM   PM User | #5
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
oesxyl,

That works! Wonderful! I'm not sure how the periods and semicolons work in the php. I would like to learn the process so that I can use it elsewhere later...

It seems you closed out the html with single quotes and used the period to append the php and then reversed the process to append the html. Am I right?

This is great, Thank you sooooo much!!
KaliK is offline   Reply With Quote
Old 03-11-2011, 11:04 PM   PM User | #6
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by KaliK View Post
oesxyl,

That works! Wonderful! I'm not sure how the periods and semicolons work in the php. I would like to learn the process so that I can use it elsewhere later...

It seems you closed out the html with single quotes and used the period to append the php and then reversed the process to append the html. Am I right?

This is great, Thank you sooooo much!!
any string can be delimited by ' or by " the difference is how variables, if exists in the string, are evaluated to build the resulting string.
I don't think there are rules when you need to use one or anothers, i usualy use ' as delimiter because i always use " in html. Also i prefere to not use variables inside a string( split the string in pieces arround variables and concatenate).
in this specific case i tried to avoid to use <?php and ?> and to make if tests simpler by using ternary operator ?:.

best regards
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
KaliK (03-28-2011)
Reply

Bookmarks

Tags
includes, main navigation, php, template, t_string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:55 AM.


Advertisement
Log in to turn off these ads.