Forum Moderators: coopster

Message Too Old, No Replies

Includes in a certain order (newbie here)

         

silverhaze

5:13 am on Aug 13, 2005 (gmt 0)

10+ Year Member



Hello,
firstly, great community here, I've found great tips here many times searching google.

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]

mcibor

1:33 pm on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What are the errors? If none, then write on the beginning

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 }?>

to
<?php
my_func() {
echo "bla bla";
}?>

Just remember, to echo double quote in quotes use \": "<a href=\"link.com\">"
If this doesn't help write the errors
Michal Cibor

silverhaze

4:25 pm on Aug 13, 2005 (gmt 0)

10+ Year Member



I don't get any errors without <?php error_reporting(E_ALL);?> on. But it's not seeing the <?php $thisPage="Home";?> which gives class="current" to the appropriate <li> and appends "¦ Home" to the <title>.

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".

mcibor

9:36 am on Aug 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see the problem!
You need to pass that variable to function:

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>

Includes.php
<?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
}
?>

Hope this helps!
Somewhere in the library there's a whole topic about passing values to function
Michal Cibor

silverhaze

4:25 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



Thank you Michal! Got it all working now, all from one include file. Nice and slick.

I will definitely read up on passing values to functions, looks like a good place to start.