Jump to content

Dynamic page title by get id


lkbolt

Recommended Posts

I have written the following php statement and it works to show pages titles if I have an id in the url example.  www.mysite.com/index.php?id=3 then the right page title is displayed but if I am on my home page www.mysite.com with no id the page title isn't displayed. I am running the code in the header in the title brackets.  here is the code. The last else doesn't display my home page title any help would be great thank you very much...

 

<title>

<?

if (isset($_GET['id']))

 

  if($_GET['id'] == '1')

  {

    echo "LBT Services Page";

else if($_GET['id'] == '2')

{

  echo "About LBT";

  }

else if($_GET['id'] == '3')

  {

  echo "Contact LBT";

  }

else if($_GET['id'] == '4')

  {

  echo "Your Message to LBT has been Submitted";

  }

else if($_GET['id'] == '5')

  {

  echo "LBT Links Page";

  }

else if($_GET['id'] == '6')

  {

  echo "LBT Back-UP Plans";

  }

else

{

  echo "LBT Home page";

  }

?></title>

Link to comment
Share on other sites

<title>

<?
if (isset($_GET['id'])) {

      if($_GET['id'] == '1')
      {
        echo "LBT Services Page";
   }     
   else if($_GET['id'] == '2')
   {
          echo "About LBT";
     }
   else if($_GET['id'] == '3')
     {
          echo "Contact LBT";
     }
   else if($_GET['id'] == '4')
     {
          echo "Your Message to LBT has been Submitted";
     }
   else if($_GET['id'] == '5')
     {
          echo "LBT Links Page";
     }
   else if($_GET['id'] == '6')
     {
          echo "LBT Back-UP Plans";
     }
   else
      {
          echo "LBT Home page";
     }
}//first if
else {
echo 'LBT Home Page';
}
?>

</title>

Link to comment
Share on other sites

How about a way with no "if" statements:

<?php
$title_array = array('LBT Home page','LBT Services Page','About LBT','Contact LBT','Your Message to LBT has been Submitted','LBT Links Page','LBT Back-UP Plans');
$id = (isset($_GET['id']))?$_GET['id']:0;
$title = (array_key_exists($id,$title_array))?$title_array[$id]:$title_array[0];
echo "<title>$title</title>\n";
?>

 

Using this code makes it very easy to add anothere title without having to worry whether you've added the "if" block correctly.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.