Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to include php headers and footers on html site Reply To: How to include php headers and footers on html site

#236199
danallen
Participant

Mine works like this. Most pages will have a file with these statements:

$directoryToPageGroups = 'pageparts';
$thisPageGroup='/002_list_of_cats';
$common='/000-common';
include $common.'/my_functions_and_variables.php';
include $thisPageGroup.'/015_start_html.php';
include $thisPageGroup.'/020_start_body.php';
include $common.'/800_footer.php';
include $common.'/899_finish_page.php';

So, I have folder with folders, each containing a set of page parts for each page. The page part files all have the same names, but their folder names show which is which. Then inside those page parts, there are a lot more include statements, or links to css and js. Here is part of what goes into most 015_start_html.php files.

 <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <? echo $copyright;?>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">
    <title><?=basename($baseURL).'/'.basename($_SERVER['REQUEST_URI'])?></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
     <? $timeChecks=addTimeCheck($timeChecks, number_format(microtime(true),6,'.',''), __LINE__, basename(__FILE__));?>
   <link href="<?= $cssURL?>css.page.layout.css" rel="stylesheet">
    <link href="<?= $cssURL?>css.footer.css" rel="stylesheet">
    <link href="<?= $cssURL?>css.css" rel="stylesheet">
    <link href="<?= $cssURL?>css.menus.css" rel="stylesheet">
    <link href="<?= $cssURL?>wg.css" rel="stylesheet">
    <!--link href="<?= $cssURL?>fonts.css" rel="stylesheet"-->
    <?
    include_once $basePath    . 'lib/SqlFormatter.php';
    include $cssPath    . 'styles-for-stripes.php';
    include $cssPath    . 'randomized-orange-gradient.php';
    include $cssPath    . 'css.php';
    ?>
    <link href="<?= $cssURL?>stripes.css.all.rights.reserved.css" rel="stylesheet">
    <link href="<?= $cssURL?>random.gradients.css.all.rights.reserved.css" rel="stylesheet">
    <link href="<?= $thisPlayCssURL?>thisPlay.css" rel="stylesheet">
    <link href="<?= $cssURL?>cssResponsio.css" rel="stylesheet">
    <link href="<?= $thisPlayCssURL?>cssResponsio.css" rel="stylesheet">

All those variables are set by my_functions_and_variables.php, called at the beginning. Those variables automatically adjust to the url and path of where the code is running, so the app can be moved and run without having to reconfigure.

COMMENTS WORTH AT LEAST WHAT YOU ARE PAYING FOR THEM, probably not more.

Organizing files for a website is pretty big topic. People go about it all kinds of ways.

One of the main objectives always is having files that can be reused and isolating the code that is unique, so you can combine the unique stuff with the stuff that is reused as efficiently as possible. Stuff that gets reused usually includes that top and bottom of a page, also the style sheets, shared functions, especially functions related to interacting with a database or any other systems that your web pages connect with, library elements, such as bootstrap or jquery.

I think it would be hard to do this well without having worked on some good examples that others have put together first. There are elaborate frameworks that provide solutions to this. I have developed a scheme Iike, but the big problem with doing my own is that it looks weird to every programmer who sees it and is looking for what they are accustomed to finding.