Forum Moderators: coopster

Message Too Old, No Replies

Using PHP code for <title></title> variable

         

peter123

12:39 pm on Jan 18, 2010 (gmt 0)

10+ Year Member



At the moment all the pages I create are done with a products.php template page and have id numbers. There is html for each page and the page name is controlled with code <%PRODUCT_NAME%>,

I am trying to create different page <title> tags for each page.

I have tried this:

<title><?php print $title; ?> - blah, blah standard text</title>

This however only shows "- blah, blah standard text".

How do I get the <%PRODUCT_NAME%> to show up?

What code is required, and where do I put it?

peter123

9:41 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



hi stout,

ok i've figured out why it wasnt reading, its the way its setup on my server. I wont go into details. Anyway, i reverted back to your coding of: $query = "SELECT * FROM products";

And it comes up with:

Connected to MySQL!
Connected to Database!Title: Electrical Products

So the text in the first name field title now shows up. Great!

Whats the next step? How do i create the variable to the $id as the other code with this in it didnt work.

Cheers

StoutFiles

10:45 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're going to use the URL to pass the id variable to the page. Say the page you are currently using is example.com/database_test.php. If you want to pass an id of 4 to the page...

example.com/database_test.php?id=4

Pass an id of 602...

example.com/database_test.php?id=602

<?

$username = your_username;
$password = your_password;
$database_name = your_database_name;

mysql_connect("localhost", $username, $password) or die(mysql_error());
echo "Connected to MySQL!<br />";
mysql_select_db($database_name) or die(mysql_error());
echo "Connected to Database!";

//change the username, password, and database_name to yours.

$id = $_GET['id']; //gets variable value from URL

$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
while($row = mysql_fetch_array($sql))
{
$title = $row['name'];
echo "Title: ".$title."<br>";
}
?>

If this works, don't think you're done! The variable still needs to be parsed for hacking attempts before used with regular people. But one step at a time.

peter123

8:49 am on Jan 29, 2010 (gmt 0)

10+ Year Member



Ok Stout, this works too. What now?

Thanks.

StoutFiles

1:29 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able to put this on your page now. PHP code before everything, and the edit the <title> part how it's done below.

<?

$username = your_username;
$password = your_password;
$database_name = your_database_name;

mysql_connect("localhost", $username, $password) or die(mysql_error());
echo "Connected to MySQL!<br />";
mysql_select_db($database_name) or die(mysql_error());
echo "Connected to Database!";

//change the username, password, and database_name to yours.

$id = $_GET['id']; //gets variable value from URL
$id = addslashes($id);

$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
$row = mysql_fetch_array($sql)
$title = $row['name'];
?>

...

<title><?php echo $title; ?></title>

addslashes() isn't bulletproof, but it helps. I would also make a MYSQL account with read-only access and use that username/password to connect to the database for your users.

peter123

3:05 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



Hi Stout,

I added all this to the top of the page and changed <title> part, but it doesnt work for me yet.

StoutFiles

3:17 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
$title = mysql_fetch_row($sql);

Try that instead.

peter123

3:21 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



I am assuming this code should be on the same page as the <title> tag and not the other page I pasted into this thread? I've tried both anyway.

peter123

3:26 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



no luck there either i'm afraid.

StoutFiles

3:36 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Go back to the test page and make sure this works.

<?

$username = your_username;
$password = your_password;
$database_name = your_database_name;

mysql_connect("localhost", $username, $password) or die(mysql_error());
mysql_select_db($database_name) or die(mysql_error());

//change the username, password, and database_name to yours.

$id = $_GET['id']; //gets variable value from URL

$sql = mysql_query("SELECT name FROM products WHERE ID = '$id'");
while($row = mysql_fetch_array($sql))
{
$title = $row['name'];
}
?>

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
Example page. Title should be on browser.
</body>
</html>

peter123

3:51 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



That works fine, however i've just realised (duh), the page the <title> is on is a .html page not .php.

If i change the file type to .php the system will probably not work. Does it have to be on this particular page? Or can it be placed in the products.php page i pasted on this thread earlier?

StoutFiles

4:19 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Changing the file type from .html to .php shouldn't break anything. If you're using php include statements to call the html page you will need to change those as well.

include('page.html')

to include('page.php')

peter123

4:34 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



Yeah, I changed

include_once ("page.html");

include_once ("page.php");

still didnt work though for some reason. But strangely works when i go to mydomain.com/page.php?id=765

The page that doesnt work is mydomain.com/product.php?id=765

Something must be overriding?

peter123

4:43 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



I also noticed this:

The url shows:

mydomain.com/products.php?product_id=123

When i remove the 'product_' so it shows

www.mydomain.com/products.php?id=123

The title works!

How do i sort this as I also have parent_id to worry about after this!

peter123

4:55 pm on Jan 29, 2010 (gmt 0)

10+ Year Member



I changed the 'id' to the following:

$id = $_GET['parent_id']; //gets variable value from URL

so this works, but is there a way to code this so it either shows parent_id or product_id like:

$id = $_GET['parent_id' or 'product_id']; //gets variable value from URL

StoutFiles

6:07 pm on Jan 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$id = $_GET['parent_id' or 'product_id'];

$id = $_GET['parent_id'];
if($id == "")
{$id = $_GET['product_id'];}

eelixduppy

6:11 pm on Jan 29, 2010 (gmt 0)




$id = $_GET['parent_id'];
if($id == "")
{$id = $_GET['product_id'];}

Ternary operator [php.net] is pretty cool here:


$id = empty($id) ? $_GET['product_id'] : $_GET['parent_id'];

peter123

1:26 pm on Jan 31, 2010 (gmt 0)

10+ Year Member



Hi StoutFiles,

All sorted. Thanks for all your help, wouldnt have been able to do that without it.

Cheers.

This 47 message thread spans 2 pages: 47