+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: PHP Include- Page Titles

  1. #1
    marvso is offline x10Hosting Member marvso is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    57

    PHP Include- Page Titles

    On my site, I have a header.php,a footer.php, and a content.php which grabs the header/footer and selects page content from a directory file (?page=pagename). The problem is that when content.php includes the directory content files, it doesn't include all of the <header> information.

    To fix this, I added this to one of the directory content files (placed above all other content):
    Code:
    <?php
    $title="PAGE TITLE";
    ?>
    And this to my header.php page:
    Code:
    <header>
    <title><?php echo $title;?></title>  
    
    </header>
    I was hoping this would print the designated title of the page, but instead I just still get no page title. Does anyone know what I did wrong/if this is possible at all?

    I'm pretty new to php, so any help is appreciated!

  2. #2
    techairlines's Avatar
    techairlines is offline x10 Flyer techairlines has much to be proud oftechairlines has much to be proud of
    Join Date
    Jul 2009
    Location
    New York City
    Posts
    2,853

    Re: PHP Include- Page Titles

    Try this:

    Code:
    <header>
    <title><?php echo "$title"; ?></title>  
    
    </header>
    Single/double quotes are important in PHP. See: http://php.net/manual/en/function.echo.php

    I can't check if this is possible without knowing the content script. The code you posted would cause the titles of all pages to display as PAGE TITLE as it is basically echoing that.
    Last edited by techairlines; 05-14-2010 at 11:08 PM.
    Best regards,
    Brian Yang - TechAirlines


    How am I doing? Click the star button at the bottom left to rate this post. Thanks.
    Terms of Service | Account Portal | Wiki

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Include- Page Titles

    Quote Originally Posted by marvso View Post
    HTML Code:
    <header>
    The tag for the header element is <head>, not <header>.

    You neglected to include a link to a live sample page so we can see the current output, in case there is another issue causing problems with the page title.

    Quote Originally Posted by techairlines View Post
    PHP Code:
    <title><?php echo "$title"?></title>
    Quotes are unnecessary around variables; all "$title" will do is interpolate the value of $title into a string with no other content. It's basically equivalent to $title by itself (except that it converts the content of $title to a string, which echo will do anyway.)
    Last edited by misson; 05-15-2010 at 12:28 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    Hello71 is offline x10Hosting Member Hello71 is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    14

    Re: PHP Include- Page Titles

    Quote Originally Posted by techairlines View Post
    Try this:

    Code:
    <header>
    <title><?php echo "$title"; ?></title>  
    
    </header>
    Single/double quotes are important in PHP. See: http://php.net/manual/en/function.echo.php

    I can't check if this is possible without knowing the content script. The code you posted would cause the titles of all pages to display as PAGE TITLE as it is basically echoing that.
    PHP Code:
    <?php echo "$title"?>
    is much slower than
    PHP Code:
    <?php echo $title?>

  5. #5
    marvso is offline x10Hosting Member marvso is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    57

    Re: PHP Include- Page Titles

    Quote Originally Posted by misson View Post
    The tag for the header element is <head>, not <header>.
    Opps, sorry. Late night typing does not work for me.

    I'm attempting a different approach, this time with switch statements:
    <?php
    $title="$title";

    switch( $title)
    {

    case($page = 'home.htm')
    $title = 'Title1';
    break;

    case($page = 'page1.htm')
    $title = 'Title2';
    break;

    }

    ?>
    However, I receive an error (expected T_VARIABLE, expecting T_CASE or T_DEFAULT or '}' ) on line 2, does anyone know the problem?

    (I'm so sorry if I'm explaining things... oddly. I will put together a live example to show soon.)

  6. #6
    brutetal is offline x10Hosting Member brutetal is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    23

    Re: PHP Include- Page Titles

    PHP Code:
    <?php
    $title
    ="$title";

    switch( 
    $title)
    {

    case(
    $page 'home.htm')
    $title 'Title1';
    break;

    case(
    $page 'page1.htm')
    $title 'Title2';
    break;

    }

    ?>
    You can't do that, its invalid syntax.

    This is correct usage:
    PHP Code:
    switch($page){
    case 
    "home.html":
    $title "Home";
    break;

    If you want to do your suggestion, then use an if structure.
    PHP Code:
    <?php
     
     
    if($page == 'home.htm'){
     
    $title 'Title1';
     }else if(
    $page == 'page1.htm'){
     
    $title 'Title2';
     }
     
     
    ?>
    Last edited by brutetal; 05-15-2010 at 10:32 PM.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Include- Page Titles

    Quote Originally Posted by Hello71 View Post
    PHP Code:
    <?php echo "$title"?>
    is much slower than
    PHP Code:
    <?php echo $title?>
    Not really. Certainly not noticeably slower. Try looping over "$title" and $title millions of times. The difference will be a few milliseconds at most.

    The double quotes are, however, unnecessary and (slightly) less readable.

    Quote Originally Posted by brutetal View Post
    PHP Code:
    case "home.html":
    $title "Home";
    ...
    if(
    $page == 'home.htm'){
     
    $title 'Title1'
    Make sure you indent to make code readable.
    PHP Code:
    switch($page) {
    case 
    'home.html':
        
    $title 'Home';
        break;
    case 
    'page1.html':
    ....
    }
    ...
    if (
    $page == 'home.html') {
        
    $title 'Title 1';
    } elseif (
    $page == 'page1.html') {
        
    $title 'Title 2';
    } else {
        ...

    Quote Originally Posted by brutetal View Post
    If you want to do your suggestion, then use an if structure.
    A switch or an associative array is preferable to a sequence of ifs. It's more readable and potentially more performant. Numerous ifs is a minor smell.

    PHP Code:
    $pages = array(
        
    'home' => array('title' => 'Home''file' => 'index.html'),
        
    'about' => array('title' => 'About Us'),
        
    'contact' => array('title' => 'Contact Us'),
       ...
        
    'invalid' => array('file' => '404.html')
    );
    function 
    getFile($pages$page) {
        if (isset(
    $pages[$page])) {
            return 
    $pages[$page];
        } else {
            return 
    $page '.html';
        }
    }

    // prevent injection attacks
    if (isset($pages$_REQUEST['page'] ])) {
        
    $page getFile($pages$_REQUEST['page']);
        
    $title $pages[$page]['title'];
         ...
    } else {
        
    // invalid page requested
        
    header('HTTP/1.0 404 Not Found');
        
    $page $pages['invalid']['file'];
    }
    include(
    'header.php');
    include(
    $page);
    include(
    'footer.php'); 
    Last edited by misson; 05-15-2010 at 11:16 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    marvso is offline x10Hosting Member marvso is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    57

    Re: PHP Include- Page Titles

    Thanks everyone for the help. I corrected the switch statement, but it still doesn't seem to work. You can see a live example here.

    This is the source code for content.php:
    Code:
    <?php
    //Snag the header:
      include('head.php');
    
    // Get the name of the page from the string query 
    // Set $page to "home" if a parameter is not passed
      if ($_GET['page']) {
        $page = $_GET['page'] . '.htm';
      } else {
        $page = 'index.htm';
      }
    
    // If file specified exists, include it in.
      if (file_exists("pages/$page")) {
        include("pages/$page");
    
    
    //If page 1 has page 2, next/previous:
      if (preg_match("/^(.*_page)(\d+)/", $page, $matches)) {
        $prev_page = $matches[2] - 1;
        $next_page = $matches[2] + 1;
        echo("<p style=\"text-align: center\">");
    
        if (file_exists("pages/" . $matches[1] . $prev_page . ".htm")) {
          echo("<a href=\"{$_SERVER['PHP_SELF']}" .
               "?page={$matches[1]}$prev_page\">Previous Page</a>");
          $prev = 1;
        }
        if (file_exists("pages/" . $matches[1] . $next_page . ".htm")) {
          if ($prev) {
            echo("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
          }
          echo("<a href=\"{$_SERVER['PHP_SELF']}" .
               "?page={$matches[1]}$next_page\">Next Page</a>");
        }
        echo("</p>");
      } 
    
    
    // If page doesn't exist, display this error message.
      } else {
        echo("<h1 align=\"center\">Page cannot be found</h1>\n");
      }
    
    //Snag the footer
      include('foot.php');
    ?>
    And here is head.php:
    Code:
    <?php
    
    //Declare what the title is based on the page
    $title="$title";
    
    switch($page){
    case "index.htm":
    $title = "Home";
    break;
    
    case "page1.htm":
    $title = "Page 1";
    break;
    
    }
    
    ?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
    
    <html>
     <head>
            <title><?php echo $title; ?></title>
        </head>
        <body>
    <a href="?page=index">Click here to go home</a> <a href="?page=page1">Click here to go to page 1</a> </br>
    Foot.php simply closes off the </body> and </html>.

    What seems to happen (at least from the browser source point of view) is that <title> won't "echo" the title variable. If I try to view the source code from my internet browser, <title></title> is blank:

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
    
    <html>
     <head>
            <title></title>
        </head>
        <body>
    <a href="?page=index">Click here to go home</a> <a href="?page=page1">Click here to go to page 1</a> </br><p>This is the index.</p></body>
    </html>
    Thanks once again for any more assistance! :D
    Last edited by marvso; 05-16-2010 at 05:02 PM.

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Include- Page Titles

    You're including head.php before you define $page, so the latter doesn't exist when the former references it.

    head.php should only output the page header; it shouldn't set any page configuration variables. The two are different functions, and, due to separation of concerns, should be handled by separate modules.

    Try the [php] tag when posting PHP code. It colorizes the code, making it more readable.

    There are a few other problems with the latest code (such as an injection attack, though not a terribly harmful one), but I don't have time to go into it right now.
    Last edited by misson; 05-16-2010 at 05:31 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    marvso is offline x10Hosting Member marvso is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    57

    Re: PHP Include- Page Titles

    Thank you so much, misson. I moved the include() head.php statement on content.php and it works like a PHP-induced dream!
    PHP Code:
    <?php

    //DECLARE WHAT PAGE IS
    ...

    //Snag the header:
      
    include('head.php');

    // If file specified exists, include it in.
      
    if (file_exists("pages/$page")) {
        include(
    "pages/$page");
    (I'll try to fix the injection attack problem... I think I see what you mean.)

    Thanks once more to everyone who helped. I've learned a lot from your assistance. :D
    Last edited by marvso; 05-16-2010 at 07:10 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. include page problem
    By Anirban1987 in forum Programming Help
    Replies: 12
    Last Post: 03-11-2009, 03:53 AM
  2. How do you get titles?
    By Teensweb in forum Off Topic
    Replies: 6
    Last Post: 02-06-2009, 04:25 AM
  3. How do you put titles below your username?
    By dickey in forum Off Topic
    Replies: 6
    Last Post: 10-29-2008, 06:22 AM
  4. Blank page/include problem?
    By ItsWesYo in forum Free Hosting
    Replies: 5
    Last Post: 08-26-2008, 10:16 PM
  5. Titles
    By Brandon in forum Feedback and Suggestions
    Replies: 5
    Last Post: 12-27-2005, 03:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers