Page 1 of 2

Page Title by page= with includes

Posted: Wed Sep 01, 2004 8:12 pm
by chrislive
Firstly id like to say that im very new to PHP and am teaching myself, however this has me stumped...

I am wanting to print a page title depending on which page the user has browsed to...

I tried using if($page == contacts){echo "Contact Details";}
but because im using includes for my templating is does not seem to work.

This is the setup.

I will use pages.php as an example.

pages.php contains:

Code: Select all

<?php
include("header.php");
include("content.php");
// $page information also in here but not used in here
// so pages.php has multiple ?page=page1 ?page=page2 etc
echo "the contents of this page";
include("footer.php");
?>
then content.php contains:

Code: Select all

<?php
//content details (table details etc for title bar)
include("title.php");
?>
the title.php contains the script to check the $page etc and then prints the appropriate title. However currenty it is looking at the $page for itself not for pages.php... does that make sense?

So i have a couple of options I guess.. the most logical one for me is to ensure that the page= information is passed into the included pages. but how to do this.

I tried using in the pages.php
$currentpage = $_PAGE;

but if i echo $currentpage from content.php it does not return anything. How do i pass that value in?

Your help would be greatly appreciated

Posted: Wed Sep 01, 2004 8:16 pm
by feyd
foo.php?page=bar

$_GET['page'] == bar

Posted: Wed Sep 01, 2004 8:19 pm
by chrislive
could u please elaborate more... like i said im a newbie here..

$_GET['page'] == bar

will do what?

Posted: Wed Sep 01, 2004 8:23 pm
by feyd
assume you go to this page: foo.php?page=bar
assume you have this page's script looks like so:

Code: Select all

<?php

echo $_GET['page'];

?>
this script outputs:

Code: Select all

bar

for more details about $_GET and it's siblings:
http://www.php.net/manual/en/reserved.variables.php

Posted: Wed Sep 01, 2004 8:26 pm
by chrislive
i had done this.. but when I include the page it doesnt work because its trying to give me the page info for title.php NOT pages.php

know what i mean?

I need a way to ensure that the information is passed from pages.php to title.php

Posted: Wed Sep 01, 2004 8:30 pm
by feyd
any variables set in pages.php before the inclusion of title.php will be available to title.php.. this works going out from title.php back to pages.php

Posted: Wed Sep 01, 2004 8:46 pm
by chrislive
its not working....

within title.php i have the following:

Code: Select all

<?php
if($currentpage == "about")&#123;echo "About Us";&#125;
if($currentpage == "")&#123;echo "Welcome";&#125;
?>
then in content i just make an include("title"); plus some formatting for text etc
then in pages.php i have

$currentpage = $_GET['page'];

is this correct??

thanks for your help..

Posted: Wed Sep 01, 2004 8:50 pm
by feyd
assuming the following url and scripts:
foo.php?page=about
pages.php:

Code: Select all

<?php

$currentPage = isset($_GET['page']) ? $_GET['page'] : '';

include 'content.php';

?>
content.php:

Code: Select all

<?php

include 'title.php';

?>
title.php:

Code: Select all

<?php

var_dump($currentPage);

?>
should output similar to

Code: Select all

string(5) "about"

Posted: Wed Sep 01, 2004 8:55 pm
by chrislive
prints NULL

could this be because its an include in an include???

Posted: Wed Sep 01, 2004 9:00 pm
by chrislive
ok ok getting somewhere...
i have my content.php and title.php in the includes/ folder... if i create a file called test.php in the same folder and do what you say it works...

Posted: Wed Sep 01, 2004 9:01 pm
by feyd
fix your include_path and you won't have this problem then.

Posted: Wed Sep 01, 2004 9:02 pm
by chrislive
how do you mean? like i said im a newbie :P

thanks for all your help though

Posted: Wed Sep 01, 2004 9:06 pm
by feyd

Posted: Thu Sep 02, 2004 4:26 am
by chrislive
I have looked through these but cant get a solution that works.. this is not my server so i cant set the includes directory through the php.ini and the cose that they show does not seem to work...

here's my setup:

i have an isp that hosts my website... http://www.mywebsite.com
i have created a sub-folder called aceair which has its own website
so within aceair i have my includes folder called "includes".

Code: Select all

// Works as of PHP 4.3.0
set_include_path('/includes');

// Works in all PHP versions
ini_set('include_path', '/includes');
i have also tried other paths like 'includes','aceair/includes','./aceair/includes'

what am i doing wrong!! this is driving me crazy!! hehe

Posted: Thu Sep 02, 2004 4:41 am
by timvw
chrislive wrote:
set_include_path('/includes');

i have also tried other paths like 'includes','aceair/includes','./aceair/includes'
/includes is an absolute path, i don't think you have a includes dir in your root dir.

includes, aceair/includes and ./aceair/includes are relative paths, and thus differ from your current working directory.


Put the following in your webroot

Code: Select all

<?php
echo $_SERVER['DOCUMENT_ROOT'];
?>
this will give you an absolute path (in my case it is /home/timvw/www/home.mysth.be/)

thus i'd have /home/timvw/www/home.mysth.be/includes as include_path