SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using php to create page titles...how can i make this work?

    okay...im still pretty new to php but i want to use php to make it easy to change my page titles...here's what i wanna do:
    Code:
     
    <title><?=$page_title ?></title>
    i wanna use that obviously to call the title...but my question is...how do i call the file that has:
    Code:
     
    <?php
    $page_title = 'Page Title Here';
    ?>
    any suggestions?

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm.... Maybe you need to use INCLUDEs ? Whereby you can then INCLUDE any file you want, in your case the file which holds the variable that has the pages title ?

    Not very complicated to use either, for example

    PHP Code:
    .
    .
    @include(
    'filetoinclude.php'); 
    .

    Notice that I use @ to avoid an error being displayed if for some reason you cannot INCLUDE a file ? Btw this works for most of PHPs functions as well

    Did I understand your question I hope ?

  3. #3
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know how to use includes...but im not very sure how to make the <?=$page_title ?> actually input the title....basically i just wanna know how to make that little snippet in the title tags call the title for the page..

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    vancouver, bc
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    i know how to use includes...but im not very sure how to make the <?=$page_title ?> actually input the title....basically i just wanna know how to make that little snippet in the title tags call the title for the page..

    you have to echo() your variables.

    eg.

    PHP Code:

    $title 
    "my home page"



    Code:
    <title><?php echo $title;?></title>

  5. #5
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    i know how to use includes...but im not very sure how to make the <?=$page_title ?> actually input the title....basically i just wanna know how to make that little snippet in the title tags call the title for the page..

    If each script is separate, i.e. they are not all loaded from index.php, I might do the following:

    title.php
    PHP Code:
    <?php
    switch ($_SERVER['PHP_SELF']) {
       case 
    'page1.php':
          
    $title "This is page 1!!!";
          break;
       case 
    'page2.php':
          
    $title "This is page 2!!!";
          break;
       default:
          
    $title "No page title defined!"// (or Welcome to my pages!)
    }

    echo 
    $title;
    ?>
    And then:
    PHP Code:
    <html>
    <head>
    <title><?php include ('title.php'); ?></title>
    </head>
    <!--...-->
    (Not tested!)
    Last edited by lieut_data; Sep 10, 2003 at 13:01.
    My name is Steve, and I'm a super-villian.

  6. #6
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by edwinx
    you have to echo() your variables.

    eg.

    PHP Code:

    $title 
    "my home page"
    Code:
    <title><?php echo $title;?></title>
    Well, that's what he was doing:
    PHP Code:
    <?=$page_title ?>
    Is, with short-tags enabled, the same as

    PHP Code:
    <?php echo $page_title?>
    My name is Steve, and I'm a super-villian.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    vancouver, bc
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oops! sorry. my bad. i didnt' even know there was a function called short tags.

  8. #8
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didn't think things in the <head> tags could use includes...but im about to try..

  9. #9
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get this error in my title using that:
    Code:
    <br />
    <b>Parse error</b>:  parse error in <b>/dir/includes/title.php</b> on line <b>11</b><br />

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Short tags -- yechhh! Don't use 'em, they reduce code portability. Back to the original question...

    You might have a file, header.inc.php, that looks like this:
    Code:
    <html>
    <head>
    <title><?php echo $title ?></title>
    </head>
    <body>
    And then you have another file, example.php, that looks like this:
    PHP Code:
    <?php

    $title 
    'example page title';
    include 
    'header.inc.php';

    ?>
    ... content ...
    Right?

  11. #11
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    i didn't think things in the <head> tags could use includes...but im about to try..
    Huh? You can use includes (or requires, if you prefer) anywhere. The content of the file gets parsed on the server before the page hits the browser, so PHP couldn't care less where you place the include. HTML, on the other hand, gets a bit picky.

  12. #12
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay...everything works now...i just used an include to call a simple page w/ the title i wanted...

  13. #13
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do it like this:
    <?php $page_title="Whatever page name you want"; include('header.php'); ?>

  14. #14
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikeall
    You could do it like this:
    <?php $page_title="Whatever page name you want"; include('header.php'); ?>
    Isn't that what I said?

  15. #15
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattjacob
    Isn't that what I said?
    Yes (post above, above sounds like a shot at a free post-increase)

    [Then what is this post, Lieut?]

    Erm...
    My name is Steve, and I'm a super-villian.

  16. #16
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I believe Matt's question was rhetorical, Data hehe.
    Mike
    It's not who I am underneath, but what I do that defines me.

  17. #17
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
    switch ($_SERVER['PHP_SELF']) { 
       case 
    'page1.php'
          
    $title "This is page 1!!!"
          break; 
       case 
    'page2.php'
          
    $title "This is page 2!!!"
          break; 
       default: 
          
    $title "No page title defined!"// (or Welcome to my pages!) 

    echo 
    $title
    ?>
    not sure what "$_SERVER" does..but either way....wouldn't it be better to do something like this:
    PHP Code:
     <?php
    $currentsite 
    = ??????? 
    switch (
    $currentsite){ 
    case 
    'page1.php'
    $title "This is page 1!!!"
    break; 
    case 
    'page2.php'
    $title "This is page 2!!!"
    break; 
    default: 
    $title "No page title defined!"// (or Welcome to my pages!) 

    echo 
    $title
    ?>
    and have $currentsite call whatever page is currently being displayed? and how would i make $currentsite equal the URL of whatever page is currently being displayed?

  18. #18
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    and have $currentsite call whatever page is currently being displayed? and how would i make $currentsite equal the URL of whatever page is currently being displayed?
    That would be what $_SERVER['PHP_SELF'] does!

    On this page, $_SERVER['PHP_SELF'] would return 'show_thread.php', which could then be evaluated for a title -- either that or you could write:

    PHP Code:
    $current_page $_SERVER['PHP_SELF'];
    switch (
    $current_page) {...} 
    But it would be redundant
    My name is Steve, and I'm a super-villian.

  19. #19
    SitePoint Zealot scriptfactory's Avatar
    Join Date
    Oct 2003
    Location
    Kaiserslautern, Germany
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_server['php_self']

    Edit: I was too slow. Oh, well. It seems like a lot of effort for only a page title. What I do is use a templating system called Smarty for all of my websites. That way I can dynamically control the page content from one page, index.php AND automatically reuse the same page layout.

  20. #20
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems like a lot of effort for only a page title.
    well the reason i was doing that is because i have numerous sections w/ different titles...either way....lieut data...the script keeps going to the default value instead of any of the cases...any other ideas or suggestions?

  21. #21
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    well the reason i was doing that is because i have numerous sections w/ different titles...either way....lieut data...the script keeps going to the default value instead of any of the cases...any other ideas or suggestions?
    Might be case sensitive?

    Try:

    PHP Code:
    switch (strtolower($_SERVER['PHP_SELF'])) {
       case 
    'lower_case_page1':
          break;
       case 
    'lower_case_page2':
          break;
       
    //...

    My name is Steve, and I'm a super-villian.

  22. #22
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried the lowercase thing...and for whatever reason it just seems to keep going to the default choice....odd..

  23. #23
    La la la la la bronze trophy lieut_data's Avatar
    Join Date
    Jun 2003
    Location
    Waterloo, ON
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RockNRollPig
    i tried the lowercase thing...and for whatever reason it just seems to keep going to the default choice....odd..
    Try outputing :

    PHP Code:
    echo $_SERVER['PHP_SELF']; 
    And can you post your code, as is?
    My name is Steve, and I'm a super-villian.

  24. #24
    Formerly known as RockNRollPig Shpigford's Avatar
    Join Date
    Dec 2002
    Location
    Denver, CO
    Posts
    2,877
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    alrighty lieut data....i think i got it...i was typing in the the whole URL for each case but i just needed to type the current file "/action-games.php" as opposed to "http://website.com/action-games.php"....thanks a ton for your help!!!


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
  •