Page 1 of 1

Dynamic Title and meta description

Posted: Tue Dec 28, 2010 6:51 pm
by dgotti007
Hello All

I am new to PHP and i am looking at existing code trying to find out why the Name and meta description are not being pulled from the database correctly.

Like i said this site has been up and running for sometime now and we finally added google webmaster tool and saw that all of the category pages have the same title and are missing the meta descriptions. I took a look at the code and can see where it should be pulling this information but the only thing that is being populated is the "echo" information.

I know I am not very good at explaining things so if this does not make any sense please ask me any questions you need to.

Code:

Code: Select all

<?php

	session_start();
	include_once("include/configuration/config.php");

	// if category was selected
	if(isset($_REQUEST['id']) && $_REQUEST['id'] != "") 
	{ 
		// get bond info
		$sql = "
			SELECT
				tbl_bond. *,
				tbl_cat.*
			FROM
				tbl_bond
			INNER JOIN tbl_cat ON tbl_bond.cat_id = tbl_cat.c_id
			WHERE
				tbl_bond.cat_id = '" . $_REQUEST['id'] . "'
				AND tbl_bond.is_active LIKE '%Y%'
			ORDER BY
				tbl_bond.b_name
		";
		$rs_bond = mysql_query($sql);
		$SEARCH = "F";
	}else{
		// if no category then must be a failed auto complete search from home page 
		// so do full text search on tbl_bonds and show results
		$query_search = "
			SELECT
				tbl_bond.*,
				tbl_cat.c_name
			FROM
				tbl_bond,
				tbl_cat
			WHERE
				tbl_cat.c_id = tbl_bond.cat_id
				AND MATCH (b_name, b_desc) AGAINST ('" . mysql_escape_string(urldecode($_REQUEST['txtSearch'])) . "')
				AND tbl_bond.is_active LIKE '%Y%'
			ORDER BY
				tbl_bond.b_name
		";
		$rs_bond = mysql_query($query_search);
		$r_rows = mysql_num_rows($rs_bond);
		// full text failed try again using LIKE search
		if($r_rows == 0) { 
		$query_search = "
			SELECT
				tbl_bond.*,
				tbl_cat.c_name
			FROM
				tbl_bond,
				tbl_cat
			WHERE
				tbl_cat.c_id = tbl_bond.cat_id
				AND b_name LIKE '%" . mysql_escape_string(urldecode($_REQUEST['txtSearch'])) . "%'
				AND tbl_bond.is_active LIKE '%Y%'
			ORDER BY
				tbl_cat.c_name,
				tbl_bond.b_name
		";

		$rs_bond = mysql_query($query_search);
		}
		$SEARCH = "T";
	} 

	while($row_bond = mysql_fetch_assoc($rs_bond))
	{
		$bonds[] = $row_bond;
	}
	$num_bonds = count($bonds);
	$half = floor(($num_bonds-1)/2);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><?
			if($row_bond['c_title']) { 
				echo $row_bond['c_title'];
			}
		?></title>
	<meta name="Keywords" content="performance, bonds, performance bond, surety, bond, contract bond, surety bonds, bonding, contract">
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta name="description" content="<? if($row_bond['c_mdesc'] != "") { echo $row_bond['c_mdesc']; } ?>">
	<meta name="Keywords" content="<? echo  $row_bond['c_keywords'];  ?>">
	<?php include('include/meta.php'); ?>
	<script language="JavaScript" type="text/JavaScript">
I have looked at the mysql and all names are matching the code above and none of the categories are blank or missing information.

Re: Dynamic Title and meta descripption

Posted: Wed Dec 29, 2010 8:00 am
by social_experiment
1. If you use the PHP Code button, your code is much easier to read.
2. mysql_escape_string() should be mysql_real_escape_string().

If you want to use values like $row_bond['c_keywords'] outside a while loop, it's best to equate them to a variable or they will only work inside the while loop.

Re: Dynamic Title and meta description

Posted: Wed Dec 29, 2010 3:48 pm
by dgotti007
thanks for the tip about the PHP button, i have done that it does make a huge difference in the way the code looks.


I have added the "real" to the mysql_escape tag, thank you

I am not sure what you mean by the last comment. this was set up by another person and he is not willing to help me to get this to read the title or description form the database. So how would i be able to add the value to the "while loop"?

Re: Dynamic Title and meta description

Posted: Wed Dec 29, 2010 5:10 pm
by social_experiment

Code: Select all

<?php 
 while($row_bond = mysql_fetch_assoc($rs_bond))
  {
     $variable1 = $row_bond['field1'];
     $variable2 = $row_bond['field2'];
     $variable3 = $row_bond['field3'];
  }
?>
Something similar to this. Variables 1 through 3 can now be used outside the loop.