tags:

views:

353

answers:

5

My code looks like:

index.php:

<html>....  
    <title>My page</title>

....

<?php
    switch ($_GET['id']){  
        case "tips":  
            include("tips.php");  
            $title = "Tips";
...

How do I get the title varible to the html title tag?

All pages pass through index.php.

+9  A: 

Do your PHP before your HTML output.

<?php

  switch ($_GET["id"]) {
    case "tips":
      $title = "Tips";
      $page  = "tips.php";
  }

?>
<html>
 <head>
  <title><?php print $title; ?></title>
 </head>
 <body>
  <?php include($page); ?>
 </body>
</html>
Jonathan Sampson
No, I start with the html head.First line of PHP comes after <body>
Johan
You're doing it wrong.
Jonathan Sampson
@Johan, Johnathan's answer was a statement, not a question. He was pointing out what you *SHOULD* do not whether you were doing it..
Miky Dinescu
:) Yes, I know I'm wrongBut it seems you i right.
Johan
@Johan, always perform logic like this before you print any HTML. That way, when you get to your HTML it isn't cluttered with PHP. Notice how much cleaner my HTML is than yours this is ideal. Hope this helps.
Jonathan Sampson
Don't forget the DOCTYPE to prevent quirks mode!
Peter Boughton
@Peter, that's true, but beyond the scope of this example :)
Jonathan Sampson
A: 
< title > <?php echo $title; ?> < /title >
Ahmet Kakıcı
$title isn't declared until after the example outputs the title tags. Your answer is partly correct, but is missing some clarity on fixing the chronology of variable-declaration.
Jonathan Sampson
i thougt that he declares before using :)
Ahmet Kakıcı
No, that would be the best thing, but he unfortunately didn't do that. He doesn't declare it until the end of his example, yet he wants to use it at the beginning.
Jonathan Sampson
A: 

And to complement @Jonathan's answer, make sure you render the $title between the <title> tags..

<?php
  switch ($_GET['id'])
  {
    case "tips":
        $page = "tips.php";
        $title = "Tips"; 
   //....
   //....
 ?>

<html>....
<title><?php echo($title); ?></title>
 ....

 ....
 <?php
  include($page);

 //....
 ?>
Miky Dinescu
That will include the contents of tips.php before the <html> tag...
Jonathan Sampson
Well, that's what I get for rushing to type an answer without reading through the whole code he had. I corrected it now
Miky Dinescu
@Miky D - I didn't downvote, or else I would have cleared it. Thanks.
Jonathan Sampson
A: 

If you cannot use what Ahmet KAKICI suggests, add a script tag which sets document.title

<html>....  
    <title>My page</title>

....

<?php
    switch ($_GET['id']){  
        case "tips":  
            include("tips.php");  
            $title = "Tips";
...
?>
<script>top.document.title="<?php=$title?>"</script>

My PHP is rusty, just wanted to suggest document.title

Narayan Raman
I think that will hurt you when it comes to search-engine optimization.
Jonathan Sampson
Agree. That is why it says "If you cannot use what Ahmet KAKICI suggests"!
Narayan Raman
For the record, I didn't down-vote you.
Jonathan Sampson
@Jonathan Cool you clarified :)) I did think you did. Cheers.
Narayan Raman
+1  A: 

You can also do this with output buffering and a little string replacement.

<?php ob_start('ob_process'); ?>
<html>....  
    <title>{{{TITLE}}}</title>

....

<?php

switch ($_GET['id']){  
  case "tips":  
    include("tips.php");  
    $title = "Tips";
    break;
}

function ob_process($buffer) {
  global $title;
  $buffer = str_replace('{{{TITLE}}}', $title, $buffer);
  return $buffer;
}
ceejayoz
Global variables? Output Buffering? This is over-complicating the situation.
Jonathan Sampson
It's an option. None of the solutions are ideal - he'd be better off using a proper framework or something that handles all this.
ceejayoz
How is my solution not ideal? I agree that he should use a framework, but if he isn't capable yet, performing logic apart from markup is ideal. Or am I missing something?
Jonathan Sampson
Your situation isn't ideal because switch statements including PHP files went out when PHP-Nuke did. It works, but it's hardly pretty.
ceejayoz
Incidentally, this solution also separates logic from markup.
ceejayoz