1

i have a lot of pages, most all have head sections and can 'stand alone'. Also, most of them get 'included' in 'larger' documents or articles. So, in a, lets call it a 'big page' I could have 3 or 4 included pages, all having their own head information. Is there a fancier way to include a 'head.html' with all the meta's, style etc, BUT only once so that if the 'parent', lets say index.php has already 'included' 'head.html' the inclusion of say, specialcharacters.html will Not also load a head, but if I were to load specialcharacters.html on it's own, it Would 'include' the 'head.html'????

(exp: index.php includes, nav.html, nav_r.html, header (logo, welcome etc), footer.html, body01.html, specialcharacters.html, etc.. BUT, i want to use specialcharacters.html as a stand alone document with head, style etc for doc formating Also.)

so, some kind of include if... so head.html is only included Once.

I hope that is relatively clear..

Thanks you, in advance, Landis. landisreed dot com/index.php - head.html

1
  • you can set $_SESSION['page_has_header'] = TRUE just once and the other files/templates will just check that (dont forget the session_start() in top) and ignore the header if its TRUE. Commented May 31, 2012 at 6:13

4 Answers 4

2

I suppose you can use

include_once 'header.html';

Then if it was included before it won't be included again.

This said you would have to include header information into every file, so your specialcharacters.html will have to use it as well as body01.html. Then whichever include_once first - there header.html will appear.

EDIT:

To differentiate titles or other information you can do as follows in header.html:

<title><?=$title;?></title>

And then in every of your scripts

$title = 'Whatever';
include_once "header.html";

Now, whoever calls header first will set the $title first and render it inot the header. Once its rendered into header, subsequent changes of $title by any other includee will simply be ignored by your page.

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

8 Comments

"include_once" just like that? I don't need code in the 'included' document to 'check' against?
that's it? <?php include_once 'head.html' ; ?> That simple?
If your header is always same for all your files then you don't need anything else. If you use different headers then it will be more complicated.
just thought of a 'road block' the <title>, meta keywords and description needs to be different on each page.. damn guess i'll learn more about the if $caller suggestion. Thanks! Landis.
Thank You! That works for me. Will it work with $keywords and or $description too? might still need the if $caller code, eh?
|
1

have you tried using 'include_once'? http://www.php.net/manual/en/function.include-once.php

Example:

include_once "header.php";

Comments

0

include_once() is what you can use, according to PHP's documentation

The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

more information here https://www.php.net/include_once

Comments

0

If I got the question right, I think what you need is a single Header file header.php to be included in all pages with require_once. The trick isto put all different kinds of heads you have , like one for head.html and one for specialcharacters.html, into the header.php file separated by if statements. The header.php might look like this :

 if ($caller == 'head') { // HTML for head.html}
 elseif($caller != 'head' && $caller ==  'specialcharacters') 
     { // HTML specific to specialcharacters.html  for standalone viewing}

Once this header.php is written with all conditions in place, you need to set $caller accordingly at the top of each file (e.g. for specialcharacters.php first line of code should be $caller = 'specialcharacters';. Then include header.php to all files as require_once("header.php") just after specifying $caller. EDIT Your index.php file will look like this:

$caller = 'in_my_index';
require_once('header.php');

Your specialcharacters.php file will look like this:

$caller = ($caller != 'in_my_index')?'in_my_specialchars':'in_my_index'; // This is to make sure that when specialcharacters.php is included inside index.php then it should still show index.php title but when loaded standalone, it will show its own title.
require_once('header.php');

Now your header.php looks like this :

 <html><head>
 <?PHP    if($caller == 'in_my_index') { echo '<title>I am Index Title</title>';} 
          elseif($caller == 'in_my_specialchars') { echo '<title>I am standalone Specialcharacters.php Title</title>';}
 ?>

This should give a better idea I hope.

Hope this helps!

8 Comments

wow... thank you. this seems to be what i ultimately want, but will take some more understanding on my part.
thank you, what goes in top of specialcharacters.html to designate it as $caller 'specialcharacters' or is it based on the 'file_name'?
welcome :). The string 'specialcharacters' can be anything of your choice. Nothing specific about file name or anything. $caller is the kind of flag that will determine which piece of if condition to be executed from header.php when the page is loaded on the browser. So, if user is loading specialcharacters.php , notice that $caller is only flagged to 'specialcharacters' and not 'head'. So, inside header.php only the head section for specialcharacters.php will get executed. Basically you are creating code blocks here with the flag $caller to centralize various header HTMLs.
Thank you very much for your time. still unsure where "$caller 'specialcharacters' " is declared. how does if statement know page is 'specialcharacters' when called? - Landis.
so, would each page being called have <?php $caller = 'specialcharacters' ; ?> ... where "specialcharaters" being the name I'm assigning to the page ($caller ID), i get that it will be diff on each page, eg, 'colorcode', 'snippets' etc.. ?
|

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.