Seriously, I don't know <smiles>. I'm hoping that someone knows what is needed to accomplish this.
Main Topics
Browse All TopicsI want to generate meta tags for my header using info stored in my database. Specifically, I want to be able to pull the title and description info from the table and have that info placed appropriately into the meta tags.
This is the table set up I have:
CREATE TABLE `articles` (
`id` bigint(24) NOT NULL auto_increment,
`title` varchar(150) NOT NULL default '',
`summary` text NOT NULL,
`description` varchar(100) NOT NULL default '',
`body` text NOT NULL,
`parent` bigint(24) NOT NULL default '0',
`seoname` varchar(150) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `title` (`title`),
KEY `seoname` (`seoname`),
KEY `parent` (`parent`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
and this is the portion of my index.php page dealing with the header area:
<?
include("common.php");
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$query = "SELECT *
FROM articles";
$result = mysql_query($query) or die(mysql_error());
while ($articles = mysql_fetch_object($result
$title = $articles->title;
$description = $articles->description;
}
?>
<html>
<head>
<meta name="title" content="<? echo "$title"; ?>">
<meta name="description" content="<? echo "$description"; ?>">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
I have 2 pages set up for testing, each with appropriate info indicating page 1 or page 2, but the code I have above always places the information for page 2 in both page 1's and page 2's meta tags. How can I set this up so that each get their own info?
I'm pretty new at this and hope I've give you all the info you need, but if not, please let me know.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
How do you know that you're getting the wrong page's information?
How in the database table does it tell you which information corresponds to which page?
If you put the same exact query at the top of each page, then of course it will return the same information for each page :)
If you filter the query on each page so it only brings back the correct page's info, then you'll only get the correct page's info on each page.
If you know a certain web page corresponds to a certain `id` record in the table, change the code like this:
For page id 1:
$query = "SELECT *
FROM articles
WHERE `id` = 1";
For page id 2:
$query = "SELECT *
FROM articles
WHERE `id` = 2";
Is that helpful?
Crimson117: How do you know that you're getting the wrong page's information?
When I "view source" when the page has been generated, the title and description head tags contain info entered for one of the other pages. (I've tried placing these fields within the body of the page and that works fine with each page's title and description showing appropriately.)
Crimson117: How in the database table does it tell you which information corresponds to which page?
I don't know. Doesn't all the info in one record in the table automatically relate to the one record? In this case, the title, description and body are all in one record. (The body field holds the page content.)
Crimson117: If you put the same exact query at the top of each page, then of course it will return the same information for each page :)
That makes sense <smiles> ... but there is only one index.php that generates each of the pages so I'm not sure how to proceed.
You say that the correct bosy information is appearing, but the meta information is always the same.
So it seems that the query that populates the content of the page DOES know exactly which row to choose, while the query that gets the META info DOESN'T know exactly which row to choose. The body query is probably filtered with "AND id = $some_number"
Can you post the php that gets the body of the page?
This is from index.php:
<p><?=nl2br($page['body'])
I added the following to the index.php and these do work to pull the content properly from the db to place it in the body:
<h1><?=$page['title']?></h
<h1><?=$page['description'
These are the functions from common.php:
<?
session_start();
include("config.php");
$dbh=mysql_connect ($dbhost, $dbuser, $dbpassword) or die ('I
cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname,$dbh);
define("DBH",$dbh);
function getPage($field='seoname',$
$result = mysql_query("SELECT * FROM articles WHERE
{$field}='{$value}'", DBH);
$page = mysql_fetch_assoc($result)
return $page;
}
function getPages($field='parent',$
$result = mysql_query("SELECT * FROM articles WHERE
{$field}='{$value}'", DBH);
$pages = array();
while( $row = mysql_fetch_assoc($result)
$pages[$row['id']] = $row;
}
return $pages;
}
function AddPage(){
unset($_POST['id']);
$_POST['seoname'] = seoname($_POST['title']);
$query = "INSERT INTO articles (".implode(", ",array_keys
($_POST)).") VALUES ('".implode("', '",array_map
("mysql_real_escape_string
mysql_query($query,DBH) or die( mysql_error() );
return mysql_insert_id();
}
function UpdatePage($pid){
$_POST['seoname'] = seoname($_POST['title']);
$query = "UPDATE articles SET ";
foreach($_POST as $field => $value) {
$query .= "$field = '".mysql_real_escape_strin
($value)."', ";
}
$query = substr($query, 0, strlen($query)-2)." WHERE id =
'{$pid}'";
mysql_query($query,DBH) or die( mysql_error() );
return mysql_affected_rows();
}
function DeletePage($pid){
unset($_POST['step']);
$query = "DELETE FROM articles WHERE id = '{$pid}'";
mysql_query($query,DBH) or die( mysql_error() );
return mysql_affected_rows();
}
function seoname($string){
$string = ltrim($string);
$string = preg_replace( "/ +/", " ", strtolower($string) );
$string = str_replace(' - ', '-', $string);
$string = str_replace(array('-
','%',';','/','?',':','@',
$string);
$search = array(' ', 'ä', 'ö',
'ü','ë','ï','é','è','à','ç
$replace = array
('','ae','oe','ue','e','i'
$string = str_replace($search, $replace, $string);
$string = preg_replace("/[^a-z0-9_-]
$string = strtolower($string);
return urlencode($string);
}
?>
If I put the following in:
<?
include("common.php");
$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
?>
<?
$query = "SELECT *
FROM articles
WHERE `id` = 1";
$result = mysql_query($query) or die(mysql_error());
while ($articles = mysql_fetch_object($result
$title = $articles->title;
$description = $articles->description;
}
?>
<html>
<head>
<meta name="title" content="<? echo "$title"; ?>">
<meta name="description" content="<? echo $description; ?>">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
this is what appears in the head tags:
<html>
<head>
<meta name="title" content="first page title">
<meta name="description" content="first page desc">
and, of course, if I change the id = 1 to id = 2, the info then changes to the page 2 content, which is great. But it remains constant for any page.
You are correct that the page function appears lower in the code.
Do you know if there is a way to change the WHERE portion of the SELECT so that it pulls the data correctly? or am I way off base? <smiles>
Thanks for your interest in this, and for all your help.
Business Accounts
Answer for Membership
by: crimson117Posted on 2006-02-27 at 19:33:37ID: 16061831
Don't you need to filter that query so it gets back the information for the correct page?
like
SELECT * FROM Articles WHERE page=1
or
SELECT * FROM Articles WHERE page=2
?