Monday, June 23, 2008

Adding Dynamic PHP To Header Or Footer


If you are wanting to pull in some dynamic PHP content on a certain page, perhaps in the header or footer section, this can be done very easily. often times I like certain text to show up in the header on a certain page. Often times maybe in the h2 tag. First copy the code below into the header.php section before the last </head> tag.

<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

Next if pulling text you may want to open notepad and try this test file. Copy below code and paste it in notepad and save as test.php on your harddrive. Then upload test.php to your website in the root section of your directory.

<h2>This is a test!</h2>

Next in the header section you can place the code below where you want the text to show. Perhaps up top in the header or underneath the search or wherever. The below example we will make the text show only on the homepage.

<?php
if($mainCat==""){
include("test.php");
}
?>

You can also tell it to display on another content page as well. Lets say you have a content page named history. The code would be like this below.

<?php
if($mainCat=="history"){
include("test.php");
}
?>

You can even display it on a certain store page using the curPageURL function. Lets say the location of a store page was http://www.yourdomain.com/store/coolstuff

You would have a command below in your header.

<?php
if (curPageURL()=="http://www.yourdomain.com/store/coolstuff"){
include("php/test.php");
}
?>

This would make that text show on that page.

You can also set it up to pull different php files for different pages. Say you created 3 different php files of different text and wanted each one to show on a different page. Lets say you named these test.php test1.php and test2.php

You want test.php to show on the home page, test1.php to show on the history page and test2.php to show on the store page namedcoolstuff. You can put all of the code together like below.

<?php
if($mainCat==""){
include("test.php");
}
?>
<?php
if($mainCat=="history"){
include("test1.php");
}
?>
<?php
if (curPageURL()=="http://www.yourdomain.com/store/coolstuff"){
include("php/test2.php");
}
?>

Wherever you place the code above in the header is where the text will show. This is a broad brush stroke on how to do this. It will take some playing around on your part and I highly advise you back up your header and footer files before playing around with it in case you mess something up. I use this feature in almost every BANS site I build. It is very easy to do once you play around with it.

Related Posts by Categories



1 comments:

Brian on July 31, 2008 at 9:51 PM said...

This was just the information I was looking for. I knew how to do the include with the mainCat== step, not the curl. Thanks for the help!

Followers

Twitter Updates

    follow me on Twitter
     

    Build A Niche Store Blog. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted by Bloganol dot com Privacy Policy