Jump to content

Recommended Posts

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...?

 

Link to comment
https://forums.phpfreaks.com/topic/298458-variable-page-title-help/
Share on other sites

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?

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?

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

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?

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 by QuickOldCar

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?

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.

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.

@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.

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 by benanamen

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 by benanamen
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.