dawnrae Posted September 26, 2006 Share Posted September 26, 2006 Hello, all,I'm pretty much a PHP newbie (if that) and have a few questions. A friend (who is no longer with us) helped me set up the basic script needed to create a very simple dynamic website, however, I am at a loss to figure out how to add in dynamic titles into what I currently am using.The script driving the site was set up to fuction like this:I have a main page called index.php, which isthe basic template of the site and includes the header, sidebar menu and footer with an open area into which the main content is called using the script.The script that drives this is: [code]<?php function GetHTTPVar($Var){ if($_GET[$Var]) { $Val = $_GET[$Var]; } if($_POST[$Var]) { $Val = $_POST[$Var]; } return $Val;}[/code]Then there is a second file (this is the one I call into the content area of index.php) that basically allows me to put in the pages to be called dynamically. My urls look something like this as a result http://www.mysite.com/index.php?p=home[code] if($Page == 'home') { include('pages/home.php'); } elseif($Page == 'aboutus') { include('pages/aboutus.php'); }[/code]and so on...Now, my question is, how do I alter this or add to this so that I can generate Dynamic Titles for my site. Right now, I get only the title that's included in the index.php file. The content pages like aboutus.php and home.php do not contain anything other than the xhtml/html content. They do not have any head, title or body tags in them, so I'm really at a loss as to what I need to do here.Anyone have any suggestions or able to point me in the right direction?Thanks in advance for any and all help,Dawn Quote Link to comment Share on other sites More sharing options...
tleisher Posted September 26, 2006 Share Posted September 26, 2006 Make an if-then-else to find out what the $page is, like you have, and then set a variable:if($page == "index"){$title = "My home";include("index.php");} else if ($page == "images") {$title = "The images";}...Make sure this is at the top of the main file, then, in your header.php file (assuming you have one) change this line:<title>Static Title</title>To<title><?php echo $title; ?></title>This will then put in whatever title you set in your if then else (above). Quote Link to comment Share on other sites More sharing options...
dawnrae Posted September 26, 2006 Author Share Posted September 26, 2006 Okay, I tried implementing this the way you indicated but it's not working. I'm wondering if I've missed something somewhere.Here's what I have:A page called index.php, which has the header, footer, menu and an open area where a file named content.php is called in.Content.php calls in the individual site pages. And there's a file called library.php that contains the following code: [code]function GetHTTPVar($Var){ if($_GET[$Var]) { $Val = $_GET[$Var]; } if($_POST[$Var]) { $Val = $_POST[$Var]; } return $Val;}[/code]I made the changes to content.php from: [code] if($Page == 'home') { include('pages/home.php'); }[/code]to this: [code] if($Page == 'home') { $title = 'Testing 123'; include('pages/home.php'); }[/code]At the top of index.php, there is an include for the library.php and the following:[code]$Page = GetHTTPVar('p');if($Page == ''){ $Page = 'home';}[/code]which I changed to this: [code]$Page = GetHTTPVar('p');if($Page == ''){ $Page = 'home'; $title = 'This is a test';}[/code]This last change works just fine, but the changes made to content.php had no effect. This file is called in the center of the page code and not before the header. Could that be the problem?DawnP.S. Thanks for your help so far. It is much appreciated. Quote Link to comment Share on other sites More sharing options...
tleisher Posted September 26, 2006 Share Posted September 26, 2006 Yes, you have to have the code for $title at the very top of all the documents, so that when you call your <title></title> you have that variable, and it's set to what you want. Quote Link to comment Share on other sites More sharing options...
dawnrae Posted September 26, 2006 Author Share Posted September 26, 2006 I have to put it into each of the content pages even though they have no header information?How should I code that exactly?DawnP.S. Sorry to be a bother, but this stuff just turns my brain into worthless spaghetti Quote Link to comment Share on other sites More sharing options...
tleisher Posted September 26, 2006 Share Posted September 26, 2006 Well tell me, how is your site setup? Do you have a global header, or a header on each file?You have to have the code you have (which you setup right, with the titles and includes) at the top of the page, and then in your header code (on the same page I assume) reflect the echo $title, command. Quote Link to comment Share on other sites More sharing options...
dawnrae Posted September 26, 2006 Author Share Posted September 26, 2006 I have one header in the index.php page, which also has the menu and footer.All the content pages are called into an area in index.php by calling in content.php in that part of the page.I have found a tenative solution so that I can make changes in one place by including this in the header: [code]elseif($Page == $Page){include('includes/title.php');}[/code]and in title.php, I have this:[code]if($Page == 'home'){ $title = 'Home Page';}[/code]This will work for the time being, but I would love to find a way to combine this in a way so that I can make the title change and assign the page in the same file. It would make things alot simpler. Any ideas on that?Dawn Quote Link to comment Share on other sites More sharing options...
dawnrae Posted September 27, 2006 Author Share Posted September 27, 2006 tleisher,Thanks so much for your help yesterday. Following your instructions and with a little improvisation, I was able to also employ descriptions and keywords in a similar manner.I would still like to figure out if there's a way for me to somehow merge the content.php and title.php info so that I can make the edits in one file. Not sure if that's possible, but if you or anyone else have any suggestions, I'll be glad to listen attentively.All the info on how my site is set up is in the previous posts but if you have any further questions or need any other information, just let me know.Thanks again to you and all the other PHP gurus out there for helping out the PHP-challenged like myself.Dawn Quote Link to comment 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.