Jump to content

Dynamic Page Title Help


aweb

Recommended Posts

I am having trouble figuring out the best way to implement dynamic page titles within my site. Currently, my header file looks like this:

<title><?php if(isset($title)) { print $title; } else { print "other title goes here"; } ?></title>

 

And within the site pages I have defined

$title "unique page title here"

 

However I have a categories.php that the 30 or so categories are pulled from, so I need a new way of generating the titles since the pages are all generated from this one file.

 

At the top of the categories.php the database connection file is included along with the header and to display the given category, the id is defined:

 

$id=$_REQUEST['id'];

 

And if this helps, the category lists are generated later with the following:

 

<?
				$servcat=mysql_query("select * from We_ServiceCategory_tbl ");
				if(mysql_num_rows($servcat)>0)
				{
					while($rscat=mysql_fetch_assoc($servcat))
					{
				?>
					<a href="categories.php?id=<? echo $rscat['Category_Id'];?>" ><? echo htmlentities($rscat['Category_text']);?></a>

 

Any ideas on what would be the easiest way for me to get this working?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/
Share on other sites

Build the list at the very start and store it in a variable like this, (also you store the title)

<?php
				$servcat=mysql_query("select * from We_ServiceCategory_tbl ");
				if(mysql_num_rows($servcat)>0)
				{
					$CatList = "";
					while($rscat=mysql_fetch_assoc($servcat))
					{
						if($rscat['Category_Id'] == $_REQUEST['id']) $title = htmlentities($rscat['Category_text']);
						$CatList .= "<a href=\"categories.php?id=".$rscat['Category_Id']."\">".htmlentities($rscat['Category_text'])."</a>";
					}?>

 

later on the page you can insert the list like this

//HTML - Insert List

<?php echo $CatList; ?>

Because you get the title at the start, thus the variable is set

note the line

if($rscat['Category_Id'] == $_REQUEST['id']) $title = htmlentities($rscat['Category_text']);

 

 

EDIT: i missed a closing } in the above code

 

it should be

<?php
               $servcat=mysql_query("select * from We_ServiceCategory_tbl ");
               if(mysql_num_rows($servcat)>0)
               {
                  $CatList = "";
                  while($rscat=mysql_fetch_assoc($servcat))
                  {
                     if($rscat['Category_Id'] == $_REQUEST['id']) $title = htmlentities($rscat['Category_text']);
                     $CatList .= "<a href=\"categories.php?id=".$rscat['Category_Id']."\">".htmlentities($rscat['Category_text'])."</a>";
                  }
               }?>

 

aweb, questions should be asked here..

 

here's the answer

 

update

$CatList .= "<a href=\"categories.php?id=".$rscat['Category_Id']."\">".htmlentities($rscat['Category_text'])."</a>";

to

$CatList .= "<a href=\"categories.php?id=".$rscat['Category_Id']."\">".htmlentities($rscat['Category_text'])."</a>";
$CatList .= "<hr size=\"1\" class=\"intleftboxhr\" />";

 

please note that i escaped the quotes

" becomes \"

Update this line

if($rscat['Category_Id'] == $_REQUEST['id']) $title = htmlentities($rscat['Category_text']);

to

if($rscat['Category_Id'] == $_REQUEST['id']) $title = "Some other text - ".htmlentities($rscat['Category_text']);

 

of couse this doesn't affect your default title

<title><?php if(isset($title)) { print $title; } else { print "other title goes here"; } ?></title>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.