0

I just need to include an html file (that has my navigation bars for all my pages).

I've tried: <?php htmlentities(file_get_contents("include/navigation.html")); ?>

But nothing shows up at all.

I've checked other questions like this and the code above is always the answer. So why is it not working for me?

Here's my navigation file if needed:

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">[ ] Advanced Web Development</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li class="active">
                    <a href="index.html">Home</a>
                </li>
                <li>
                    <a href="#about">About</a>
                </li>
                <li>
                    <a href="#resume">Résume</a>
                </li>
                <li>
                    <a href="#blog">Blog</a>
                </li>
                <li>
                    <a href="#projects">Projects</a>
                </li>
                <li>
                    <a href="#blog">Contact</a>
                </li>
                <!--
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Portfolio <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="portfolio-1-col.html">1 Column Portfolio</a>
                        </li>
                        <li>
                            <a href="portfolio-2-col.html">2 Column Portfolio</a>
                        </li>
                        <li>
                            <a href="portfolio-3-col.html">3 Column Portfolio</a>
                        </li>
                        <li>
                            <a href="portfolio-4-col.html">4 Column Portfolio</a>
                        </li>
                        <li>
                            <a href="portfolio-item.html">Single Portfolio Item</a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Blog <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="blog-home-1.html">Blog Home 1</a>
                        </li>
                        <li>
                            <a href="blog-home-2.html">Blog Home 2</a>
                        </li>
                        <li>
                            <a href="blog-post.html">Blog Post</a>
                        </li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Other Pages <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="full-width.html">Full Width Page</a>
                        </li>
                        <li>
                            <a href="sidebar.html">Sidebar Page</a>
                        </li>
                        <li>
                            <a href="faq.html">FAQ</a>
                        </li>
                        <li>
                            <a href="404.html">404</a>
                        </li>
                        <li>
                            <a href="pricing.html">Pricing Table</a>
                        </li>
                    </ul>
                </li>
            -->
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>
10
  • Is there a reason you can't just include 'include/navigation.html';? Commented Mar 12, 2015 at 2:15
  • Tried that as well. Nothing Commented Mar 12, 2015 at 2:15
  • why don't save it as php and then <?php include 'nav.php'; ?> Commented Mar 12, 2015 at 2:16
  • Was there an error or warning of some kind? If include (or, more strictly, require) fails then there would be something in the logs. If the code isn't executing at all then that's a completely different problem. Commented Mar 12, 2015 at 2:16
  • 1
    let me guess you're not doing it in local host like wamp or xamp Commented Mar 12, 2015 at 2:18

4 Answers 4

1

In your above example, php is creating a string from the file, but doing nothing with it. In order to make your code work, you would need to add an echo:

<?php echo htmlentities(file_get_contents("include/navigation.html"));

This is because there are generally three ways that functions can affect things:

  1. Returning a value (what htmlentities does)
  2. Modifying (a) value(s) passed into them (take a look at passing by reference
  3. Echoing something directly or to the output buffer

Since htmlentities returns a value, nothing is sent to output. You would have to either save it to a variable for output later, or echo/print it to the output now.

Alternatively, you could include it as suggested by @David.

Sign up to request clarification or add additional context in comments.

1 Comment

You should touch on the fact that htmlentities() is a return function so it must be assigned to a variable or echoed in order to be used.
1

While using the echo on your normal code would be sufficent, if you want people to be able to access the HTML, you can include it in a php file itself, anywhere except inside the <?php ?> tag.

Comments

0

htmlentities() does not output to the browser. You still need to echo() or otherwise output.

<?php echo htmlentities(file_get_contents("include/navigation.html")); ?>

Or as suggested in the comments use include() to embed the entire file. PHP will attempt to process the file, so make sure it is error free if it contains any PHP code.

<?php include( "include/navigation.html" ); ?>

Comments

0

try:

<?php
    $file = htmlentities(file_get_contents("include/navigation.html"));
    echo $file;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.