cdmafra Posted September 14, 2013 Share Posted September 14, 2013 Hello! I need to create a dynamic page title and description to my website. However, the content is all generated from "index.php" page (example: the URL of post is [...].net/?id=2 ). So, title and description are the same in all pages, the website only has a "real" php file. So, how can I change the title with this situation? I have a mysql table for news and another for news/posts. Content generator PHP lines: <!-- Header --> <?php if (isset($_REQUEST["id"])) { $query = "SELECT * FROM news WHERE news_id=".$_REQUEST["id"].""; $result = mysql_query($query); $row = mysql_fetch_array($result); ?> <div class="twelve columns"> <div class="row"><h3 class="n3"><?php echo $row["news_title"]; ?></h3> </div> </div> <div class="row"><!-- Row interior--> <div class="twelve columns"> <div class="panel"> <p id="data"><?php echo $row["news_date"]; ?>, <?php echo$row["hour"] ;?> GMT</p> <p><?php echo $row["news_post"]; ?></p> Quote Link to comment Share on other sites More sharing options...
Yohanne Posted September 14, 2013 Share Posted September 14, 2013 Just create a class for your objective Quote Link to comment Share on other sites More sharing options...
cdmafra Posted September 14, 2013 Author Share Posted September 14, 2013 Just create a class for your objective I'm not sure if I understood.. What you mean with that? Quote Link to comment Share on other sites More sharing options...
fastsol Posted September 14, 2013 Share Posted September 14, 2013 Well you didn't provide us with how you are doing your title tags now, so this is a bit of a guess on that for your situation. <!-- Header --> <?php if (isset($_REQUEST["id"])) { $query = "SELECT * FROM news WHERE news_id=".$_REQUEST["id"].""; $result = mysql_query($query); $row = mysql_fetch_array($result); ?> <!DOCTYPE HTML> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title><?php echo htmlentities($row['news_title']); ?></title> <meta name="description" content="<?php echo htmlentities($row['news_description']); ?>"/> // You need to chang the news_description to something that your db actually holds to describe the news article. </head> </body> <div class="twelve columns"> <div class="row"><h3 class="n3"><?php echo $row["news_title"]; ?></h3> </div> </div> <div class="row"><!-- Row interior--> <div class="twelve columns"> <div class="panel"> <p id="data"><?php echo $row["news_date"]; ?>, <?php echo$row["hour"] ;?> GMT</p> <p><?php echo $row["news_post"]; ?></p> </body> Quote Link to comment Share on other sites More sharing options...
DFulg Posted September 14, 2013 Share Posted September 14, 2013 Note: Never insert data that can be tampered with directly into a mysql query. Always perform the proper sanitation on that data before inserting it. Quote Link to comment Share on other sites More sharing options...
cdmafra Posted October 5, 2013 Author Share Posted October 5, 2013 Don't worked that code , fastsol :/ I have the title and description generated only one time to entire website (articles, pages, etc.) in the Index.php, this way: <title>Motor Racing News | Informações de Altas Rotações</title> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 5, 2013 Share Posted October 5, 2013 So you're wanting to append the title of the news article to your existing title in the <title></title> tags? Quote Link to comment Share on other sites More sharing options...
cdmafra Posted October 5, 2013 Author Share Posted October 5, 2013 So you're wanting to append the title of the news article to your existing title in the <title></title> tags? Yes Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 5, 2013 Share Posted October 5, 2013 To do that you'll have to get the news article title before you output the <title></title> tags. Quote Link to comment Share on other sites More sharing options...
cdmafra Posted October 5, 2013 Author Share Posted October 5, 2013 (edited) To do that you'll have to get the news article title before you output the <title></title> tags. Just like in fastsol example? If yes, this is not working for me ... sorry all mess, but I am a beginner in PHP/MySQL.. My base code: <title>Motor Racing News | Informações de Altas Rotações</title> (...) <?php if (isset($_REQUEST["guid"])) { $guid = mysql_real_escape_string($_REQUEST["guid"]); // sanitize the guid $query = "SELECT * FROM news WHERE news_guid='".$guid."'"; $result = mysql_query($query); $row = mysql_fetch_array($result); ?> <div class="twelve columns"> <div class="row"><h3 class="n3"><?php echo $row["news_title"]; ?></h3> </div> </div> <div class="row"><!-- Row interior--> <div class="twelve columns"> <div class="panel"> <p id="data"><?php echo $row["news_date"]; ?>, <?php echo$row["hour"] ;?> GMT</p> <p><?php echo $row["news_post"]; ?></p > Edited October 5, 2013 by cdmafra Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 6, 2013 Share Posted October 6, 2013 Just like in fastsol example? If yes, this is not working for me ... sorry all mess, but I am a beginner in PHP/MySQL.. As Ch0cu3r suggested, the MySQL query needs to be executed prior to displaying the <title> tag. In the code shown above, the query appears to be after the page title. Quote Link to comment Share on other sites More sharing options...
Irate Posted October 6, 2013 Share Posted October 6, 2013 You could also use a server-side push once something changed on the client side and/or server-side(Ajax and Comet are the keywords, here) and change the title with JavaScript. Quote Link to comment Share on other sites More sharing options...
cdmafra Posted October 6, 2013 Author Share Posted October 6, 2013 You could also use a server-side push once something changed on the client side and/or server-side(Ajax and Comet are the keywords, here) and change the title with JavaScript. Thank you... but it works? I am not inside Ajax and Comet.... Quote Link to comment Share on other sites More sharing options...
Irate Posted October 6, 2013 Share Posted October 6, 2013 I haven't worked with Comet either, but it's basically just establishing and maintaining a connection while waiting for the server to push responses to you, rather than pulling the data from the server with Ajax (though both work). JavaScript works on all browsers, so this should work, and you're not sending any confident data, so it's fine. JavaScript function for that... function setTitle(title){ document.title = title; return null; } 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.