Peoii’s Place

Rediscovering myself, one post at a time…

Page ID

So you installed the PHP HTML Wrapper or something similar, and you’d like to be able to have some customizing as to what page you’re on eh? Well, there’s a very simple and straight forward answer to this problem fortunately, and it takes all of 2 minutes to get up and running.

The trick is in creating a $thisPage variable, which will store the value we can later reference in our header or footer to know what page we’re on. So, we’d take something like this for our news page:

<?php
  $thisPage 
“News”;
?>



So, if we couple that from the bits from our PHP HTML Wrapper, we’d get something like this:

<?php
  $thisPage 
“News”;
  include(
“header.php”);
  include(
“inc/news.php”);
  include(
“footer.php”);
?>



So this of course begs the question, why would we want to do this? Well, there are plenty of reasons, and these are only scratching the surface of what could be done with this one variable.

  1. Dynamic Title - Have something like: Peoii’s Place - News as your title
  2. Custom Style Sheet - Make each page render using a different stylesheet.
  3. Popularity Results - Find out which pages on your site people are visiting the most (very nice thing to have in a header)
  4. Script Usage - Hey, maybe you only want say a Shoutbox on a handful of pages, well, with this you can choose

So, now that we know what we can do with something like this, lets give a couple examples. First I’ll explain how to use the variable to modify the title, which is probably the most simple thing you could do with it. I mean heck, just check out this simple code:

<HTML>
  <head>
    <title>Peoii’s Place :: <?php print(“$thisPage”); ?></title>
  </head>
  <body>
……


And voila! You’re title will appear as (in this example) Peoii’s Place :: News. Nice little trick eh? Ok, so that was really easy, what about if you wanted to have say a custom style sheet on a page? That sounds a little more tricky, but I’ll guarentee you, once you understand how to do it, it’s not tricky at all! So here we go with some more code:

<HTML>
  <head>
    <title>Peoii’s Place :: <?php print(“$thisPage”); ?></title>
    <?php
    
if(strtolower($thisPage) == “news”) {
      echo 
“<link rel=\”stylesheet\” href=\”newsstyle.css\” type=\”text/css\” />\n”;
    } else if (
strtolower($thisPage) == “main”) {
      echo 
“<link rel=\”stylesheet\” href=\”mainstyle.css\” type=\”text/css\” />\n”;
    } else {
      echo 
“<link rel=\”stylesheet\” href=\”default.css\” type=\”text/css\” />\n”;
    }
    
?>
  </head>
  <body>
……


Ok, don’t panic, lemme walk you thru this a little bit, and we’ll discover what’s going on here. First off, I’ve taken the same code we had from our title experiment, and added only 1 new bit really to it, which is this:

    <?php
    
if(strtolower($thisPage) == “news”) {
      echo 
“<link rel=\”stylesheet\” href=\”newsstyle.css\” type=\”text/css\” />\n”;
    } else if (
strtolower($thisPage) == “main”) {
      echo 
“<link rel=\”stylesheet\” href=\”mainstyle.css\” type=\”text/css\” />\n”;
    } else {
      echo 
“<link rel=\”stylesheet\” href=\”default.css\” type=\”text/css\” />\n”;
    }
    
?>



So, now to explain what this piece of code does, lets start with this line:

    if(strtolower($thisPage) == ”news”) {


Looks simple enough, well, it really is. What we’re doing is saying this really:

IF THE VALUE OF $thisPage IS EQUAL TO news THEN

So whatever is coming in the {}’s will be done if our variable $thisPage is equal to news. But be careful here, because news and News may look the same, but they’re not as far as the code is concerned. So that’s why we used the function strtolower(), which gives us back the value of $thisPage in all lower case. Very handy indeed. Now that we know that, we can move on to the next line:

echo "<link rel=\"stylesheet\" href=\"newsstyle.css\" type=\"text/css\" />\n";

What this is doing is “echoing” or “printing” the link tag out to the screen, which gives us our stylesheet of “newsstyle.css” for our news page. So now we’ve got it setup for the news page, what comes next? We do an else if, which is to say “Ok, it’s not news, so is it this?” If it is, then we echo out the data that gives us “mainstyle.css”, if not, we move on to the final bit, which is the “else” without an if. This says “If we havn’t caught it yet, catch it now and do this!” That way even if we don’t have a special style sheet for our specific page, we have a default one that will happen, just incase.

Welp, that about sums up this Tutorial. It has more information than I was expecting to get into in it, but at the same time, I’m trying to make them very indepth until I get my “PHP Primer” tutorial up and running. Once that is up, I’ll probably go back thru and get rid of pieces like the IF explaination. But until then, I’m keeping this as is.

Enjoy.

2 Comments so far

  1. kReEsTaL November 13th, 2005 4:09 pm

    Hello! First thank you so much for sharing those tutorials, I think they’re really clear and useful. About this one, precisely: does the page ID work if you don’t create a PHP page for each page which will appear on your website (I’m using includes).

  2. Peoii November 15th, 2005 9:18 am

    Sure, I mean you could always have the page_id portion in whatever page you were including. There are many many ways to make this dynamic, so it all really depends on your specific methodology. If you’ve got an example of how you’re doing the include, I’d be more than happy to take a look at it with you and help you figure out the best way to get this concept accomplished within your work.

Leave a reply