www.webdeveloper.com
Results 1 to 15 of 15

Thread: Dynamic page - title?

  1. #1
    Join Date
    Nov 2004
    Posts
    9

    Question Dynamic page - title?

    Hi,

    I’m making a website system that will automatically include the header, content and footer. This is my code:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Home Page</title>
    <link href="design/styles/main.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <?php require("design/includes/header.htm"); ?>
    <?php 

    $pagesList 
    = array( "default""missing""help""website design" ); 

    $page = isset( $HTTP_GET_VARS['page'] ) ? strtolower$HTTP_GET_VARS['page']) : 'default'

    $check = @include($file); 

    if (
    in_array($page$pagesList) ) { 

       include( 
    $page.'.php' ); 



    elseif (!
    $check

      include(
    'missing.php'); 


    ?> 
    <?php require("design/includes/footer.htm"); ?>
    I have a problem with the page titles, I have thought about using “<title>Site - <? echo $_GET['page']; ?></title>” and to use the URL “?page=Website%20Design” but I’d rather not. What can I do?

  2. #2
    Join Date
    Nov 2004
    Posts
    155
    How are you getting to the page? If it's one page that a user clicks a link (thus the URL '?page=Web%20Design') then you could instead use a cookie, or a session. That way it gets rid of the need to pass the variable through the URL.

    If I'm completely misunderstanding your question, I blame Warcraft and the obsession that requires me to stay up late.

    Jaelan
    "I killed the world once! You can too, if you try hard!"
    --Lews Therin,
    Robert Jordan's "The Wheel of Time"

  3. #3
    Join Date
    Nov 2004
    Posts
    9
    Hehe, yea I don’t mind about the variables in the URL.

    I want to change the actual page title.

  4. #4
    Join Date
    Nov 2004
    Posts
    155
    Have you tried:
    PHP Code:
    <title>Site - <? echo urldecode($_GET['page']); ?></title>
    "I killed the world once! You can too, if you try hard!"
    --Lews Therin,
    Robert Jordan's "The Wheel of Time"

  5. #5
    Join Date
    Nov 2004
    Posts
    9
    What will that do, just get the ?page= variable dispalyed as the page title?

  6. #6
    Join Date
    Nov 2004
    Posts
    155
    I'm not sure what you're wanting to do....consider this example to explain what that line of code does:

    the url is: http://www.somedomain.com/index.php?page=Web%20Design

    when you: echo urldecode($_GET[page]);, in the title tags, it displays Web Design, and not Web%20Design


    If that's not what you're asking, I will throw a right back at you
    "I killed the world once! You can too, if you try hard!"
    --Lews Therin,
    Robert Jordan's "The Wheel of Time"

  7. #7
    Join Date
    Nov 2004
    Posts
    9
    Yea, I’ve already tried that and I know it displays Website Design.

    What I want to know is how I can add an extra array so when “domain.com/index.php?page=website_design” is requested it can display anything like “Website Design Page” and so on for all the pages.

  8. #8
    Join Date
    Jan 2003
    Location
    Windsor, UK
    Posts
    367
    What I use for the sites I manage is the following:

    I've simiplified out all the meta, encoding info, doc types, etc.. I have it setup to display a different image on differents page and I've removed that aswel for the example so... anyway....

    3 Files -
    head.inc.php - containing all the header information
    foot.inc.php - containg all the footer information
    pagename.php - the contentm plus the calls to include the head and foot files.

    In the head.inc.php file I have the following:
    PHP Code:
    <?php
    if ($pgname == "") {
        
    $pgname "Default Page Title";
    }
    $title $pgname .  " - BA Silver Wing Sailing Club ";
    ?>
    <html>
    <head>
    <title><?php print($title); ?></title>
    </head>
    And then in pagename.php I have
    PHP Code:
    <?
    $pgname 
    "Join Us"
    include(
    "head.inc.php"); 
    ?>
    <p>Content Here</p>
    <? include("foot.inc.php"); ?>
    I'm sure you can follow what it's doing, setting the variable when the page is requested, and then passing it on to the header file which then adds - BA Silver Wing Sailing Club to the end of it, so that's on every page, and you can change the first bit for each page by changing the $pgname string, so you end up with titles like the following for example:
    Welcome - BA Silver Wing Sailing Club
    Join Us - BA Silver Wing Sailing Club
    Contact - BA Silver Wing Sailing Club


    If a title isnt set, it uses the 'Default Title' instead.
    Which you can have as an echo, like I put in above, or you can use like I do on some of my sites: (date($format)); which sets the date.

    You can add all sort of extra things to it, like I also have
    PHP Code:
    if ($imgname == "runrandom") { include("s/ranimg.php"); }
    else { 
    $imgloc "images/".$imgname; } 
    in the header file, and
    PHP Code:
    $imgname "history.jpg";
    $imgalt "WASC - 1994"
    in the page file.

    What this does, it allows me to have a different image at the top of every page, by setting the name of the image in the image directory, history.jpg, main.gif, contact.jpg, location.gif, etc... But if I set $imgname string as the text - runrandom then it runs the random image script and displays a random header image on that page.

    I hope some of that makes some sort of sense.

    Gav
    Last edited by GavinPearce; 12-04-2004 at 11:53 AM.
    Have I really been coming here for 6 years? You would have thought I would have learnt something by now!

  9. #9
    Join Date
    Aug 2003
    Location
    Oslo, Norway
    Posts
    50
    If you don't have a lot of pages you want to administer the titles of, you could just delete the <title> tag on your index.php page and instead put it on your included pages. Index.php will use that <title> tag. Like this:

    index.php has no <title> tag
    test.php has a <title> tag ( <title>Test</title> )
    beer.php has a <title> tag ( <title>Beer</title> )

    index.php?page=test will have the title Test
    index.php?page=beer will have the title Beer

    But, if you have a few hundred pages to maintain, gavinnet's suggestion makes perfect sense.

    Regards
    BluesMan

  10. #10
    Join Date
    Nov 2004
    Posts
    155
    I'm not sure how well this would work (but in my mind's eye, it will work, dammit!!)

    have an array:

    PHP Code:
    $array = array("Title 1 Page""Title 2 Page""Title 3 Page"); 
    Then, the URL would be www.somedomain.com/index.php?page=1

    The the title would be:

    PHP Code:
    <TITLE><? echo $array[$page]; ?></TITLE>
    Of course, this would make the title 'Title 2 Page' being the nature of arrays and starting at 0.

    Is this what you wanted?

    Jaelan
    "I killed the world once! You can too, if you try hard!"
    --Lews Therin,
    Robert Jordan's "The Wheel of Time"

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,178
    I have done something like this:
    PHP Code:
    ### functions.txt ###
    function header($title)
    {
      echo <<<EOD
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang='en'><head><title>
    $title</title>
    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
    <LINK REL=stylesheet HREF='style.css'>
    </head>
    <body>
    EOD;
    }
    function 
    footer()
    {
      echo 
    "</body></html>";

    PHP Code:
    ### some_page.php ###
    <?php
    include "functions.txt";
    header("This Page's Title");
    ?>
    <h1>This Page's Heading</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
    quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
    dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
    sunt in culpa qui officia deserunt mollit anim id est laborum</p>
    <?php
    footer
    ();
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Jan 2003
    Location
    Windsor, UK
    Posts
    367
    Something I forgot to add is that I tend to use head.inc.php instead of just ending it with the .inc and then I echo it out.

    This doesn't really achieve anything except it stops you viewing the contents of head.inc.php direct, which does nothing really except people can't find out what info is teh page info and what info is the header info.

    Stupid I know but just seems to me to be better than head.inc

    lol I'll shut up now.
    Have I really been coming here for 6 years? You would have thought I would have learnt something by now!

  13. #13
    Join Date
    Nov 2004
    Posts
    9
    Hi! A bit of a delay in my response!

    Thanks for your suggestions, but I decided not to use the variable URL (?page=) instead I’m using separate PHP pages which include the header after the title tag:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html lang="en">
    <head>
    <title>Page! - Test</title>
    <?php require("inc/header.php"); ?>
    Thanks for all your help!

    Regards,

  14. #14
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    If you use the following if the page is called dogs.php the title will be dogs:
    PHP Code:
    <title><?php print(str_replace('.php'''$_SERVER['SCRIPT_NAME'])); ?></title>
    Last edited by bokeh; 04-12-2005 at 02:32 PM.

  15. #15
    Join Date
    Feb 2005
    Location
    BC, Canada
    Posts
    1,098
    $titles['index.php'] => 'Main';
    $titles['whatever.php'] => 'TEST';

    echo $titles[basename($_SERVER['PHP_SELF'])];



    Put that in your header file then make a title value for each page.

    [Oops] Just read you have it finished. Sorry.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles