You are here:  » Meta Tags with Database Table


Meta Tags with Database Table

Submitted by biglion on Wed, 2009-07-15 15:45 in

I would like to customize the meta tags on the product pages. I've added a table to the database that includes custom product descriptions (as per http://www.pricetapestry.com/node/1278) as well as extra fields for meta-title, meta-keywords, and meta-description.

The custom product descriptions appear fine with the revised code for product.php, but what code is needed and what file needs to be edited for the custom meta tags to appear in the header?

Thanks.

Submitted by support on Wed, 2009-07-15 15:52

Hi,

You'll find where the title and meta tags are set on lines 50/52/54 of products.php;

      $header["title"] = htmlentities($q,ENT_QUOTES,$config_charset);
      $header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
      $header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);

...so to use fields from your database instead, use something like:

      $header["title"] = htmlentities($product["products"][0]["meta-title"],ENT_QUOTES,$config_charset);
      $header["meta"]["description"] = htmlentities($product["products"][0]["meta-description"],ENT_QUOTES,$config_charset);
      $header["meta"]["keywords"] = htmlentities($product["products"][0]["meta-keywords"],ENT_QUOTES,$config_charset);

(no newlines in the database field names of course, if you copy and paste the above block it should appear correctly!)

Hope this helps!

Cheers,
David.

Submitted by biglion on Wed, 2009-07-15 16:43

The table i'm using is not part of the original set. I've created a new table called "description" containing the custom information. From the html/product.php file, the code for the custom on-page title and description is below. I think I need some similar code for the header in the products.php file?

<?php
$desc_sql 
"SELECT name FROM `".$config_databaseTablePrefix."description` WHERE sku='".$mainProduct["name"]."'";
if (
database_querySelect($desc_sql,$desc_rows))
{
  print 
$desc_rows[0]["name"];
}
else
{
  print 
$mainProduct["name"];
}
?>

<?php
$desc_sql 
"SELECT description FROM `".$config_databaseTablePrefix."description` WHERE sku='".$mainProduct["name"]."'";
if (
database_querySelect($desc_sql,$desc_rows))
{
  print 
$desc_rows[0]["description"];
}
else
{
  print 
$mainProduct["description"];
}
?>

Submitted by support on Wed, 2009-07-15 18:15

Hi,

Ah - in that case have a go with something like this;

  $sql = "SELECT meta-title,meta-description,meta-keywords FROM `".$config_databaseTablePrefix."description` WHERE sku='".$product["products"][0]["name"]."'";
  database_querySelect($sql,$rows);
  $header["title"] = htmlentities($rows[0]["meta-title"],ENT_QUOTES,$config_charset);
  $header["meta"]["description"] = htmlentities($rows[0]["meta-description"],ENT_QUOTES,$config_charset);
  $header["meta"]["keywords"] = htmlentities($rows[0]["meta-keywords"],ENT_QUOTES,$config_charset);

Cheers,
David.

Submitted by biglion on Wed, 2009-07-15 18:59

The following error ocurred...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bl/public_html/compare/includes/database.php on line 21

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/bl/public_html/compare/includes/database.php on line 26

Warning: Cannot modify header information - headers already sent by (output started at /home/bl/public_html/compare/includes/database.php:21) in /home/biglion2/public_html/compare/html/header.php on line 6

Submitted by support on Wed, 2009-07-15 19:18

Hi,

The first thing I noticed is that database_safe() should be in place...

  $sql = "SELECT meta-title,meta-description,meta-keywords FROM `".$config_databaseTablePrefix."description` WHERE sku='".database_safe($product["products"][0]["name"])."'";

...but other than that; can you double-check the field names? Are they exactly as shown, e.g. meta-title, meta-description, meta-keywords?

Cheers,
David.

Submitted by biglion on Wed, 2009-07-15 23:43

Same error.

Yes, the table and field names look to be correct.
Table: description
fields: sku, name, description, meta-title, meta-keywords, meta-description

Submitted by support on Thu, 2009-07-16 05:24

Hi,

The debug version of includes/database.php should reveal the actual error. Click here to download, extract and upload database.php to the /includes/ folder of your site - then when you run the script again the actual MySQL error message will be displayed which should indicate the problem...

Cheers,
David.

Submitted by biglion on Thu, 2009-07-16 13:01

The error is:

[SELECT meta-title,meta-description,meta-keywords FROM `description` WHERE sku='51625A'][Unknown column 'meta' in 'field list']

Submitted by support on Thu, 2009-07-16 13:03

Hi,

That sounds like the hyphen in the field name is breaking the structure, so back-ticks would be required. Have a go with:

  $sql = "SELECT `meta-title`,`meta-description`,`meta-keywords` FROM `".$config_databaseTablePrefix."description` WHERE sku='".database_safe($product["products"][0]["name"])."'";

Cheers,
David.

Submitted by biglion on Fri, 2009-07-17 12:33

That works now, thanks!

One additional issue. How can I get the regular meta-tags to show if I don't have a record for a custom meta tag? Right now, it's just blank if the record doesn't exist.

Submitted by support on Fri, 2009-07-17 13:30

Hi,

That's a simple case of using an IF statement on the value before using it; otherwise using the original code; so in place of:

  $header["title"] = htmlentities($rows[0]["meta-title"],ENT_QUOTES,$config_charset);
  $header["meta"]["description"] = htmlentities($rows[0]["meta-description"],ENT_QUOTES,$config_charset);
  $header["meta"]["keywords"] = htmlentities($rows[0]["meta-keywords"],ENT_QUOTES,$config_charset);

...use:

  if ($rows[0]["meta-title"])
  {
    $header["title"] = htmlentities($rows[0]["meta-title"],ENT_QUOTES,$config_charset);
  }
  else
  {
    $header["title"] = htmlentities($q,ENT_QUOTES,$config_charset);
  }
  if ($rows[0]["meta-description"])
  {
    $header["meta"]["description"] = htmlentities($rows[0]["meta-description"],ENT_QUOTES,$config_charset);
  }
  else
  {
    $header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
  }
  if ($rows[0]["meta-keywords"])
  {
    $header["meta"]["keywords"] = htmlentities($rows[0]["meta-keywords"],ENT_QUOTES,$config_charset);
  }
  else
  {
    $header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);
  }

Cheers,
David.

Submitted by biglion on Fri, 2009-07-17 19:13

Works great, thanks!

Submitted by philstone on Wed, 2011-03-16 15:31

Hi david

trust you are well

was just wondering how would i go about including the main product category in the product page meta tags?

regards

Phil Stone
www.buy24-7.net

Submitted by support on Wed, 2011-03-16 16:43

Hi Paul,

Sure - for example to add the category as a keyword look for the following line in products.php:

      $header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);

...and then immediately afterwards, add:

      if ($rows[0]["category"])
      {
        $header["meta"]["keywords"] .= ",".htmlentities($rows[0]["category"],ENT_QUOTES,$config_charset);
      }

Cheers,
David.
--
PriceTapestry.com