-2

CLARIFICATION: Being that I've never used PHP, it was unclear how to format the PHP file, so that when you include it in the index, then header(nav bar) would show up. No posts explained why I had to change my index.html to index.php to make it work. And again, being that I haven't used PHP, I was under the impression that changing the extension meant that I would have to convert my HTML code to PHP commands. I was trying to avoid having to change my whole code around. That's where the confusion was.

I was going to delete the question once I got it figured out. Then again, I'm sure I'm not the only person that may run into this while making their first website.

SOLUTION. Save only the nav code in html language with a PHP extension (header.php). Change index/home file from .html to a .php extension (don't have to modify any code. but accepts the include prompt). Then, include the header.php in your index.php. Same goes for your footer. Thanks for the help everyone.

*ORIGINAL POST: Through another post, I was told it would be easier to use a nav/footer on multiple pages by using PHP.

I created a file and tried including it and it is not displaying. I'm not sure what I am doing wrong.

There is some CSS in my nav bar as well, do I need to import the css file in the PHP file? or does the index.html file automatically attach to the included (php) file.

PHP Code:

<?php
echo '<nav>
    <div>
        <a href="/">
            <div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
            <div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
            <div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
        </a>
    </div>
    <div> 
        <a href="/" class="here">Home</a>
        <a href="/about.html" >About</a>      
        <a href="/services.html" >Services</a>          
        <a href="/pricing.html" >Pricing</a>    
        <a href="/contact.html" >Contact Us</a>
        <input id="srchbar" type="search" placeholder="Search">
    </div>
</nav>';
?>

HTML Code (include):

<body>
<?php include '/header.php';?>
    ....other code....
</body>

If there's a problem with the PHP file, if I'm missing something, can someone show an explain please?

blackRob4953
  • 1,585
  • 2
  • 12
  • 15
  • 1
    In `PHP` you need to `echo` them or use only `HTML`, remove the `` tag. – Sougata Bose Aug 17 '15 at 05:13
  • I removed the PHP tags in header.php , and still no display of my nav bar... – blackRob4953 Aug 17 '15 at 05:27
  • What happens when you change it to ``? Can you show your directory structure? *P.S* - your index file **needs** to be `.php`, not `.html` – Darren Aug 17 '15 at 05:41
  • I'm using HTML5, I was told only to make the header a .php file so I can add it to multiple pages. Was this wrong? I tried adding gwtcwd()., still same response, no header... Directory structure: Root folder: -Images folder; -JS folder; -Stylesheets folder; header.php; index.html; index.php; Only created the index.php due to the answer above, that still didn't display nav bar – blackRob4953 Aug 17 '15 at 05:48

3 Answers3

1

no need to add <?php ?> code in the header file just keep it as it is :

  1. Create a new file named header.php and add this code to it

    <nav>
    <div>
        <a href="/">
            <div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
            <div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
            <div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
        </a>
    </div>
    <div> 
        <a href="/" class="here">Home</a>
        <a href="/about.html" >About</a>      
        <a href="/services.html" >Services</a>          
        <a href="/pricing.html" >Pricing</a>    
        <a href="/contact.html" >Contact Us</a>
        <input id="srchbar" type="search" placeholder="Search">
    </div>
    

  2. Include header.php anywhere you want

  • I removed the PHP tags in header.php , and still no display of my nav bar... – blackRob4953 Aug 17 '15 at 05:26
  • are you working on local machine ? and what is the mother page ? like index.php or index.html .. i prefer you should change it to index.php – Aakash Shah Aug 17 '15 at 06:17
  • I am working on a local machine, I'm using Cloud9 to build and test my site. Only thing I could find that actually imported my .css and worked, so I don't see php being a problem. My mother page is index.html, i tried changing it to .php, still same issue with not displaying the header.php – blackRob4953 Aug 17 '15 at 06:19
  • use this and also store the file one directory above .. if it doesn't work use require rather than include – Aakash Shah Aug 17 '15 at 06:22
  • http://create.shah.gq/ visit this page ... its working fine here – Aakash Shah Aug 17 '15 at 06:26
  • also did not work. I'll update the question with an image of how it looks. .html and .php and saved in the root folder, theres no directory above. – blackRob4953 Aug 17 '15 at 06:28
  • you can check the same code working on create.shah.gq page .. its working fine .. change the page to index.php and use php require .. if still problem persist let me know and don't use echo .. just remove whole php frame – Aakash Shah Aug 17 '15 at 06:30
  • Thanks man it worked. index.php and using require. So for my remaining pages, do I use .php and I can still write html code? is That how it works? – blackRob4953 Aug 17 '15 at 06:41
  • well @blackRob4953 it depends .. like if your data is dynamic you have to use php and if your data is remaining same i.e. static you can use html .. but if you want easiness and speed go for php .. you can make a header and footer file in which you can define header and footer so afterwards on every page you just have to include file of header and footer .,.. and yes you need to change the code to php if you are including file from another source – Aakash Shah Aug 17 '15 at 07:04
0

Remove the <?php tag from your code. As you generated html output. <?php tags are used to generate output by php.

<nav>
    <div>
        <a href="/">
            <div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
            <div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
            <div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
        </a>
    </div>
    <div> 
        <a href="/" class="here">Home</a>
        <a href="/about.html" >About</a>      
        <a href="/services.html" >Services</a>          
        <a href="/pricing.html" >Pricing</a>    
        <a href="/contact.html" >Contact Us</a>
        <input id="srchbar" type="search" placeholder="Search">
    </div>
</nav>

If you want to use php tags, than you have to write the code in this way:

<?php
echo '
<nav>
        <div>
            <a href="/">
                <div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
                <div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
                <div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
            </a>
        </div>
        <div> 
            <a href="/" class="here">Home</a>
            <a href="/about.html" >About</a>      
            <a href="/services.html" >Services</a>          
            <a href="/pricing.html" >Pricing</a>    
            <a href="/contact.html" >Contact Us</a>
            <input id="srchbar" type="search" placeholder="Search">
        </div>
    </nav>
';
?>
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

Your problem is most likely the pathing of you include. Often you'd need to specify from the server root, and not just /header.php.

To specify your including-path, you could specify the root like this

include $_SERVER['DOCUMENT_ROOT']."/header.php";

You can also set the default include path in your php.ini file, thus making the $_SERVER['DOCUMENT_ROOT'] an automatic process, that can be done with running this code, that will alter your php.ini file.

ini_set('include_path', '/usr/lib/pear');

As others have pointed out, you don't need the contents of your /header.php to be echoed out as it's pure HTML. Anything that's included will be included the way it is, so having the contents of /header.php as pure HTML just makes for better practice, as it's easier to read.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • I replaced my include statement, with the code you gave me, still no show. If it helps, I'm using Cloud9 to build and test my website. I'm not sure if the "document_root" should be different because of that. I just have my website folder, with everything else in it. So normal to get to root I would just use a forward slash. Also, haven't touched any php.ini file, this is my first website and this is the first i've used php so that's my problem i suppose – blackRob4953 Aug 17 '15 at 06:17
  • Using `include` will produce a warning in your `error_log` file, stating that it could not include the file. If you read out the path it specifies there, and what `$_SERVER['DOCUMENT_ROOT']` is, (just echo it in PHP), you might find out what the path you need is. I'm nearly sure that your problem is the pathing of your file. Another sidenote, if everything is in the same folder, you can just remove the slash, as that would be to include by relative path. Also, using `include "../header.php";` will include from a parent directory, you could try that as well. – Qirel Aug 17 '15 at 06:28
  • Question below solved the problem. simply changing the mother file to index.php, and using require instead of include – blackRob4953 Aug 17 '15 at 06:42
  • Using `require` or `include` doesn't really make a difference, the require() function is identical to include(), except that it handles errors differently. As for your solution, you're using relative path (which can be a problem later on). If you `echo $_SERVER["SCRIPT_FILENAME"];` in the `header.php`, you will get the full file, and if you include from that, it's an absolute path - meaning that it will be included from all over your server, not just in the current subfolder. – Qirel Aug 17 '15 at 14:06
  • Ok, so in my header.php. You're saying to use php parameters () around my html code and the first line in my body should be (echo $_SERVER["header.php"];) . Am I getting that correctly, and it will still display my header, throughout all platforms? – blackRob4953 Aug 18 '15 at 05:59
  • Not exactly... Your `header.php` is pure HTML, so you do **not** need any PHP in there! However - to find the path of the file (so you can include it), run `` in `header.php`. This will get you the full path of the file when you visit your `header.php` in a web-browser. Once you know that, you can copy that path to your `include` and remove `` from `header.php`. This way, your `header.php` can be included from all over your application, and not just from a relative path. Also, include and require does the same. – Qirel Aug 18 '15 at 14:14
  • Makes sense, I see what you were saying and got the path. Thank you for that! – blackRob4953 Aug 18 '15 at 18:07