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

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

10+ Year Member



Maybe wasnt clear about the <%PRODUCT_NAME%> part. The html has been made standard so <%PRODUCT_NAME%> shows the actual title when the product page is created.

Thanks.

russkern

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

10+ Year Member



IT seems like there is info missing from your question...

have you set a variable somewhere that $title= PRODUCT_NAME ?

StoutFiles

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

WebmasterWorld Senior Member 10+ Year Member



You need to set the variable first. Is the variable value currently in a database or being passed in the URL?

peter123

1:38 pm on Jan 18, 2010 (gmt 0)

10+ Year Member



Variable value is in a mysql database. But where and what code do i need to set the variable $title= PRODUCT_NAME?

Sorry, as you can see i'm a newbie at php...

peter123

3:25 pm on Jan 18, 2010 (gmt 0)

10+ Year Member



I found this in another forum which looks like it could work:

<?php
$sql = mysql_query("SELECT link FROM oblast_metal WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>
<head>
....
<title><?php echo $title[0]; ?></title>
....
</head>

But whats "SELECT link FROM oblast_metal" ?

Thanks for the help.

StoutFiles

4:16 pm on Jan 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

$sql = mysql_query("SELECT link FROM oblast_metal WHERE ID = '$id'");
while($row = mysql_fetch_array($sql)){
{
$title = $row['PRODUCT_NAME'];
}

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

//Also, make sure you set $id to a value as well before you make that query.

?>

ski442

1:08 am on Jan 19, 2010 (gmt 0)

10+ Year Member



But whats "SELECT link FROM oblast_metal" ?

This is the call to your database to return your stored values.

As you may well be calling many values from your database, like price, size, weight etc change the "link" above to this * so your code will now read,

<?php
$sql = mysql_query("SELECT * FROM oblast_metal WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>

Adding the star in the select call means,
SELECT everything FROM database table name WHERE the id in the table matches $id

You will need to change this "oblast_metal" to your database table name.
Hope this helps as I know had mad these little things can make you.
Ski442

peter123

11:16 am on Jan 19, 2010 (gmt 0)

10+ Year Member



Hi ski442,

my database table is called 'products' and there is an 'id' field and a 'name' field and this is all i need.

How do I modify the code so that it links up to the 'name'field in the table?

Is this correct?

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

It doesnt seem to work yet though....

Thanks for your help!

ski442

12:17 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



Hi Peter123.
Yes thats it, so the script now will only be able call just that field only, or if you want to call 2 feilds then use

<?php
$sql = mysql_query("SELECT name, price FROM products WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>

It doesnt seem to work yet though....

how are you passing the $id varible from the page where this script is located?
In the URL maybe or sessions.
If you pass the var in the url you can use something like

<a href="/yourpage.php?id=<?php echo $id ?>">More Info</a>

let us know how you doing
Ski442

peter123

12:54 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



Hi ski442,

I am just using <title><?php echo $title; ?></title>

All i want is the browser title to vary with each name, but nothing shows up yet - its just blank.

StoutFiles

1:34 pm on Jan 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't have $id set as anything. Set $id in the URL.

http:// www.yoursite.com/products.php?id=4

Then on your page...

<?php
$id = $_GET['id'];
?>

Now that $id is set as 4, your sql query will find the row that has an value of 4 in the ID column. If you want the id to be 232 instead, then change it in the URL.

http:// www.yoursite.com/products.php?id=232

peter123

2:23 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



Hi Stout,

The pages all work fine, its just the <title> tag that isnt displaying the text for the name related to the id.

ski442

2:49 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



[quote]I am just using <title><?php echo $title; ?></title> [?quote]
then you need to change this to
<title><?php echo $name; ?></title>
as this is the only feild you are calling from your DB
Try and let us know
ski442
we should get there soon!

StoutFiles

3:03 pm on Jan 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Forget about the <title> field for now. Just try to get something echoed from your database. Prove that you can connect to it and use this instead to get a value out. If you don't understand what you're doing this will continue to take forever.

<?php

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.

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

If you don't see "Connected to MySQL" or "Connected to Database" you aren't even connected, and need to use a correct username, password, and database name. If you don't see a list of titles then you have the table name or column name wrong.

peter123

3:35 pm on Jan 19, 2010 (gmt 0)

10+ Year Member



Hi ski442,

I tried this but still not working. Is there a particular place to put this code?
<?php
$sql = mysql_query("SELECT name, price FROM products WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>

I just placed it at the bottom of the coding and have noticed the following error is shown at the bottom of the page:

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/p/r/.......
------------------------------------------------------------------
Hi StoutFiles,

I am connecting as everything else is coming from the database so surely the page wouldnt work otherwise.

StoutFiles

3:53 pm on Jan 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi StoutFiles,

I am connecting as everything else is coming from the database so surely the page wouldnt work otherwise.

Copy and paste your output.

peter123

2:44 pm on Jan 27, 2010 (gmt 0)

10+ Year Member



Sorry, been away last week. Where do i copy the output?

ski442

10:22 pm on Jan 27, 2010 (gmt 0)

10+ Year Member



Copy and paste your page script and lets see what we can find
Ski442

peter123

9:35 am on Jan 28, 2010 (gmt 0)

10+ Year Member



Hi Ski442, heres the code without
<?php
$sql = mysql_query("SELECT name, price FROM products WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>

As that didnt seem to work, unless i placed it in the wrong area of code?

<?php
define ("OSS_PATH", "");
session_start ();
set_time_limit(3000);
include (OSS_PATH."install/config.php");
$conOSS = mysql_connect ($dbConfig["host"], $dbConfig["user"], $dbConfig["pass"]);
mysql_select_db($dbConfig["db"], $conOSS);
$nPageSize = 30;
if (isset ($_GET["parent_id"]))
{
$res = mysql_query ("select * from products where published=1 and id=".intval ($_GET["parent_id"]), $conOSS);
$rec = mysql_fetch_array ($res);
$tplCategoryPage = $rec["html_page"];
}
else $tplCategoryPage = file_get_contents (OSS_PATH."modules/Members/templates/category_page.html");

if (isset ($_GET["product_id"]))
{
$res = mysql_query ("select * from products where published=1 and id=".intval ($_GET["product_id"]), $conOSS);
$rec = mysql_fetch_array ($res);

$rec["html_page"] = str_replace ("<%OSS_PATH%>", OSS_PATH, $rec["html_page"]);
$rec["html_page"] = str_replace ("<%PRODUCT_ID%>", $rec["id"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%PRODUCT_NAME%>", $rec["name"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%RAND%>", time (), $rec["html_page"]);

if ($_POST["send"])
{
$strErr = "";
if ((!strlen ($_POST["name"])) ¦¦ !strlen ($_POST["email"]) ¦¦ !strlen ($_POST["email2"]) ¦¦ (isset($_POST["country"]) and !strlen ($_POST["message"]))) $strErr = "You did not enter data in one of fields.";
else if ($_POST["email"] != $_POST["email2"]) $strErr = "You entered different e-mails.";
else if ($_SESSION["reg_code"] != $_POST["captcha"]) $strErr = "You entered invalid code. Please, try again.";

foreach ($_POST as $k => $v) $_POST[$k] = htmlspecialchars (strip_tags ($v));

if (!strlen ($strErr))
{
$arr = array ("name", "email", "email2",'country', "phone", "message", "send", "captcha", "product_id", "product_name");
$strOther = "";
foreach ($_POST as $k => $v)
{
if (!in_array ($k, $arr)) $strOther .= $v."\n";
$_POST[$k] = $v;
}
$countries_array=array(
'US'=>'United States',
'GB'=>'United Kingdom',
'AU'=>'Australia',
'CA'=>'Canada'
);

$strEnquiry =
"Product: ".$rec["name"]."\n".
"Name: ".$_POST["name"]."\n".
"Email: ".$_POST["email"]."\n".
"Phone: ".$_POST["phone"]."\n".
"Country: ".$countries_array[$_POST["country"]]."\n".
"Message: \n".$_POST["message"]."\n".
"Other info: \n".$strOther
;
$hashEnquiry = serialize($strEnquiry);
mysql_query ("insert into enquiries (product_id, product_name,country, enquiry,hash, t) values (".
"\"".mysql_escape_string ($_POST["product_id"])."\", ".
"\"".mysql_escape_string ($_POST["product_name"])."\", ".
"\"".mysql_escape_string ($_POST["country"])."\", ".
"\"".mysql_escape_string ($strEnquiry)."\", ".
"\"".mysql_escape_string ($hashEnquiry)."\", ".
"\"".time ()."\") ",
$conOSS);
if (!mysql_insert_id ($conOSS)) $strErr = "Failed to send your enquiry. Error: ".mysql_error ($conOSS);
}

unset ($_SESSION["reg_code"]);
$fp = fsockopen("pricemeup.com", '80',$err_num, $err_msg, 30);
fputs($fp, "GET /pmu/send_enquiries.php HTTP/1.0\r\n");
fputs($fp, "Host: pricemeup.com\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fclose($fp);

if (strlen ($strErr))
{
$rec["html_page"] = str_replace ("<%NAME%>", $_POST["name"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%EMAIL%>", $_POST["email"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%COUNTRY%>", $_POST["country"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%EMAIL2%>", $_POST["email2"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%PHONE%>q", $_POST["phone"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%MESSAGE%>", $_POST["message"], $rec["html_page"]);
$rec["html_page"] = str_replace ("<%MSG%>", $strErr, $rec["html_page"]);
$alert='';
}
else{
$alert='<script>alert("Thank you - your enquiry has been sent!")</script>';
}

}
include_once ("top.html");
echo eregi_replace ("<%([A-Z0-9_]+)%>", "", $rec["html_page"]);
echo $alert;
include_once ("bottom.html");
}
else
{
$strItems = "";
/*
$res = mysql_query("select * from products where published=1 and parent_id=".
intval ($_GET["parent_id"])." order by is_product limit ".intval ($_GET["page_num"]).", ".$nPageSize,
$conOSS);
*/
$res = mysql_query("select * from products where published=1 and parent_id=".
intval ($_GET["parent_id"])." order by is_product,name" , $conOSS);
while ($rec = mysql_fetch_array ($res))
{
$strItem = $rec["is_product"] ? $rec["html_product_item"] : $rec["html_category_item"];
$strItem = str_replace ("<%ID%>", $rec["id"], $strItem);
$strItem = str_replace ("<%NAME%>", $rec["name"], $strItem);
$strItems .= $strItem;
}

$strPageNums = "";
$res = mysql_query("select count(id) as num from products where published=1 and parent_id=".intval ($_GET["parent_id"]), $conOSS);
$rec = mysql_fetch_array ($res);
$n = ceil ($rec["num"] / $nPageSize);
for ($i = 0; $i < $n; $i++)
{
$str = ($i == $_GET["page_num"]) ?
"(".($i + 1).")" : "<a href=\"products.php?parent_id=".$_GET["parent_id"]."&page_num=".$i."\">".($i + 1)."</a>";
$strPageNums .= $str;
}

$tplCategoryPage = str_replace ("<%PRODUCTS%>", $strItems, $tplCategoryPage);
$tplCategoryPage = str_replace ("<%PAGE_NUMS%>", '', $tplCategoryPage);
include_once ("top.html");
echo eregi_replace ("<%([A-Z0-9_]+)%>", "", $tplCategoryPage);
include_once ("bottom.html");
}

?>

ski442

11:10 am on Jan 28, 2010 (gmt 0)

10+ Year Member



So reading your other posts too, all you need is have the title of each page change to the products on display and that page consists of the code in your last post.

If this is correct then the select statement will need to go before the title tag, as you can only echo results after the call the to db.

If you place the select in the head i do not know how you stand on seo or web compliance.

Test this out and see if it works.
Ski442

StoutFiles

11:28 am on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I really think you should back way up and make an entirely new page to learn how to make database calls, just a blank page that has variables echoed when you call them from the database. Once you get how it works then updating pages will be much easier.

peter123

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

10+ Year Member



Hi ski442,

Placing the 'select' code in the head did not do anything either.

:-(

StoutFiles

1:55 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Placing the 'select' code in the head did not do anything either.

Of course it didn't. You have no database connection code before the select statement...you haven't even proved that you can connect to your database.

Do this. Make a whole new page. Call it database_test.php. Copy this code into your new page.

<?php

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

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

Now. Change these three variables in the code above to be your username, password, and database_name.

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

Good? Ok! Run that page. If you don't see...

Connected to MySQL!
Connected to Database!

And then you should see a big list of titles.

Do this and report what comes back. Give up on the page you're trying to edit, you have to learn to crawl before you sprint.

peter123

2:30 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



Hi Stout,

This is what is displayed on the page:

Connected to MySQL!
Connected to Database!
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/p/r/mydomain/public_html/pmu/database_test.php on line 15

Any ideas on this?

Thanks.

StoutFiles

3:42 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace this

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

with this.

$query = "SELECT * FROM products";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$title = $row['name'];
echo "Title: ".$title."<br>";

Report back on result.

peter123

4:42 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



Hi Stout,

There was a "Connected to MySQL!
Connected to Database!Table doesn't exist" message,

so I changed the first line:
$query = "SELECT * FROM products";

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

now the following appears:

Connected to MySQL!
Connected to Database!Query was empty

StoutFiles

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

WebmasterWorld Senior Member 10+ Year Member




There was a "Connected to MySQL!
Connected to Database!Table doesn't exist" message,

Okay, that's good. That means the table name you're using is wrong. Are you sure the table name is "products"?

so I changed the first line:
$query = "SELECT * FROM products";

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

$id doesn't have a value! $id is a variable, like 'x'. $id defaults to a value of nothing. You have to give $id a value before it can be used in a query, otherwise you're just saying:

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

peter123

7:12 pm on Jan 28, 2010 (gmt 0)

10+ Year Member



Hi Stout,

Table is definitely called products.

Within this table is the 'id' field and 'name' field. But how do i get a value for $id so that it links with the associated 'name' field?

$id = ?

Thanks.

StoutFiles

7:47 pm on Jan 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Table is definitely called products.

You need to find out why you can't open it then.


Within this table is the 'id' field and 'name' field. But how do i get a value for $id so that it links with the associated 'name' field?

$id = ?

You aren't ready for this step. If you can't get the table open there's no point in working with the query. One step at a time.

This 47 message thread spans 2 pages: 47