1

Let says below are my web directories:

My web distribution

From web searching i get to use include or require_once and something similar.But I'm confuse in different directory causes much of directory error.

And my question is, how to include Navbar or Footer or Specific Pages in different directory.

Condition: Same directory as above;

2 Answers2

1

For header,navBar and footer you should used simple

include(page/header.php);

About require and include function Reference

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

But for image,css files and other js files you should follow these steps

Create config.php file

add this in config.php

define('BASE_URL', 'http://your_website_url');//OR define('BASE_URL', 'localhost')

you can use this path like

<?php
    include('config.php');//add this code into top of the page
?>

and finally you can used where you want, like

For Style sheet

<link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • Hi! Thank you for your reply. But if I have multiple pages in ** \localhost\page\ ** and my `index.php` is in the base? How do the `include` works now? – Richard Kok Oct 04 '18 at 02:28
0

Create a config.php file in your home directory.

<?php
    /* Saves the directory path to base directory as `BASE_DIR`
       Example: /var/user1/home/public_html/
       'Base directory' is where your homepage/index.php is located */

       define('BASE_DIR', dirname(__FILE__) . '/');
?>

now just require this config.php file

<?php
    require_once('config.php'); //add this line at top of your pages
    // No need to include this inside 'header.php' and 'footer.php'
?>

Now u can include files easily, use it in index.php as follows:

<?php
    require_once('config.php');

    require(BASE_DIR . 'query/conn.php');
    // becomes '/var/user1/home/public_html/query/conn.php'

    include(BASE_DIR . 'pages/header.php');
    include(BASE_DIR . 'pages/navbar.php');
    include(BASE_DIR . 'pages/footer.php');
?>

Note: Similar approach is used by WordPress (learn more)

Jaswinder
  • 361
  • 2
  • 11
  • Emm... sorry for I'm not so understanding of the use of `config.php` file from your answer above. Do you mind to explain more ? – Richard Kok Oct 04 '18 at 02:33
  • `config.php` should be the file that contains all configuration variable like database details extras, it should also `include` or `require` all the essential files like `conn.php` as you'll most likely import it on more than one page, so its easier to edit just `config.php` than to change the `include` in every single file that uses it, in case you don't need to include `conn.php` anymore – Jaswinder Oct 04 '18 at 03:16
  • I see, but the `navbar.php`, `index.php`, and `other.php` are dispersion into different folders. It means it still won't work fine right? – Richard Kok Oct 04 '18 at 03:22
  • Nope, it'll work all right, just change the location to where the files are (realtive to `config.php`) – Jaswinder Oct 04 '18 at 03:24
  • So it means if the `config.php` is place with the `aaa/bbb/ccc.php` same folder than it will works?! – Richard Kok Oct 04 '18 at 03:34
  • I didn't understand your last comment – Jaswinder Oct 04 '18 at 03:40
  • I mean I put the `config.php` file with the `navbar.php`, `footer.php` in the same folder than it will work without directory error right ? – Richard Kok Oct 04 '18 at 04:20
  • You should put the config file in the root of your project – Jaswinder Oct 04 '18 at 04:22
  • But how about the other file such as `navbar.php`, `footer.php` ? – Richard Kok Oct 04 '18 at 06:58
  • You should check out `include()` [Php Manual]( http://php.net/manual/en/function.include.php), `include` n `require` does not need all files to be in same folder. I think you r not familiar with how it works. Checkout the linked documentation. Also checkout the last code snippet in my answer to see how `include` or `require` files from different locations – Jaswinder Oct 04 '18 at 07:08