Forum Moderators: coopster
So I've been using basic PHP includes for a while, usually like this:
<?php require("$_SERVER[DOCUMENT_ROOT]/c/header.php");?>
<?php require("$_SERVER[DOCUMENT_ROOT]/c/navigation.php");?>
content here
<?php require("$_SERVER[DOCUMENT_ROOT]/c/footer.php");?>
Today I thought I'd get smart and put all my includes in one file, I found [webmasterworld.com...] and after some trial and error I got it working.
Figured out that you can't use "function header(){" because "header" is a PHP command? "my_header" worked fine.
So anyway my problem now is that the other PHP I use for navigation doesn't work now, presumably because the PHP is being called in the wrong order.
Here is what I have:
Main PHP page:
<?php $thisPage='Home';?>
<?php require("$_SERVER[DOCUMENT_ROOT]/c/includes.php");?>
<html>
<?php my_head();?>
<body id="about">
<?php my_header();?>
<?php my_sidebar();?>
<?php my_footer();?>
</html>
Master Include File:
<?php
function my_head(){
?>
head stuff
<?php
}
function my_header(){
?>
<h1>Header</h1>
<?php
}
function my_sidebar(){
?>
<ul>
<li><a href="/" <?php if ($thisPage=='Home') echo 'class="current"';?>>Home</a></li>
<li><a href="/about/" <?php if ($thisPage=='About Us') echo 'class="current"';?>>About Us</a></li>
<li><a href="/services/" <?php if ($thisPage=='Services') echo 'class="current"';?>>Services</a></li>
<li><a href="/gallery/" <?php if ($thisPage=='Gallery') echo 'class="current"';?>>Gallery</a></li>
<li><a href="/resources/" <?php if ($thisPage=='Resources') echo 'class="current"';?>>Resources</a></li>
<li><a href="/contact/" <?php if ($thisPage=='Contact Us') echo 'class="current"';?>>Contact Us</a></li>
</ul>
<?php
}
function my_footer(){
?>
<p>Footer</p>
<?php
}
?>
So I'm wondering if it's possible to get them both working together. Hopefully that makes some sense to someone, any ideas would be very appreciated. Sorry if the code sample is too big.
[edited by: jatar_k at 4:04 pm (utc) on Aug. 14, 2005]
[edit reason] removed url [/edit]
error_reporting(E_ALL);
However discard any Notice:
Moreover look into your source code and see if the function output something.
If not, then you may have to change your structure from
<?php
my_func() {
?>
bla bla
<?php }?>
<?php
my_func() {
echo "bla bla";
}?>
When I do turn error_reporting on I get
<b>Notice</b>: Undefined variable: thisPage in <b>/home/me/public_html/test/c/includes.php</b> on every line in the include file that references "thisPage".
Main PHP page:
<?php $thisPage='Home';?>
<?php require("$_SERVER[DOCUMENT_ROOT]/c/includes.php");?>
<html>
<?php my_head();?>
<body id="about">
<?php my_header();?>
<?php my_sidebar($thisPage);?>
<?php my_footer();?>
</html>
<?php
function my_head(){
?>
head stuff
<?php
}
function my_header(){
?>
<h1>Header</h1>
<?php
}
function my_sidebar($thisPage){
?>
<ul>
<li><a href="/" <?php if ($thisPage=='Home') echo 'class="current"';?>>Home</a></li>
<li><a href="/about/" <?php if ($thisPage=='About Us') echo 'class="current"';?>>About Us</a></li>
<li><a href="/services/" <?php if ($thisPage=='Services') echo 'class="current"';?>>Services</a></li>
<li><a href="/gallery/" <?php if ($thisPage=='Gallery') echo 'class="current"';?>>Gallery</a></li>
<li><a href="/resources/" <?php if ($thisPage=='Resources') echo 'class="current"';?>>Resources</a></li>
<li><a href="/contact/" <?php if ($thisPage=='Contact Us') echo 'class="current"';?>>Contact Us</a></li>
</ul>
<?php
}
function my_footer(){
?>
<p>Footer</p>
<?php
}
?>