Forum Moderators: coopster
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?
<?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.
//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.
?>
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
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!
<?php
$sql = mysql_query("SELECT name, price FROM products WHERE ID = '$id'");
$title = mysql_fetch_row($sql);
?>
It doesnt seem to work yet though....
<a href="/yourpage.php?id=<?php echo $id ?>">More Info</a>
let us know how you doing
Ski442
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
<?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.
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.
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");
}
?>
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
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.
$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.
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
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 = ''");
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.