musnoure Posted August 9, 2011 Share Posted August 9, 2011 Hi guys, In index.php, I am creating files to be included in the body section yet these pages' title should go in the header, not in the body so I thought I would work it out like this: -------------------header.php------------------- <title><?php echo $page_title; ?></title> -------------------page1.php------------------- <?php $page_title = 'Title of my first page'; require_once ("header.php"); text text text ?> -------------------index.php------------------- in the header: <?php require_once ("header.php"); ?> In the Body: <?php require_once("page1.php"); ?> THE OUTPUT: In the header: <title></title> (empty) In the Body text text text I do not know what I am missing here. If anyone could help, I would greatly appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/ Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 you're including the header.php twice, once in index, and then again in page1. remove from index. (since page1 is where you have the variable with the actual title) Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254806 Share on other sites More sharing options...
musnoure Posted August 9, 2011 Author Share Posted August 9, 2011 Thanks for the reply! ... but the title tags will be included in the body, not in the header, I want the title tags to go in the header in index.php and the content of the page that's included to go in the body. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254808 Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 anyway you look at it, you will need to define the page title BEFORE your <title> tags. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254809 Share on other sites More sharing options...
musnoure Posted August 9, 2011 Author Share Posted August 9, 2011 Yes, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254810 Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 you could have an array in your session (or somewhere else, external file, included in index, etc...) that translates page names to page titles. then on each load, you just check the page name, and grab the appropriate title. how are you linking from page to page? (since you're including page1.php in index, i assume you've got a structure like: <a href = "index.php?page=page1"> ?? Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254811 Share on other sites More sharing options...
musnoure Posted August 9, 2011 Author Share Posted August 9, 2011 I would need help if I were to use sessions or external files. Yes, but I I pull the content out of the Database. For instance, the page is named auto.php and contains the following: $found = mysql_query("SELECT * FROM auto") or die (mysql_error()); while ($row = mysql_fetch_assoc($found)) { echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' . "<br />" . substr($row['article'], 0, 250). '...' ; } And the link is: <a href="index.php?proc=auto"> Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254820 Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 and is $row['article_title'] what you want to use as the page title when the link is accessed? Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254821 Share on other sites More sharing options...
musnoure Posted August 9, 2011 Author Share Posted August 9, 2011 An exmaple of the output when I click on "auto" link: Why my Check Engine Light is on? (This is $row['article_title']) Why is my Check Engine Light on? All modern vehicles have a built-in ECM (Electronic Control Module, also Electronic Control Unit) that controls the functioning of the engine and transmission and mainly keeps the engine run as efficient as possible w... (And this is $row['article']) Both $row['article_title'] and $row['article'] are two fields in DB. auto.php basically loads content from a table named auto in the Database and displays them like this: Why my Check Engine Light is on? Why is my Check Engine Light on? All modern vehicles have a built-in ECM (Electronic Control Module, also Electronic Control Unit) that controls the functioning of the engine and transmission and mainly keeps the engine run as efficient as possible w... My car has been stolen! My car, auto, autos, has been stolen! Experiencing car theft can be very frustrating especially if you have left valuable stuff inside it. It is extremely important to act as soon as you find out that it has been stolen.The first thing to do is call ... How to change engine oil How to change car auto, autos, engine oil Changing oil and filter for your car is a job that you can do yourself instead of paying a mechanic too much for such an easy task. Learning how to check oil and change it yourself will not only save you mone... Tips on passing your driving road test Tips on passing your driving road test As many think, it is a frustrating process to prepare for the road test, it can be, but the more you practice, the easier the test gets. Here is a list of some tips that will help you prepare for the test:Start... etc. etc. So auto.php has to have a title, and when you click on the title of the article (to view full text), the entire article has to have its title as well. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254824 Share on other sites More sharing options...
WebStyles Posted August 9, 2011 Share Posted August 9, 2011 well, I got all that from your previous little bit of code, my question was simple: I just wanted to know if the article title is what you also want to use as a page title... I'll assume it is. Since you're grabbing the articles from the database and creating links to them, you could just add the title to the links, so instead of: echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' . "<br />" . substr($row['article'], 0, 250). '...' ; you would have: echo '<a href="index.php?proc='.$row['id'].'&title='.$row['article_title'].'">'.$row['article_title']. '</a>' . "<br />" . substr($row['article'], 0, 250). '...' ; and then all you need to do is: <title> <?php echo isset($_GET['title']) ? $_GET['title'] : 'YOUR DEFAULT TITLE HERE'; // for when no article is selected ?> </title> Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254832 Share on other sites More sharing options...
Jumpy09 Posted August 9, 2011 Share Posted August 9, 2011 That is one way to do it, but if you are using the ID anyway. Run the Query through first then use what you get back from the query to spit out. <?php if ( isset ( $_GET [ "proc" ] ) && !empty ( $_GET [ "proc" ] ) { // Run Query } $title = ( isset ( $row ) ) ? $row [ "article_title" ] : "Default Title" ; That should work. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1254841 Share on other sites More sharing options...
musnoure Posted August 10, 2011 Author Share Posted August 10, 2011 WebStyles, Thank you so much for your help, it did work perfect! I still have two more questions though, I would really appreciate if you could take sometime to help me out. 1. Can I apply the same thing to pull out Keywords and Description Meta Tags from DB? For example meta_keywords and meta_description? 2. Is there anyway auto.php can have a title as well? Again, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255148 Share on other sites More sharing options...
musnoure Posted August 10, 2011 Author Share Posted August 10, 2011 Also, for security measures, don't you think I should use POST instead of GET? Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255154 Share on other sites More sharing options...
trq Posted August 10, 2011 Share Posted August 10, 2011 Also, for security measures, don't you think I should use POST instead of GET? Neither is more secure than the other. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255156 Share on other sites More sharing options...
Jumpy09 Posted August 10, 2011 Share Posted August 10, 2011 Also, for security measures, don't you think I should use POST instead of GET? Personally I use them in accordance of what they look like they would do. GET information from , POST information to! You can send a lot more information when you submit a form using POST, but if you are going to retrieve information using $_GET is the least painful method. Always use MRES when dealing with post or get values though, you can never really trust what the client is sending your way. In fact that is the #1 rule to coding: Never trust the client! Well it is my #1 rule. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255160 Share on other sites More sharing options...
PFMaBiSmAd Posted August 10, 2011 Share Posted August 10, 2011 Back to your current problem. The links you make should only be what you originally had - echo '<a href="index.php?proc='.$row['id'].'">'.$row['article_title']. '</a>' . "<br />" . substr($row['article'], 0, 250). '...' ; Your code in index.php should detect that $_GET['proc'] is set and use the id value to do everything it needs to do to produce and output the necessary content that matches that id. Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255231 Share on other sites More sharing options...
musnoure Posted August 11, 2011 Author Share Posted August 11, 2011 I am still trying to figure it out. I thought I would do this: $result = mysql_query("SELECT meta_keywords FROM auto LIMIT 1"); while($row = mysql_fetch_assoc($result)) { echo $row['meta_keywords']; } It does pull out keywords and display them, but it's not dynamic. If you click on a different link, the keywords won't change. So I need to find a way to display keywords according to the id or something like that. Table structure is as follows: id article_title article article_author article_date meta_keywords meta_description word 1, word2, word 3 etc... Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255692 Share on other sites More sharing options...
musnoure Posted August 11, 2011 Author Share Posted August 11, 2011 I am still trying to figure it out. I thought I would do this: $result = mysql_query("SELECT meta_keywords FROM auto LIMIT 1"); while($row = mysql_fetch_assoc($result)) { echo $row['meta_keywords']; } It does pull out keywords and display them, but it's not dynamic. If you click on a different link, the keywords won't change. So I need to find a way to display keywords according to the id or something like that. Table structure is as follows: id article_title article article_author article_date meta_keywords meta_description word 1, word2, word 3 etc... Any help is appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255693 Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 if you're using LIMIT 1 in your query, you do not need a while loop to pull out data... The while loop basically repeats the code per each row retrieved from the database. Since you're only retrieving one row, you don't need to loop through them. you can change this: while($row = mysql_fetch_assoc($result)) { echo $row['meta_keywords']; } for this: $row = mysql_fetch_assoc($result); echo $row['meta_keywords']; also, SELECT meta_keywords FROM auto LIMIT 1 is always pulling out the same row, since you do not have any conditions in there. you'll need to add a WHERE clause: (...) where `id` = '$id' limit 1 Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255718 Share on other sites More sharing options...
musnoure Posted August 11, 2011 Author Share Posted August 11, 2011 I made those modifications, but the output is empty. <meta name="keywords" content=" " /> Here is the edited code: $result = mysql_query("SELECT meta_keywords FROM auto WHERE 'id' = '$id' LIMIT 1"); $row = mysql_fetch_assoc($result); echo $row['meta_keywords']; May I ask something, do I have to enclose id with single quotes? I actually tried it with and without but same output. I tried the following: WHERE id = id, and it did work but only for the first row. when you click on a different article, the keywords stay the same, do not change accordingly. So I have no idea what I am missing here. Thanks for the quick replies by the way! Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255722 Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 `id` = '$id' was an example of how to implement. You need to make sure the variable $id exists and has the value you want. (or use another field) Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255727 Share on other sites More sharing options...
musnoure Posted August 11, 2011 Author Share Posted August 11, 2011 I don't have that variable yet. something like this: $id = $_GET['id']; ?? Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255728 Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 that will work if the variable id exists in the url Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255729 Share on other sites More sharing options...
WebStyles Posted August 11, 2011 Share Posted August 11, 2011 ... also, you did not type the query as I showed you. I never used single quotes on the field name, I used backticks. (you can leave them out) Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255731 Share on other sites More sharing options...
musnoure Posted August 11, 2011 Author Share Posted August 11, 2011 Ok, so far I have this: $id = $_GET['id']; $result = mysql_query("SELECT meta_keywords FROM auto WHERE `id` = '$id' LIMIT 1"); $row = mysql_fetch_assoc($result); echo $row['meta_keywords']; still not working, output is empty ): Quote Link to comment https://forums.phpfreaks.com/topic/244317-dynamic-page-title-how-to-achieve/#findComment-1255733 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.