How to dynamically change page title using php and require_once?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • the wonderer

    How to dynamically change page title using php and require_once?

    This is an elementary question, but I've not been able to find the
    answer, so here goes:

    I am developing a site using php. I have the html header information
    in a file that I include in all the pages using the require_once
    function.

    That is, each page includes the line

    <?require_once( 'PageStart.php' )?>

    The PageStart.php file essentially defines the header. Omitting the
    boring stuff, it's laid out like so:

    <html>
    <head>
    <title>The Title of the Page</title>
    <?require_once( 'styles/PageStyles.css' )?>
    </head>

    <boring stuff snipped>

    This works great. The only complaint I have is that there are LOTS of
    pages on the site, and using this approach means that every page is
    titled "The Title of the Page" When scanning my browser history, I
    could have surfed to 30 different pages in the site and they all have
    the same title. Makes it hard to know which page I'd like to go back
    to.

    I'd like to have a way to pass a (sometimes) dynamically generated
    page title to the PageStart.php file. I say sometimes because certain
    pages (the Main one for example) will always have the same title, so
    that could be passed statically. There is another series of pages in
    which the same php file accesses a MySQL database to display
    information about various family members, and I'd like that title to
    be generated dynamically (for example, "All about Mary" or "All about
    Ted" depending on the person being described in the page).

    Now I KNOW that with php there's a way to do this. I just can't find
    it.
  • Geoff Berrow

    #2
    Re: How to dynamically change page title using php and require_once?

    I noticed that Message-ID: <2o50pvs74mssks dg8c5hceg5pp10h q84e5@4ax.com>
    from the wonderer contained the following:
    [color=blue]
    >
    > <?require_once( 'PageStart.php' )?>
    >
    >The PageStart.php file essentially defines the header. Omitting the
    >boring stuff, it's laid out like so:
    >
    > <html>
    > <head>
    > <title>The Title of the Page</title>
    > <?require_once( 'styles/PageStyles.css' )?>
    > </head>
    >
    > <boring stuff snipped>[/color]

    Make it a function. Make it so that if you don't pass a value you get
    the default page title and if you do then it uses that title. The value
    can be generated from a query for your All about Mary pages.

    <?php
    function header($title){
    if(!$title){
    $title="default page title";
    }
    ?>
    <html>
    <head>
    <title><?php print $title; ?></title>
    <?require_once( 'styles/PageStyles.css' )?>
    </head>

    <?php } ?>

    Then in your page

    <?require_once( 'PageStart.php' );
    $title="My new Page";
    header($title);
    ?>

    or for the default

    <?require_once( 'PageStart.php' );
    header($title);
    ?>




    --
    Geoff Berrow
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • the wonderer

      #3
      Re: How to dynamically change page title using php and require_once?

      On Fri, 17 Oct 2003 18:11:31 +0100, Geoff Berrow
      <bl@ckdog.co.uk .the.cat> wrote:
      [color=blue]
      >I noticed that Message-ID: <2o50pvs74mssks dg8c5hceg5pp10h q84e5@4ax.com>
      >from the wonderer contained the following:
      >[color=green]
      >>
      >> <?require_once( 'PageStart.php' )?>
      >>
      >>The PageStart.php file essentially defines the header. Omitting the
      >>boring stuff, it's laid out like so:
      >>
      >> <html>
      >> <head>
      >> <title>The Title of the Page</title>
      >> <?require_once( 'styles/PageStyles.css' )?>
      >> </head>
      >>
      >> <boring stuff snipped>[/color]
      >
      >Make it a function. Make it so that if you don't pass a value you get
      >the default page title and if you do then it uses that title. The value
      >can be generated from a query for your All about Mary pages.
      >
      ><?php
      >function header($title){
      > if(!$title){
      > $title="default page title";
      > }
      >?>
      ><html>
      > <head>
      > <title><?php print $title; ?></title>
      > <?require_once( 'styles/PageStyles.css' )?>
      > </head>
      >
      ><?php } ?>
      >
      >Then in your page
      >
      ><?require_once ('PageStart.php ');
      >$title="My new Page";
      >header($title) ;
      >?>
      >
      >or for the default
      >
      ><?require_once ('PageStart.php ');
      >header($title) ;
      >?>[/color]
      Thanks Geoff. I'm not sure though if I understand this.

      I currently have the header information, including title, in
      PageStart.php.

      Are you saying to incorporate the header function into the
      PageStart.php code, or make the function a separate file, or put the
      function into any php file that would need it? Should I remove the
      title declaration from PageStart.php altogether?

      I'm not sure how this would work:

      <?require_once( 'PageStart.php' );
      $title="My new Page";
      header($title);
      ?>

      or for the default

      <?require_once( 'PageStart.php' );
      header($title);

      Since PageStart.php already has <title> in it, wouldn't the subsequent
      header($title) function either be ignored or cause a problem?

      Comment

      • Geoff Berrow

        #4
        Re: How to dynamically change page title using php and require_once?

        I noticed that Message-ID: <c5b0pvsj4f8pm7 imhgl49p8c2vpqe khi6n@4ax.com>
        from the wonderer contained the following:
        [color=blue]
        >Thanks Geoff. I'm not sure though if I understand this.
        >
        >I currently have the header information, including title, in
        >PageStart.ph p.
        >
        >Are you saying to incorporate the header function into the
        >PageStart.ph p code, or make the function a separate file, or put the
        >function into any php file that would need it? Should I remove the
        >title declaration from PageStart.php altogether?
        >
        >I'm not sure how this would work:[/color]

        First of all let me apologise. The function cannot be called header()
        because that already exists in PHP (duh!) So call it head()[color=blue]
        >
        > <?require_once( 'PageStart.php' );
        > $title="My new Page";
        > header($title);
        > ?>
        >
        > or for the default
        >
        > <?require_once( 'PageStart.php' );
        > header($title);
        >
        >Since PageStart.php already has <title> in it, wouldn't the subsequent
        >header($titl e) function either be ignored or cause a problem?[/color]

        The variable $title is what is passed to the function. If it isn't set
        then the default title is used. Here is my (corrected) code:

        PageStart.php (it's actually the full page here, but could just be the
        header naturally)

        <?php
        function head($title){
        if(!$title){
        $title="Default page title";
        }
        ?>
        <html>
        <head>
        <title><?php print $title; ?></title>

        </head>
        <body>
        hello world
        </body>
        </html>

        <?php } ?>

        And pages that use it
        p1.php

        <?require_once( 'PageStart.php' );
        head("My new Page");
        ?>

        p2.php

        <?require_once( 'PageStart.php' );
        head($title);
        ?>

        p3.php
        <?require_once( 'PageStart.php' );
        $title="somethi ng";
        head($title);
        ?>

        It all works. I even tried

        p4.php

        <?require_once( 'PageStart.php' );
        $title="somethi ng";
        head(5+7/3);
        ?>

        :-)

        See:



        --
        Geoff Berrow
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • the wonderer

          #5
          Re: How to dynamically change page title using php and require_once?

          On Fri, 17 Oct 2003 19:14:01 +0100, Geoff Berrow
          <bl@ckdog.co.uk .the.cat> wrote:
          [color=blue]
          >I noticed that Message-ID: <c5b0pvsj4f8pm7 imhgl49p8c2vpqe khi6n@4ax.com>
          >from the wonderer contained the following:
          >[color=green]
          >>Thanks Geoff. I'm not sure though if I understand this.
          >>
          >>I currently have the header information, including title, in
          >>PageStart.php .
          >>
          >>Are you saying to incorporate the header function into the
          >>PageStart.p hp code, or make the function a separate file, or put the
          >>function into any php file that would need it? Should I remove the
          >>title declaration from PageStart.php altogether?
          >>
          >>I'm not sure how this would work:[/color]
          >
          >First of all let me apologise. The function cannot be called header()
          >because that already exists in PHP (duh!) So call it head()[color=green]
          >>
          >> <?require_once( 'PageStart.php' );
          >> $title="My new Page";
          >> header($title);
          >> ?>
          >>
          >> or for the default
          >>
          >> <?require_once( 'PageStart.php' );
          >> header($title);
          >>
          >>Since PageStart.php already has <title> in it, wouldn't the subsequent
          >>header($title ) function either be ignored or cause a problem?[/color]
          >
          >The variable $title is what is passed to the function. If it isn't set
          >then the default title is used. Here is my (corrected) code:
          >
          >PageStart.ph p (it's actually the full page here, but could just be the
          >header naturally)
          >
          ><?php
          >function head($title){
          > if(!$title){
          > $title="Default page title";
          > }
          >?>
          ><html>
          > <head>
          > <title><?php print $title; ?></title>
          >
          > </head>
          > <body>
          > hello world
          > </body>
          > </html>
          >
          ><?php } ?>
          >
          >And pages that use it
          >p1.php
          >
          ><?require_once ('PageStart.php ');
          >head("My new Page");
          >?>
          >
          >p2.php
          >
          ><?require_once ('PageStart.php ');
          >head($title) ;
          >?>
          >
          >p3.php
          ><?require_once ('PageStart.php ');
          >$title="someth ing";
          >head($title) ;
          >?>
          >
          >It all works. I even tried
          >
          >p4.php
          >
          ><?require_once ('PageStart.php ');
          >$title="someth ing";
          >head(5+7/3);
          >?>
          >
          >:-)
          >
          >See:
          >www.ckdog.co.uk/php/test[/color]

          Okay, I reread your post and I went to the /php/test url above. So I
          see that it works. But in trying to implement it, I'm failing. Maybe
          if I could see the source of your pX.php and PageStart.php files?

          Here is the start of my PageStart.php file (note that I changed the
          name of your function head() to make_title() just so as not to confuse
          the function head() with the <head> tag):

          <?php
          function make_title($tit le)
          {
          if(!$title)
          {
          $title="Page Default Title";
          }
          ?>

          <?php } ?>

          <html>
          <head>
          <title><?php print $title; ?></title>
          <?require_once( 'styles/BlueAndGold.css ')?>
          </head>

          Now here is the first line of my index.php page:

          <?require_once( 'PageStart1017. php', $title='My Web Site: Main
          Page')?>

          That line triggers a parse error. Since PageStart.php is included in
          virtually every page on the web site, I don't know how to "let it
          know" what page title is being used.

          I appreciate your help on this. I am a php novice (obviously!) and
          much of what I do consists of copying stuff I understand, then
          gradually modifying it to suit my needs, with frequent visits to
          php.net to check what I'm doing.

          Comment

          • Geoff Berrow

            #6
            Re: How to dynamically change page title using php and require_once?

            I noticed that Message-ID: <59i0pvc35mr0kl jvm9q51gp82frqk t0gc6@4ax.com>
            from the wonderer contained the following:
            [color=blue]
            >Okay, I reread your post and I went to the /php/test url above. So I
            >see that it works. But in trying to implement it, I'm failing. Maybe
            >if I could see the source of your pX.php and PageStart.php files?[/color]

            It's all there. I quoted the entire source.[color=blue]
            >
            >Here is the start of my PageStart.php file (note that I changed the
            >name of your function head() to make_title() just so as not to confuse
            >the function head() with the <head> tag):
            >
            > <?php
            > function make_title($tit le)
            > {
            > if(!$title)
            > {
            > $title="Page Default Title";
            > }
            > ?>
            >
            > <?php } ?>
            >
            > <html>
            > <head>
            > <title><?php print $title; ?></title>
            > <?require_once( 'styles/BlueAndGold.css ')?>
            > </head>
            >
            >Now here is the first line of my index.php page:
            >
            > <?require_once( 'PageStart1017. php', $title='My Web Site: Main
            >Page')?>
            >
            >That line triggers a parse error. Since PageStart.php is included in
            >virtually every page on the web site, I don't know how to "let it
            >know" what page title is being used.[/color]

            This is the bit where you 'call' the function. Basically what you are
            doing is saying "run the bit of code called 'make_title' and pass the
            parameter $title to it".

            So on each page you have:

            <?require_once( 'PageStart1017. php');
            $title="My Web Site: MainPage";
            make_title($tit le);
            ?>
            Changing the $title variable to suit, of course.

            For the default use:

            $title="";

            If $title doesn't contain anything, the if() makes the title the page
            default title.

            Obviously you can then get $title from anywhere, a database query, if
            you are that advanced.[color=blue]
            >
            >I appreciate your help on this. I am a php novice (obviously!) and
            >much of what I do consists of copying stuff I understand, then
            >gradually modifying it to suit my needs, with frequent visits to
            >php.net to check what I'm doing.[/color]

            I'm still very much a novice myself and I'm glad to help. I get a lot
            of help from other people - it's only fair to give it if you can, plus
            it's a great way to learn.
            --
            Geoff Berrow
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • the wonderer

              #7
              Re: How to dynamically change page title using php and require_once?

              Oops Geoff... I never replied again the other day. I was overthinking
              things. Your simple suggestions worked perfectly. Thanks again.


              On Fri, 17 Oct 2003 21:57:51 +0100, Geoff Berrow
              <bl@ckdog.co.uk .the.cat> wrote:
              [color=blue]
              >I noticed that Message-ID: <59i0pvc35mr0kl jvm9q51gp82frqk t0gc6@4ax.com>
              >from the wonderer contained the following:
              >[color=green]
              >>Okay, I reread your post and I went to the /php/test url above. So I
              >>see that it works. But in trying to implement it, I'm failing. Maybe
              >>if I could see the source of your pX.php and PageStart.php files?[/color]
              >
              >It's all there. I quoted the entire source.[color=green]
              >>
              >>Here is the start of my PageStart.php file (note that I changed the
              >>name of your function head() to make_title() just so as not to confuse
              >>the function head() with the <head> tag):
              >>
              >> <?php
              >> function make_title($tit le)
              >> {
              >> if(!$title)
              >> {
              >> $title="Page Default Title";
              >> }
              >> ?>
              >>
              >> <?php } ?>
              >>
              >> <html>
              >> <head>
              >> <title><?php print $title; ?></title>
              >> <?require_once( 'styles/BlueAndGold.css ')?>
              >> </head>
              >>
              >>Now here is the first line of my index.php page:
              >>
              >> <?require_once( 'PageStart1017. php', $title='My Web Site: Main
              >>Page')?>
              >>
              >>That line triggers a parse error. Since PageStart.php is included in
              >>virtually every page on the web site, I don't know how to "let it
              >>know" what page title is being used.[/color]
              >
              >This is the bit where you 'call' the function. Basically what you are
              >doing is saying "run the bit of code called 'make_title' and pass the
              >parameter $title to it".
              >
              >So on each page you have:
              >
              ><?require_once ('PageStart1017 .php');
              > $title="My Web Site: MainPage";
              > make_title($tit le);
              >?>
              >Changing the $title variable to suit, of course.
              >
              >For the default use:
              >
              >$title="";
              >
              >If $title doesn't contain anything, the if() makes the title the page
              >default title.
              >
              >Obviously you can then get $title from anywhere, a database query, if
              >you are that advanced.[color=green]
              >>
              >>I appreciate your help on this. I am a php novice (obviously!) and
              >>much of what I do consists of copying stuff I understand, then
              >>gradually modifying it to suit my needs, with frequent visits to
              >>php.net to check what I'm doing.[/color]
              >
              >I'm still very much a novice myself and I'm glad to help. I get a lot
              >of help from other people - it's only fair to give it if you can, plus
              >it's a great way to learn.[/color]

              Comment

              Working...