aweb Posted November 2, 2009 Share Posted November 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/ Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949079 Share on other sites More sharing options...
aweb Posted November 2, 2009 Author Share Posted November 2, 2009 How does this help me to display the page titles dynamically though? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949086 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 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>"; } }?> Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949093 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 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 \" Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949110 Share on other sites More sharing options...
aweb Posted November 2, 2009 Author Share Posted November 2, 2009 Great this worked perfectly. How would I add some other plain text to the title, such as the site name? Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949117 Share on other sites More sharing options...
MadTechie Posted November 2, 2009 Share Posted November 2, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/179908-dynamic-page-title-help/#findComment-949444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.