Kyi Zet
Kyi Zet

Reputation: 59

Is it possible to link to a header.php and footer.php from different folder sources?

I'm a beginner at PHP. I have multiple webpages residing in different locations. So when I wish to link to header.php and footer.php from the webpages in different folders, is it possible to do so? As shown in the picture, I have to create three different folders, containing same files, header.php and footer.php, to be able to link from three different sources. With Best regards!

enter image description here

Upvotes: 0

Views: 2747

Answers (3)

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

Yes it is possible to use a single footer.php and single header.php files and load them anytime you need.

What I would suggest you can do is that you create an include folder, then inside the include folder create another folder called common where by you will place website that elements that are always the same throughout the website ie, footer and header.

then I would also place a functions file inside the includes where I will place my website functions. Included in this function file is a function that I will use anytime I want to use the header.php and footer.php files.

enter image description here

Functions.php

<?php

    function loadView($viename,$meta=[]){
        //load footer/header page
        include_once "common/$viename.php";
}

//any other functions

The loadView() function is used anytime you want to load these two dynamic files. This functions takes two parameters 1 optional. The first parameter is the name of the view you want to load which is header or footer then the second optional is the meta information important for the header file, as the page title and meta description needs to be dynamic and change according to the page.

header.php

<!DOCTYPE html>
<html>
<head>
    <title><?=$meta['pagetitle']?><!-- Dynamic page title --></title>
    <meta name="description" content="<?=$meta['pagedescription']?>"><!-- Dynamic description -->

    <!-- load your styles -->
</head>
<body>
    <header>
        <nav>
            <!-- Your page navigation -->
            <ul>
                <li><a href="index.php">Home</a></li>
                <li><a href="about.php">About</a></li>
                <li><a href="anotherpage">Another Page</a>
            </ul>
        </nav>
    </header>

footer.php

<footer>
    footer content

    <p>&copy; website name <?=date('Y')?>
</footer>

</body>
</html>

Main website pages

Your main website pages are pages such as index, about,services etc.

In these pages you would load the functions file, then be able to load the header and footer.

index.php

<?php
    include 'includes/functions.php';

    //meta info
$meta = array(
        'pagetitle' => 'Welcome to my site | site | bla bla',
        'pagedescription' => 'This is your website description'
   );

loadview('header',$meta); //load heade
?>

<section>
    <div id="content">
        <p>Page Content</p>
    </div>

</section>
<?php

    loadview("footer"); //load footer
?>

About Page

<?php
    include 'includes/functions.php';
$meta = array(
        'pagetitle' => 'About Us',
        'pagedescription' => 'This is about page'
   );

loadview('header',$meta);
?>

<section>
    <div id="content">
        <p>Page Content</p>
    </div>

</section>
<!-- load footer -->
<?php

    loadview("footer");
?>

Hope this gives you the idea on how you could achieve your goal, there are many ways you can achieve this.

Let me know when you need any help

Upvotes: 3

Kyi Zet
Kyi Zet

Reputation: 59

My apologies for not providing enough information about the issues. My issue is that, when the index.php refers to the header and footer by "includes/header.php" and "includes/footer.php" respectively, and other webpages are located inside another folder which needs to access the includes folder via "../includes/header.php". There is no problem while referring to the files but the issue occurs when headers.php targets the webpages inside when it is written to only work with index.php. For example, would only work on index.php but not on the php files inside the folder which needs , But I'll try with $h_path = ''; and $f_path = '' soon.

Upvotes: 0

Lakshika Wijayasinghe
Lakshika Wijayasinghe

Reputation: 41

   Assign values for $h_path and $f_path dynamically.
    <?php 

   $h_path = '';
   $f_path = '';

   include($h_path.'header.php'); 
   include($f_path.'footer.php'); 
   ?>

Upvotes: 1

Related Questions