Change a DIV in PHP Include

时间:2015-05-04 19:20:44

标签: php html

I'm new to this site and VERY new to HTML/PHP so pardon me if I do anything wrong... but here's my question:

I just started learning HTML through many Google searches. 2 weeks ago I had never touched HTML in my life. I'm building a site with 6 main pages: Home, Questions, Forums, Contact, About, and Help.

Each page has 4 main components:

Header (including horizontal navbar)

Sidebar

Main Content Area

Footer

The Header, Sidebar, and Footer are replicated on each page using the PHP INCLUDE command. This works great. Everything is working fine.

However, I don't exactly want the Header/Navbar to be 100% identical on each page. In my Header.HTML file (the file that is called upon by my PHP INCLUDE command) contains a DIV I rightfully called "headertext".

For example, on the home page, my headertext DIV says "Cool Stuff, Free of Charge!" (visually styled with CSS, but the text information is contained within the Header.HTML file) and it's centered nicely between the website logo and the website ad box, all within the Header area.

Since I'm using PHP INCLUDE, EVERY page on the site has this same line of text in the header. However... here's what I want:

When I navigate to my "ABOUT" page for example, I want the headertext div to become "ABOUT US" instead of "Cool Stuff, Free of Charge".. Get what I'm saying?

But I still want to use PHP INCLUDE to minimize the amount of code I have to sift through for each page's index.php file...

I hope all of this made sense.... While I am a newbie to this stuff, I've grown to become pretty good at HTML over the past 2 weeks. However, please note that I am brand new to PHP (as of today). So is this possible?

Thanks for any help!


UPDATE: SOLVED:

Thanks to Kainaw, I figured it out.

2 个答案:

答案 0 :(得分:5)

A very simplistic way to do it...

In the included header PHP file, make the text a variable:

if(empty($headertext)) $headertext = 'Cool Stuff, Free of Charge';

Then, when you print the data, do something like:

<div id='header'><?php echo $headertext; ?></div>

Finally, in your about.php file, before you include the header.php file, you set what you want the text to be (I'm guessing at the file names)...

$headertext = 'About Me Page';
include('header.php');

答案 1 :(得分:0)

Use the PHP include on all pages that will have the same / consist 'Global Header'. On unique pages, call a unique include or just omit any include on those pages and just have it static HTML.

Or wrap your header include with if statements conditions.

<?php if (is_page('1')) { include("page1_ads.php"); }

elseif (is_page('green-page')) { include("green-page-ads.php"); }

elseif (is_page('another-page')) { include("another-page-ads.php"); }

else { include("everything-else.php"); 

} ?>

Also, your question is very broad. Add some code, or demo links.