Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post

    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!

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,293
    Thanks
    4
    Thanked 202 Times in 199 Posts
    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

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    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!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    KaliK (03-28-2011)

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 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!!

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    KaliK (03-28-2011)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •