designer76 Posted May 8, 2010 Share Posted May 8, 2010 I am trying to figure out how to get my page titles to change based off of using dynamic pages. The problem that I am having is that my dynamic pages are being called after my header, so my title and keywords are not setting or changing based on which pages are being called dynamically. Someone somewhere else suggested a method where the $_GET takes the page name and uses it as the title, but that won't work for me because I want specific information displayed for each pages title. Can anyone help me with this? I have provided example code below. Thanks in advance for any help given! EXAMPLE PAGE TO BE INCLUDED IN INDEX FILE: $title = "Title Information That I Want To Display."; <div class="test"> <div class="junk"></div> </div> INDEX FILE: header (included in the head section of the header is:) <title><?php echo $title ?></title> <?php include ('include/header.php'); ?> $a = $_GET['a']; $b = $_GET['b']; if (!$a) $a = "home"; if (!$b) $b = "a"; $path = "$b/".$a.".php"; include ($path); <?php include ('include/footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/ Share on other sites More sharing options...
teamatomic Posted May 8, 2010 Share Posted May 8, 2010 Aint gonna work that way. If its for SEO then just take the $title and put it in <h1> and use it as a page header. Lots of weight in that. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055062 Share on other sites More sharing options...
designer76 Posted May 8, 2010 Author Share Posted May 8, 2010 I understand it won't work that way, that is why I'm here asking for ways to make it work properly. I really don't want to use the H1 thing, and I'd much rather find a PHP solution. I do appreciate your response! Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055067 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2010 Share Posted May 8, 2010 Variables don't have values until the code that assigns them a value has been executed. You are going to need to reorganize your logic if you want it to work. Simplest method would be to stop looking at php as a way to copy/paste headers, footers, and content that makes up a page and look at it as a way to build and output content where you want it on a page. Put the code that determines $path and the include ($path) statement as one of the first things on the page (after any other initialization logic.) The code being included by $page should consists of some business logic (the $title variable) and presentation logic (the content or even a template.) You then simply echo the content that was built at the appropriate point on the page, but the business logic that defines what the page means is available to everything on the page because it was included first. Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055073 Share on other sites More sharing options...
litebearer Posted May 8, 2010 Share Posted May 8, 2010 Old guy musing... Perhaps: var1 = array of possible titles var2 = this page (a get or post or session var that says with which page/header we are dealing) include header.php (inside that -- <title><?PHP echo $var[$var2]; ?></title> ) Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055079 Share on other sites More sharing options...
designer76 Posted May 8, 2010 Author Share Posted May 8, 2010 Variables don't have values until the code that assigns them a value has been executed. You are going to need to reorganize your logic if you want it to work. Simplest method would be to stop looking at php as a way to copy/paste headers, footers, and content that makes up a page and look at it as a way to build and output content where you want it on a page. Put the code that determines $path and the include ($path) statement as one of the first things on the page (after any other initialization logic.) The code being included by $page should consists of some business logic (the $title variable) and presentation logic (the content or even a template.) You then simply echo the content that was built at the appropriate point on the page, but the business logic that defines what the page means is available to everything on the page because it was included first. OK, I tried doing something like this below and my title is working, but my layout is screwed up, What am I doing wrong? $a = $_GET['a']; $b = $_GET['b']; if (!$a) $a = "home"; if (!$b) $b = "a"; $path = "$b/".$a.".php"; include ($path); <?php include ('include/header.php'); ?> (included in the head section of the header is:) <title><?php echo $title ?></title> <?php echo $path; ?> <?php include ('include/footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055090 Share on other sites More sharing options...
teamatomic Posted May 8, 2010 Share Posted May 8, 2010 drop the include($path), thats your content and you are putting it before and after your header. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055105 Share on other sites More sharing options...
litebearer Posted May 8, 2010 Share Posted May 8, 2010 An example (http://nstoia.com/demo/demo_header/) index.html <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- Created on May 8, 2010 12:43:23 PM --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> origin </title> <meta name="GENERATOR" content="Arachnophilia 5.4"/> <meta name="FORMATTER" content="Arachnophilia 5.4"/> </head> <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"> This simply is where your page choice is comming from. It could be a form or <br> links (that pass the what_page via url) etc<p> For purposes of this example we will use radio buttons.<p> <form action="page1.php" method="post"> <input type="radio" name="what_page" value="Home Page"/> Home<br> <input type="radio" name="what_page" value="Movie Page"/> Movies<br> <input type="radio" name="what_page" value="Music Page"/> Music<br> <input type="radio" name="what_page" value="Contact Page"/> Contact<br> <input type="submit" value="Submit"/> </form> </body> </html> page1.php <?PHP session_start(); if(!isset($_POST['what_page'])){ $what_page = "Home Page"; }else{ $what_page = $_POST['what_page']; } include('header.php'); ?> header.php <?PHP ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- Created on May 8, 2010 12:43:23 PM --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> <?PHP echo $what_page; ?> </title> <meta name="GENERATOR" content="Arachnophilia 5.4"/> <meta name="FORMATTER" content="Arachnophilia 5.4"/> </head> <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000"> put the content for the appropriate <?PHP echo $what_page; ?> here </body> </html> <?PHP ?> make sense? Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055107 Share on other sites More sharing options...
designer76 Posted May 8, 2010 Author Share Posted May 8, 2010 drop the include($path), thats your content and you are putting it before and after your header. HTH Teamatomic The problem when I do that is that it is echoing the path and not the content. So it is literally echoing a/landing.php in the html and not the content inside landing.php. litebearer, the problem with your method is that I may end up having a ton of pages, which would mean that form/whatever is going to get pretty long. I kinda prefer the route I am going if I can get it to work properly. I do appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055111 Share on other sites More sharing options...
litebearer Posted May 8, 2010 Share Posted May 8, 2010 NP. the concept is still the same - the var does NOT have to come from a form Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055119 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2010 Share Posted May 8, 2010 Based on your code example form the first post in the thread - Some page to be included - <?php $title = "Title Information That I Want To Display."; $main_content = <<<EOT <div class="test"> <div class="junk"></div> </div> EOT; ?> index.php <?php $a = $_GET['a']; $b = $_GET['b']; if (!$a) $a = "home"; if (!$b) $b = "a"; $path = "$b/".$a.".php"; include ($path); include ('include/header.php'); echo $main_content; include ('include/footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055125 Share on other sites More sharing options...
designer76 Posted May 8, 2010 Author Share Posted May 8, 2010 Awesome PFMaBiSmAd, That worked like a charm. Thanks for your assistance! Quote Link to comment https://forums.phpfreaks.com/topic/201103-how-to-get-dynamic-page-title-to-work-with-dynamic-pages/#findComment-1055134 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.