SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Page title with common header

    Using includes, I am using one header file for almost every page of my site. One problem this presents is that all these pages have the same title.

    I have figured out one way to get around this, but I'm a php newbie so there is probably a better way. I wanted to here some suggestions and compare them with what I came up with.
    Last edited by SVTBlackLight01; May 24, 2003 at 14:01.
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  2. #2
    SitePoint Zealot Anagram's Avatar
    Join Date
    Feb 2003
    Location
    Norway
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, you could have a common header, but with "open spaces" for variables, like name of the page and so on. So if you visit the "download" page, there's the common header, and a "Download" text or something with it. You have to send of course a $name variable to fill out the "fill-out-here" spaces in your header. Hope you understood that. The only problem is when visit the page directly, not by a link. Then you won't set the variables (like the $name) and the part of the include-file where variables can go,will be empty.

    Regards
    Anagram.
    If pigs could fly, the prize of bacon would reach the sky.

    www.dosspirit.net - Norwegian
    reviews of DOS games

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep - the easiest way is just to have your header with this inbetween the title tags:

    <title><?=$pagetitle?></title>

    and then, BEFORE you include it in your page simply put the title in

    ie
    PHP Code:
    <?
    $pagetitle 
    "downloads";
    include(
    'header.php');
    ?>

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK. I used this for the title in the header:

    <title><?
    if(!$title) {
    echo "MODern ENGINEuity - THE Place for SVT and Late Model Ford, Lincoln & Mercury Enthusiasts";
    }
    else{
    echo "$title";
    }
    ?></title>

    A default title is used if the page is visited directly and there is no value for the variable. Otherwise I can set the values with the navigational links.

    I like your idea platinum, I may go with that.

    Thanks for the ideas! [img]images/smilies/biggrin.gif[/img]
    Last edited by SVTBlackLight01; Apr 27, 2003 at 13:01.
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  5. #5
    SitePoint Evangelist ucahg's Avatar
    Join Date
    Apr 2001
    Location
    Sarnia, Ontario, Canada
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SVTBlackLight01
    OK. I used this for the title in the header:

    <title><?
    if(!$title) {
    echo "MODern ENGINEuity - THE Place for SVT and Late Model Ford, Lincoln & Mercury Enthusiasts";
    }
    else{
    echo "$title";
    }
    ?></title>
    The quotes around $title aren't needed (and decreaes performance), and you should test for
    if (!!isset($title)) {...
    rather than
    if (!$title) {...

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using (!!isset($title)) doesn't display the default title.
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  7. #7
    SitePoint Zealot oivaf's Avatar
    Join Date
    Apr 2003
    Location
    Mexico
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if (!!isset($title)) {...
    rather than
    if (!$title) {...
    i believe there is a tiny typing error (two '!' instead of one); it should be like this i think:
    PHP Code:
    if (!isset($title)) {...
    rather than
    if (!$title) {... 

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oivaf
    i believe there is a tiny typing error (two '!' instead of one); it should be like this i think:
    PHP Code:
    if (!isset($title)) {...
    rather than
    if (!$title) {... 
    That works. What is the advantage of using (!isset($title)) as opposed to the way I had it at first?
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  9. #9
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, isset does check if the variable is set but your way says, if $title is false then, which is well, not the best way since $title isn't even set, it works in php (too many things work in php!) but it is not the 'good' way.
    - website

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the help. It's working great!

    I have one question about it though.

    Is there a way to keep the variable strings from showing in the URL?
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  11. #11
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you talking about with forms or with your example above?
    I don't see how variables would be set in the url with what you are doing.

    If you are using forms then you would change the method to post from get and use either $HTTP_POST_VARS or $_POST to access the variables.

    Myself or someone can explain further if thats what you are talking about.

  12. #12
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Vancouver, USA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way you're doing it, no there's no way to hide the info in the bar. The better way to do it is the way platinum suggested, where you define the page title and then include the header.

  13. #13
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Vancouver, USA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LiamW
    I don't see how variables would be set in the url with what you are doing.
    They're set in the url because he's using the link to define the title something like
    Code:
    <a href="page.html?title=This+is+the+title">link</a>

  14. #14
    SitePoint Zealot LiamW's Avatar
    Join Date
    Mar 2003
    Location
    Hamilton, Ontario
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh. I missed the part where he said he was setting the title in the nav links.
    Sorry bout that.

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To hide the variable - w/out using $_GET[] why not use $_SESSION[] instead ? Wouldn't be that much more scripting to use instead ?

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2003
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    To hide the variable - w/out using $_GET[] why not use $_SESSION[] instead ? Wouldn't be that much more scripting to use instead ?
    Can this be used for my application?
    MODern ENGINEuity - Ford Modular Powered Community
    Automotive Books, Movies, Games and More!
    -----------------------
    King-Fans

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the start of every page you have the following:

    PHP Code:
    # using PHP4.2.x btw
    session_start();
    if(!isset(
    $_SESSION['PageTitle'])) {
    # no session so begin a new one
    $_SESSION['PageTitle']; // empty but active
    }
    .
    .
    # then when your ready...
    $_SESSION['PageTitle'] = 'Home Page... Welcome'
    Now when you build your page, you use the session variable like so:

    PHP Code:
    .
    .
    <title><?php echo($_SESSION['PageTitle']); ?></title>
    .
    .
    You need to declare your sessions at the very top of the page; doesn't matter if it's within the TEMPLATE or else where - you may want to INCLUDE the session part of the script.

    Doesn't matter; provided that you do NOT output anything before you declare those sessions, even blank lines etc.

    Also includeing sessions on every page will mean you can re-set the page title at any point during your script before you build the page.


Bookmarks

Posting Permissions

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