Jump to content

Trouble creating active links with PHP and .inc files


KaliK

Recommended Posts

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 main_template.inc file but when I make that code into it's own includes file (main_nav.inc), I get an error.

 

I believe the issue is with using both single and double quotes in my code and wrapping that code in single quotes in my main_nav.inc file.

 

Here is the code from my main_nav.inc file:

 

 

<?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. When I use the main_nav.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

 

<?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:

 

<!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!

Just in case anyone else has this problem.. I found the answer to my question from a user on another forum..

 

 

    <?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

   

    ?>

 

This works without errors!

Archived

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

×
×
  • 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.