1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP echo title ...

Discussion in 'PHP' started by Carly, Feb 28, 2008.

  1. #1
    Hello. I have a web site to fix and clean up, and it's in php includes.

    Some documents currently have keywords, titles and description defined in each individual page by variables, and these are then called into the header.php file, so:

    <title><?php echo $title ?></title>

    Ok, remember how I said some of the pages? Well there are about 2000 perhaps, 3000 pages that DON'T! I'd like to somehow use a find and replace type thing in dreamweaver and where the title of the page is in the header tags, I'd like to surround that with something like this:

    <h2><?php $title= "Test page"; ?><?php echo $title ?></h2>

    This echos the page title fine, but doesn't actually put it in the title bar. If someone could figure our why, or even figure out a mass way of doing this, or a tool I could generate and insert with I'd be so greatful I'd make you ten cups of tea.

    Thanks....
     
    Carly, Feb 28, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    In Dreamweaver, click CTRL + F

    - Find in: Folder
    - Search: Source code

    Find: Whatever you want to find
    Replace: Whatever you want to replace it with

    -> Replace all



    Make backups before replacing... just in case.
     
    nico_swd, Feb 28, 2008 IP
  3. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, that doesn't actually answer my question.

    I know how to mass find and replace.

    My question is, if I'm typing:

    <h2><?php $title= "Test page"; ?><?php echo $title ?></h2>

    Why doesn't the title bar say 'test page' - the code in the header.php file is correct too: <title><?php echo $title ?></title>

    Could anyone shed some light on this? OR perhaps suggest a better way of giving all 2000 pages a unique title tag!
     
    Carly, Feb 28, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You mean in Dreamweaver, or your server?

    This code:
    
    <h2><?php $title= "Test page"; ?><?php echo $title ?></h2>
    
    PHP:
    ... WILL output:
    
    <h2>Test page</h2>
    
    Code (markup):
    If it doesn't for you, then you're something different which you're not telling us.
     
    nico_swd, Feb 28, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    It sounds like you're setting the <?php $title = "TEST" ?> after the header is called. It needs to be set before it can be echo'd out.

    Not positive on this without seeing the code though.
     
    jestep, Feb 28, 2008 IP
  6. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm echoing $title, then setting title, (which works on other pages) then I'm echoing it again. Make sense?
     
    Carly, Feb 28, 2008 IP
  7. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    My CODE:

    <?php
    $pagename = "Family06";
    $keywords = "keywords";
    $description = "Welcome...";
    include 'header.php';
    include 'menu.php';
    ?>

    <h2><?php $title= "Test page"; ?><?php echo $title ; ?></h2>

    --------HEADER CODE:
    <title><?php echo $title ?></title>
     
    Carly, Feb 28, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Then you're doing something different there. It's not possible to echo a variable before defining it.
     
    nico_swd, Feb 28, 2008 IP
  9. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I was about to disagree with you and say it works on every other page, but I realised, it is 'defined' then the header is CALLED after.


    <?php
    $title = "LAW TEACHER - new chiefs will help to get CPS back on track.";
    include 'header.php'; <-- there see!
    ?>


    So is there anyway I can do this then... any way at all.

    I can't think of any solution at all now...
     
    Carly, Feb 28, 2008 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    So what is your actual question?

    How to give each page a unique name/title, without having to define the title before including the header file?


    Maybe you could do something nasty with the output buffer. Do all your files have an include for the footer?
     
    nico_swd, Feb 28, 2008 IP
  11. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yeah, that's pretty much my question. I never coded the site, and the whole thing is very SEO unfriendly... There are millions of pages and by millions I mean over 13 thousand, perhaps even more than that as site anaylizer might only check pages linked from the site map.

    I don't fancy going through all of those to add a title/meta - but I'd love to find a quick way if I can.

    Yes, it's all PHP includes.. header/menu - mainpage - footer.

    For now I have a conditional statement say if $title is set, echo it, if it isn't echo 'this generic one.'

    But I'd love a way of setting a unique and relevent title for each page, somehow using the pages H tag - <h2>title</h2>

    I hope that's a little clearer. :)
     
    Carly, Feb 28, 2008 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    I see, I have an idea. Place this in your header file:
    
    ob_start();
    
    PHP:
    ... right after the <?php tag.

    And place this in the last lines of your footer:
    
    <?php
    
    $buffer = ob_get_contents();
    
    if (strpos($buffer, '<title></title>') !== false AND preg_match('~<h2>([^<]+)</h2>~', $buffer, $title))
    {
        $buffer = str_replace('<title></title>', "<title>{$title[1]}</title>", $buffer);
    }
    
    ob_end_clean();
    echo $buffer;
    ?>
    
    PHP:
    I didn't test it, 'cause I was too lazy to reconstruct your site logic. :p

    But give it a try...
     
    nico_swd, Feb 28, 2008 IP
  13. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Hey, I've just got back to work and seen your reply.

    Before I test it could you explain what it does to me, cus then I'll love you forever and make you lots of cups of tea and bring you cake.

    :D
     
    Carly, Feb 29, 2008 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    ob_start() turns on output buffering. That means no output is sent to the browser until we tell PHP to.

    Then we read the output in a variable, and we check if there's a title in the <title> tags. If not, we check if there's a title between the <h2> tag, and if so, we put the same in the <title>... and finally output everything.
     
    nico_swd, Feb 29, 2008 IP
  15. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    That sounds good... I'm going to give that a try thank you.

    Choose a cake from pimpmysnack.com and I'll send it to you in return. Honest. Hehe.
     
    Carly, Feb 29, 2008 IP
  16. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #16
    So did it work?

    And thanks. :D I'll pick one later!
     
    nico_swd, Feb 29, 2008 IP
  17. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Looking at some of the code above, it appears the $title might be in the wrong place

    basically your <title></title> tag needs to be inside the <head></head> tag, and the head tag needs to be before the <body></body> tag.
    something like
    
    <html>
    <head>
    <title><?=$title;?></title>
    </head>
    <body>
    
    HTML:
    Hopefully that helps, obviously you'll need to set $title before you use it .


    Also just a note, you might want to look at the Smarty templating system - it helps with templating and makes life easier =)
     
    powerspike, Mar 1, 2008 IP
  18. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #18
    The first point was already suggested, and the second falls out of question, 'cause he has too many files and doesn't want to edit them all.
     
    nico_swd, Mar 1, 2008 IP
  19. Carly

    Carly Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Erm... 'she' I'll have you know! Heh!
     
    Carly, Mar 3, 2008 IP
  20. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #20
    Oops. :D Sorry about that.

    And you never told me if it actually worked!
     
    nico_swd, Mar 3, 2008 IP