Look at also other topic groups, which I have done!
You can return to this topic group by using this menu and the link Table of topic groups in the top of the page.
| |||||||||||||||
| Table of topic groups > Front page of mixed information technology pages > Mixed > Dynamic structure with PHP > Getting fragments (section 1/4) |
|---|
Fragments have been gathered together by using either readfile(), include() or require() functions. The file is normally inside parenthesis but concerning require quotation marks (' or ") are enough (for example <?php require(some.inc';?>). In addition it is noticeable that include(') should be enclosed with a curly brackets ({ + }) using statement block if it is inside a conditional block.
In addition of these function is needed for basic output the function print() or echo(), which either write a new code or print the brought code into another place. In the latter case the code has been defined as a variable. Below is an example of using a variable in order to print a string:
<?php $Variable="\"This is the text, which the should be printed by using a variable\"";
print $Variable;?>
"This is the text, which the should be printed by using a variable"
The string could have ordinary text or for example HTML-code. If the string has single or double quotation marks it is necessary to put a backward slash (\) as the escape character before quotation marks. Otherwise the PHP-parser would interpret quotation marks either terminating the string or diving the string into pieces. If the string would have divided into pieces between sub-strings should be a dot (for example "string"."string"), when the string would be concatenated.
In some is necessary use so called arrays, which has several particles, for example:
$aLinks = array('Link1.inc','Link2.inc','Link3.inc');
Functions, which include fragments into main document should use so that the server would use as fast as possible but functions have also their special problems. Below is some basic information:
The readfile() function can be used, if the included fragment doesn't have any PHP-encoding (if the file has by accident PHP-tags, the PHP-parser doesn't parse anything as PHP). For example the file topSection.inc
<div class="cNoPrint"><a href="#Top"><img src="../Kuvat/buttons/Top.gif" border="0" alt="[Top]" align="middle" title="To the top of this page"/></a></div>
has been get for example with the following encoding:
<?php readfile('topSection.inc');?>
The require() function can be used, if the included fragment has PHP-encoding but included fragmenst have not been used in a loop and included files are not a part of an array. For example in the file letters.inc is a variable
<?phpwhich has been brought with the following encoding:
$Standards="A";
...
?>
<?php require ('letters.inc'); print $Standards;?>
The include() function must be used, if the included fragment has PHP-encoding and it has been used in a loop and as a part of an array. For example from the files Link1.inc, Link2.inc and Link2.inc has been made an array and they have been set into a for-loop, which prints list tags around each item. The link set has been generated inside a list element:
<ul>
<?php
$aLinks = array('Link1.inc','Link2.inc','Link3.inc');
for ($i = 0; $i < count($aLinks); $i++)
{
print "<li>";
include $aLinks[$i];
print "</li>";
}
?></ul>
Server-related notes:
All servers don't support the readfile() function.
If the server founds an error, when the require() has been used and the required file can't be found, the server stops immediately downloading the page. If the include() would have used instead, the server could skip the error. It is the most safe to use the include() function.
Servers have different error message printing. It might be impossible to debug all server errors online.
With certain settings of the working mode and open_basedir relative references don't work. If pages are necessary to test offline it is necessary to think thoroughly reasonable naming conventions and put most included files to the same folder. Each data type could be searched by using file masks (for example *index.inc).
Servers might also prevent using absolute paths (http://...), when it is impossible to share PHP-files with several folders.
At least following fragments would be reasonable:
Most of the code inside the HEAD element.
Major structural tags of the top of the page.
Major navigation elements of the top of the page.
The actual content of the page.
Major structural tags of the end of the page + possible advertisements and announcements.
I had in the page DynamicMenus.php3 have once been the following structure (I have added comments):
<?php
/* The top part of the page: */
readfile('AdviceMeta.inc'); /* The generic beginning until the common HEAD-section. */
require ('DynamicMenus0Top.inc'); /* Page level HEAD-section and the start-tag of theBODYelement.
/* The beginning of the BODY-section: */
readfile(topSection.inc'); /* Immediately after the start-tag of theBODY-element existing common part of the BODY-section. */
require('dynamicMenusButtons.inc'); /* Page level navigation buttons. */
readfile('AdviceOldInfo.inc'); /* Generic information for old browsers. */
/* Common navigation elements in the top part of the BODY-section: */
readfile('AdviceHideInst.inc');
require (allPages4.inc');
requre ('AdviceNavigation.inc');
/* The content-related parts of the BODY-section: */
print "<div class=\"doc\" id=\"docSection\">"; /* The start-tag of the surround the actual content existing basic structural element. In some pages this is a part of the actual content of the page. */
require ('DynamicMenus0Content.inc'); /* The actual content of the page. */
/* Parts, which concern terminating the page: */
readfile(nextSection.inc'); /* Two navigation buttons (to the top of the page and to the next page). */
print "<br /></div>"; /* The end-tag of the structural element, which was written around the actual contents. */
readfile(bottomShort.inc'); /* The final termination of the page including termination tags forBODYandHTMLelements. In some page the page I have advertisements and announcements before termination tags. */
?>
Some fragments can think as as if modules. In the example above all browsers get the same modules. It is also possible to tailor modules for different browsers (I handle browser-detections further).
In page-level META tags a part of the content is common and part page-specific. For example before the page-specific description is the generic description of the sub-site:
<meta name="description" content="<?php readfile(metaContent.inc');?>..." />
I have made with PHP + JavaScript very effective way to control relative navigation links (up, prev and next) by setting to the HEAD-section:
<link rel="prev" href="javascript:prev();" />
<link rel="next" href="javascript:next();" />
<link rel="up" href="javascript:up();" />
...
<script language="JavaScript1.1" type="text/javascript">
<!--
function up(){
open("index1.php3","_self");}
function prev(){
open("Linkkeja.php3","_self");}
function next(){
open("index2a.php3","_self");}
//--></script><style type="text/css">
...
And into the BODY-section:
<body...>
...
<td align="left" style="width:16px !important">
<a href="javascript:prev();" onmouseover="window.status='The\ previous\ page\ or\ anchor';return true;" onmouseout="window.status='';return true;" class="left" title="The previous revious page or anchor"><img
src="../Kuvat/buttons/Left.gif" border="0" alt="[Prev]" align=
"middle" style="height:16px; width:16px" /></a>
</td>
<td align="center" class="up">
<a href="javascript:up();" onmouseover="window.status='To\ the\ upper\ page\ level';return true;" onmouseout="window.status='';return true;" class="up" title="To the upper page level"><img
src="../Kuvat/buttons/TopSmallDouble.gif" border="0" alt="[Up]" align=
"middle" style="height:16px; width:16px" class="up" /></a></td>
<td align="right" style="width:16px !important">
<a href="javascript:next();" onmouseover="window.status='The\ next\ page\ or\ section';return true;" onmouseout="window.status='';return true;" title="the next page or section" class="right"><img
src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="middle" style="height:16px; width:16px" /></a>
</td>
...
<?php readfile('nextSection.inc'); ?>
</div>
</body>
</html>
In the nextSection.inc is the following HTML-code:
<div style="margin-top:1.0em"><div class="cNoPrint"><a href="#Top"><img
src="../Kuvat/buttons/Top.gif" border="0" alt="[Top]" align="middle" title="To the top of this page"/></a><br /><br /></div>
<div class="noPrint noBig" align="right"><a href="javascript:up()"><img
src="../Kuvat/buttons/TopSmallDouble.gif" border="0" alt="[Up]" align="middle" title="To the upper page level" /></a> <a href="javascript:next();"><img
src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="right" title="The next page or section" /></a></div><br /></div>
The disadvantage of using JavaScript is that the JavaScript-support must be enabled. The same functionality could be achieved by using only PHP for example at the following way (as an example going from the page Elementit0.php3 to Elementit.php3):
<?php
$next = "Elementit.php3";
$up = ...
?>
LINK element has been used, with PHP will generate attribute values from variables, for example rel="up" href="<?php print $up;?>"<?php print "...<a href=\"".$next."\"><img src=\"../Kuvat/buttons/Right.gif\" border=\"0\" alt=\"[Next]\" align=\"right\" title=\"The next section\" /></a>"; ?>
or
...<a href="<?php print $next;?>"><img src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="right" title="The next section" /></a>
Variables can be written also inside funtions and call assosiated functions, for example:
<?php
function nextSection(){
$next=...
print $next; /* Variables don't work if they have defined outside the function if they have not explicit declared within the function as global variables (for exampleglobal $next, ...;). */
...
}
...<?php nextSection();?>
<a href="">...
The easiest way is to predefine variable and if necessary variables have been in page level redefined. In all cases it is not necessary to redefine all variables in individual pages. It is however necessary to remember that variables takes the values, which are last defined before the associated functions. The following code doesn't work in any browser:
<link rel="up" href="<?php up();?> />
<?php
$up = "Content.php";
function up(){
global $up;
print $up;
}
?>
In that example I have marked links as variables. I handle using variable more in the next section.
| Sections of page: | First section | Previous section | Next section![]() | Last section![]() |