AdamTwyx Posted October 6, 2015 Share Posted October 6, 2015 Hello All, Can someone explain something to me (Im a Noob) I am currently trying to set a page title to be based on the result of a query on that page but not having much joy and I am too inexperienced to know where I am going wrong. let me try and explain where I am at with this starting with the code for the page in question... <?php $pageTitle = " | PoolHQ"; include("../includes/header.php"); ?> <div class="container"> <?php $req = mysql_query('select * FROM Venues WHERE id="' . $_GET["id"] . '" '); while($dnn = mysql_fetch_array($req)) { ?> <div class="row" id="features"> <div class="col-sm-12 feature" style="background-color:transparent"> <br /> All of the DB connection details are in the header.php file along with this <title><?php echo $pageTitle; ?></title> therefore I usually set the page title to whatever I want but in the case above, I would like the page title to be the 'Name' which is a field from the 'Venues' Table. The query is on that page now as the rest of the page (which I did not post details for) uses the information in the page. If I move the query to the top of the page, I get basically a blank page apart from the footer.php. Can someone give me an idea as to why its not owrking and what i should do...? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/ Share on other sites More sharing options...
ginerjm Posted October 6, 2015 Share Posted October 6, 2015 A good way to solve this problem and many other future ones is to design your script to do ALL of your php work at the top and ALL of your html work at the bottom. That way everything that you want to appear on the page is ready when the html part is sent to the client. It also makes it easier to read and interpret and perform maintenance on at a later date. In this case - simply set your $pagetitle var once you grab the query result record. PS - why do a loop instead of just a simple fetch since your query appears to only be seeking a single record? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522473 Share on other sites More sharing options...
AdamTwyx Posted October 6, 2015 Author Share Posted October 6, 2015 Like I said, I am a noob and slowly mucking my way through this project I have based on a lot of Self Taught Code... Given what you said, Would the code be like this? <?php $req = mysql_query('select * FROM Venues WHERE id="' . $_GET["id"] . '" '); while($dnn = mysql_fetch_array($req)) { ?> <?php $pageTitle = " | PoolHQ"; include("../includes/header.php"); ?> <div class="container"> etc etc If thats the case, how can I call the 'Name' into the Page title? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522475 Share on other sites More sharing options...
QuickOldCar Posted October 6, 2015 Share Posted October 6, 2015 You don't need to do a while loop to return one result set. Look into mysqli_* functions or PDO as mysql_* functions are deprecated and will be removed in php7 Always escape your data before using them in a query mysql_real_escape_string Use the mysqli related functions if to use that and/or use prepared statements check proper data types I do not know your database structure so giving an example. <?php $req = mysql_query('select * FROM Venues WHERE id="' . mysql_real_escape_string($_GET["id"]) . '" '); $dnn = mysql_fetch_assoc($req); $pageTitle = $dnn['title']; include("../includes/header.php"); ?> You include your header file, edit that file and add $pageTitle to the title tag When is a single page using $_GET["id"] can check if $pageTitle is set, otherwise use a default title //inside header.php if(isset($pageTitle)){ $title = $pageTitle; } else { $title = "My default page title"; } echo "<title>$title</title>"; Really should change to at least mysqli, is example code there. http://php.net/manual/en/mysqli-result.fetch-assoc.php Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522479 Share on other sites More sharing options...
maxxd Posted October 6, 2015 Share Posted October 6, 2015 The problem with that is that you said all the database connection details are in header.php, so you're not going to be able to run the query before you include the header, which is where you define the database connection. You're going to need to do some refactoring. What does the file header.php look like? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522480 Share on other sites More sharing options...
QuickOldCar Posted October 6, 2015 Share Posted October 6, 2015 (edited) Good point maxxd, ginerjm summed it up with all the processing first then display html after Sometimes can't even tell in these posts if getting it complete or a snippet. Place your database connection with credentials in a file like dbcon.php and include that first <?php include("../includes/dbcon.php"); $req = mysql_query('select * FROM Venues WHERE id="' . mysql_real_escape_string($_GET["id"]) . '" '); $dnn = mysql_fetch_assoc($req); $pageTitle = $dnn['title']; include("../includes/header.php"); ?> Edited October 6, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522482 Share on other sites More sharing options...
AdamTwyx Posted October 6, 2015 Author Share Posted October 6, 2015 Ah I see what you guys mean now. When I was first starting out, I had the aid of a programmer who said that he always put the Connection details into a header.php as that will always be included, thus cutting down on the amount of code having to be written on each page. I wont lie but I have used Google (other search engines are available ) and I will admit that most of the time in the examples shown, the connection details are entered separately as opposed to in a header file. Fortunately, the project I am working on is still in its early days so refactoring will not be an issue at all. Out of Interest, Is there a 'Best Practice' you stick to when it comes to DB Connections etc? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522487 Share on other sites More sharing options...
maxxd Posted October 6, 2015 Share Posted October 6, 2015 If the project is still early in it's development, I'd highly recommend switching to PDO (my choice) or MySQLi, because (as already pointed out), the mysql_* functions are deprecated and removed in version 7, coming out sometime in the near-ish future. You'll find plenty of documentation on both classes in the manual, including come ideas about best practices. Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522488 Share on other sites More sharing options...
ginerjm Posted October 6, 2015 Share Posted October 6, 2015 I put my connection logic into a module by itself as a function. When I'm writing a script that requires db access I make sure to include that file at the top of my script and call the function (in it) when I need to open a connection. I use the dbname as an argument in the call to that function. The function returns the connection handle which I can then use in my script. My connection code: // Filename: sql_connection.php function PDOConnect($dbname) { // my connection code follows ... } And when I need it in my script I do this: <?php .. .. .. require($path_to_php."/sql_connection.php"); $pdo = PDOConnect('my_db_1'); $q = "select......."; $qresults = $pdo->query($q); Of course you only have to make the connect call once in your script, unless you deliberately close it or need another handle to the driver. Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522490 Share on other sites More sharing options...
benanamen Posted October 6, 2015 Share Posted October 6, 2015 @ginerjm, I was wondering why you are putting your connection in a function and passing the DB name. Do you use different DB's in the same application? That would be the only reason I know of to be passing the database name every time you connect. It is very rare that multiple DB's are used in the same app. Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522492 Share on other sites More sharing options...
AdamTwyx Posted October 7, 2015 Author Share Posted October 7, 2015 Thats great guys, Appreciate the help / Pointers / Guidance. Its good to hear peoples opinions on how or why things should be done a certain way. Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522548 Share on other sites More sharing options...
ginerjm Posted October 7, 2015 Share Posted October 7, 2015 Benanamen, The db parm is there because the same included file is used in all my scripts. Obviously I don't write all my scripts to use the same dbname, so that is why. Make sense now? Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522575 Share on other sites More sharing options...
benanamen Posted October 7, 2015 Share Posted October 7, 2015 (edited) Actually it doesn't. If the db connection file is included in all your scripts then so will the connection without having to call a function, let alone pass a database name. You set the database name in the connection string for that particular app one time and never touch it again for the rest of your life. The only thing you might ever have to change is the database username and password if you move it somewhere else. Edited October 7, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522581 Share on other sites More sharing options...
benanamen Posted October 7, 2015 Share Posted October 7, 2015 (edited) After reading your response 10 times I think I understand your response. If I understand correctly, you are using the exact single DB connect file for multiple apps, not duplicates of that file for every app. If that is the case, you will run into problems when you have different groups of Mysql DB users having different logins (Not app users). Really, each app should be self contained and have its own copy of a DB config file. While it is fine for your own personal use to do it that way, it really shouldn't be used as an example to beginners. To be honest, it's a bit lazy. Edited October 7, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522587 Share on other sites More sharing options...
QuickOldCar Posted October 7, 2015 Share Posted October 7, 2015 Let's try to stick to the original topic. Quote Link to comment https://forums.phpfreaks.com/topic/298458-variable-page-title-help/#findComment-1522600 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.