Forum Moderators: coopster

Message Too Old, No Replies

How do I define variable in template, not page

I want a pic and bgcolor to change depending on page that is called

         

Flash72

8:16 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



I'm using PHP

What I'm trying to do is have a jpg (or gif) and bgcolor change depending on what section (or page) is being viewed. I want this to happen from the template, or master header, to keep control over the site. I can currently define it on the product page, but this is not what I want.

For example,

I have a php template.php file (used by all product pages),
which includes:
header.html
content.html
footer.html

header.html contains:
html structure of the header and a pic and background color that I want to change depending on what page you view. But I don't want the product page to define the variable like it currently does...

header.html:
bgcolor="<?php echo "$bgcolor";?>">
<?php echo("<img src='/includes/headers/$headerpic'>");?>

product1.php contains:
<?php $headerpic = "product1.gif"; $bgcolor = "000000"; $content = "product1.html";
include $_SERVER['DOCUMENT_ROOT'] . "/includes/template.php";
?>

Thank You.

WhosAWhata

11:33 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



this is all a little confusing perhaps you could state your question a little more clearly? What are you asking?

Flash72

11:49 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



Sorry.

I have a header.html file that gets included in a template.php

when someone views a page product1.php (which uses the template.php)

I want the header.html to display a different picture and bgcolor than it would for product2.php

I can do this by defining the header pic in product1.php (ie $headerpic = "product1.gif") but I want to define it in the header.html, or the template.php if possible.

I just can't get the variables to work if I put this in the header.html file:
$headerpic[1] = "product1.gif"
$headerpic[2] = "product2.gif"

How do I call this in the product1.php page?

ergophobe

2:55 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't define something downstream and expect it to be available upstream.

This may be too little too late in terms of advice, but....

That's why it's best to design a template-based your site so that all processing occurs before any output. So rather than including file after file in your template, you define all page-specific variables ($header_image, $main_content, $message_to_my_aunt_in_the_footer) first, then you output them in the template. Make your that you assign default values to all variables.

Tom

Flash72

5:11 pm on Jun 11, 2004 (gmt 0)

10+ Year Member



How (or where) do I define the page (or folder)-specific variables?

Is there a way to define folder specific variables? That really is what I'm trying to do because the product1.php page will be in folder 1 (there are at least 200 pages in about 10 folders):

(use headerpic1,color1)
mypage.com/folder1/product1.php mypage.com/folder1/product2.php

(use headerpic2,color2)
mypage.com/folder2/product3.php
mypage.com/folder2/product4.php

if you could give an example, or point me to some text that describes this, I would greatly appreciate it.

ergophobe

5:44 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It will depend on the whole architecture of you site. One good article to start with to wrap your head around it is

[zend.com...]

Back to your original question and the easy way to do it. If you are building a path to a given directory, just put the different images in the different product directories.

You want the image to change based on the folder. Just use a relative path in your header so instead of

src="http://mydomain.com/images/header_image1.jpg
src="http://mydomain.com/images/header_image2.jpg

You have

src="http://mydomain.com/folder1/header_image.jpg
src="http://mydomain.com/folder2/header_image.jpg

It will be harder to maintain, but easy to set up in the interim.

Tom

isitreal

5:54 pm on Jun 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There's a pretty easy way to do that.

If you create an array of the page file name and image file names:

say this:

$a_image_data = array (
'page1.php' => 'page1.gif',
'page2.php' => 'page2.gif'
)

then use the server script name to tell the header what page it's on, like this:
$page_name = basename($_SERVER['SCRIPT_NAME'])
$header_graphic = $a_image_data[$page_name]

that's the rough idea, the page file name is the index for the image array, and you just extract the correct image using that information, that would require almost no changes in your header include. Or you could use the whole path to root and get rid of basename and compare the root path in the array and the server script name directly, that way you could have pages with the same name in different folders, I use this method on many sites and it works very well, although I use it for different reasons, to tell the navigation bars whether to reveal submenus or not.