0

hope you fine and well,

i have a navigation bar like this :

  <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li>
                        <a onclick="load_home()"href="#">About</a>
                    </li>
                    <li>
                        <a href="#">Services</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>
            </div>

how i can show this bar in all my html and php pages !

regards.

MSMC
  • 141
  • 1
  • 5
  • 16

3 Answers3

2

Put the render code in a my_navbar.php

         echo '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li>
                    <a onclick="load_home()"href="#">About</a>
                </li>
                <li>
                    <a href="#">Services</a>
                </li>
                <li>
                    <a href="#">Contact</a>
                </li>
            </ul>
        </div>';

and then in all php file you need the navbar put

<?php
 include_once "my_navbar.php"; // this will include a.php

?>

Use include_once for avoiding multiple include

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • thanks dear , but to use this i have to change all my files to php ! true ?! how i can do similar concept but with keeping html files ?! – MSMC May 08 '16 at 18:29
  • I think the php solution is the more simple but you can add code with jquery .. or html link .. – ScaisEdge May 08 '16 at 18:36
  • Yes .. i know it this solution but you can explore are several .. depend on your context.. .. another good "way" with php is use framework .. they are all good solution for this kind of need .. anyway if the problem is strictly related to navbar .. try firts the link solution ... is more HTML complaint.. then i suggest the jquery solution .. i hope this suggestions are useful for you .. – ScaisEdge May 08 '16 at 18:40
  • thanks dear i will tests all given solutions, thanks. – MSMC May 08 '16 at 18:43
1

To expand on scaisEdge's comment, here's how you can do this in an include.

In nav.php (name it however you want):

<?php
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
              <li>
                  <a onclick="load_home()"href="#">About</a>
              </li>
              <li>
                  <a href="#">Services</a>
              </li>
              <li>
                  <a href="#">Contact</a>
              </li>
          </ul>
      </div>
?>

Then where you want the navigation:

<?php include('nav.php'); ?>
Nathan Wakefield
  • 105
  • 1
  • 11
1

You can use include(), i.e.:

navigation.php

<?php
echo <<< EOF
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
   <ul class="nav navbar-nav">
      <li>
         <a onclick="load_home()"href="#">About</a>
      </li>
      <li>
         <a href="#">Services</a>
      </li>
      <li>
         <a href="#">Contact</a>
      </li>
   </ul>
</div>
EOF;

anotherfile.php

<?php
include("navigation.php");
//the rest of the code...

include vs include_once:

The include_once and require_once functions are slower than include and require, simply because they keep track of the files that have already been included, to avoid including them more than once.

Community
  • 1
  • 1
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268