Grabbing Category Name for <title>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neilson
Forum Newbie
Posts: 4
Joined: Fri Oct 28, 2005 5:42 pm

Grabbing Category Name for <title>

Post by neilson »

I want to use the directory script at IndexScript.com but the page titles it generates are pulled from a global setting so ALL page titles are the same. I want to modify it so that if I am in category 2 (show_cat.php?cat_id=2) instead of this pages title being what ever you set $meta_title as, I want it to pull from the name of what the category is.

Ultimatley I'd like to write a function that will pull both the title of the category and the description from the database that I can include in show_cat.php so I can dynamically change the <title> tag and Meta Description tag depending on what category is selected.

I hope someone can help me!

Just to let you know, the data base structure for the category table is as follows:

Code: Select all

cat_id,     parent_id,     name,     description,                   indexdisplay  
    8,             1,             Test,       Test Description,                  Y,
show_cat.php

Code: Select all

<?php

//////////////////////////////////////////////////////////////////////
// expects $_GET values for:
// 1. cat_id
// 2. page
//////////////////////////////////////////////////////////////////////

session_start();

///////////////////////////////////////////////////////////////////////////
// get meta information
///////////////////////////////////////////////////////////////////////////
include_once("config.php");


mysql_connect($db_host, $db_login, $db_pwd) or die("Cannot connect to DB!");
mysql_select_db($db_name) or die("Cannot select DB!");
$sql = "select meta_title, meta_description, meta_keywords, seo_urls from dir_settings limit 1";
$rst = mysql_query($sql);

if(mysql_affected_rows() == 0) {
  $meta_title = "";
  $meta_description = "";
  $meta_keywords = "";
  $seo_urls = "N";
}
else {
  $row = mysql_fetch_array($rst);
  $meta_title = $row['meta_title'];
  $meta_description = $row['meta_description'];
  $meta_keywords = $row['meta_keywords'];
  $seo_urls = $row['seo_urls'];
}

mysql_close();

?>
<?php

mysql_connect($db_host, $db_login, $db_pwd) or die("Cannot connect to DB!");
mysql_select_db($db_name) or die("Cannot select DB!");

  $sql = "select cat_id, name from dir_cat where parent_id = " . $start_id;
  $rst = mysql_query($sql);
  
 mysql_close();

?>


<html>
<head>
  <title><?php echo($path); ?> <?php echo($_GET['name']); ?></title>
  <meta http-equiv="Content-Language" content="en-us">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

  <meta name="description" content="<?php echo($meta_description); ?>">
  <meta name="keywords" content="<?php echo($meta_keywords); ?>">
  
  <link href= <?php echo($dir_root_path . "style/styles.css"); ?> rel="stylesheet" type="text/css">
</head>
<body>

<div align="center">
<div id="container">


<?php

if($_SESSION["logged_in"] == "yes") {
  include('./style/header_adm.php');
  print "<center><br><br><a href=\"" . $dir_root_path . "add_cat.php?parent_id=". $_GET['cat_id'] . "\">
    Add Sub-category</a>&nbsp&nbsp|&nbsp&nbsp";
  print "<a href=\"" . $dir_root_path . "pend_cat.php" . "\">Pending Categories</a>&nbsp&nbsp|&nbsp&nbsp";
  print "<a href=\"" . $dir_root_path . "pend_url.php" . "\">Pending URLs</a>&nbsp&nbsp|&nbsp&nbsp";
  print "<a href=\"" . $dir_root_path . "stats.php" . "\">Stats</a>&nbsp&nbsp|&nbsp&nbsp";
  print "<a href=\"" . $dir_root_path . "settings.php" . "\">Settings</a><br><br></center>";
}
else {
  include('./style/header.php');
}

?>

<div id="content">

<?php

include_once("./include/utils.php");

mysql_connect($db_host, $db_login, $db_pwd) or die("Cannot connect to DB!");
mysql_select_db($db_name) or die("Cannot select DB!");

///////////////////////////////////////////////////////////////////////////
// display intermediate menu
///////////////////////////////////////////////////////////////////////////
//
print "<div id=\"intermenu\">";
print "<a href=\"" . $dir_root_path . "SuggestCategory.php?parent_id=" . $_GET['cat_id'] . "\">Suggest Category</a> | " .  "<a href=\"" . $dir_root_path . "add.php?cat_id=" . $_GET['cat_id'] . "\">Add URL</a>";
print "</div>";

///////////////////////////////////////////////////////////////////////////
// get the path to current category
///////////////////////////////////////////////////////////////////////////

$path = fngetcatpath($_GET['cat_id'], $dir_root_path);
print $path . " <br><br><br>";

///////////////////////////////////////////////////////////////////////////
// get the existing sub-categories
///////////////////////////////////////////////////////////////////////////

print "<div id=\"intermediate\">";
print "<br>Sub-categories in this category:<br><br>";

fndisplaysubcats($_GET['cat_id'], $dir_root_path);

///////////////////////////////////////////////////////////////////////////
// display featured sites
///////////////////////////////////////////////////////////////////////////

$sql = "select title, description, url,name from dir_url where " .
  "cat_id = " . fnpreparesql($_GET['cat_id']) . " and " .
  "featured = 'Y' order by title";

$rst = mysql_query($sql);

if(mysql_affected_rows() > 0) {
  print "<br>";
  print "<div id=\"feature\">";
  print "<br>Featured URLs in this category:<br><br>";

  while($row = mysql_fetch_array($rst)) :
    print "<a href=\"" . $row['url'] . "\">" . $row['title'] . "</a>";
    if($_SESSION["logged_in"] == "yes") {
      print "&nbsp&nbsp[<a href=\"" . $dir_root_path . "edit_url.php?cat_id=" . $_GET['cat_id'] .
        "&url=" . $row['url'] . "&src=app\">EDIT</a>]";
    }
    print "<br><span class=\"sub-text\">" . $row['description'] . "<br>" .
      "<a href=\"" . $row['url'] . "\">" . $row['url'] . "</a></span><br><br>";
  endwhile;

  print "</div>";
}

print "<br></div>";

///////////////////////////////////////////////////////////////////////////
// get display order
///////////////////////////////////////////////////////////////////////////

$display_order = fngetdisplayorder();

///////////////////////////////////////////////////////////////////////////
// display listed urls in this category
///////////////////////////////////////////////////////////////////////////

print "<br><br><br>Resources listed in this category:<br><br><br>";

$sql = "select title, description, url from dir_url where " .
  "cat_id = " . fnpreparesql($_GET['cat_id']);
if($display_order == 1) {
  $sql = $sql . " order by date";
}
else {
  $sql = $sql . " order by title";
}

$rst = mysql_query($sql);

if(mysql_affected_rows() == 0) {
  print "None";
}
else {
  $page = $_GET['page'];
  if($page == 0) {
    $page = 1;
  }
  mysql_data_seek($rst, ($page - 1) * 20);

  $cnt = ($page - 1) * 20;
  while($cnt < mysql_affected_rows() && $cnt < $page * 20) :

    $row = mysql_fetch_array($rst);
    print "<a href=\"" . $row['url'] . "\">" . $row['title'] . "</a>";
    if($_SESSION["logged_in"] == "yes") {
      print "&nbsp&nbsp[<a href=\"" . $dir_root_path . "edit_url.php?cat_id=" . $_GET['cat_id'] .
        "&url=" . $row['url'] . "&src=app\">EDIT</a>]";
      print "&nbsp&nbsp[<a href=\"" . $dir_root_path . "del_rec.php?item=url&cat_id=" . $_GET['cat_id'] .
        "&url=" . $row['url'] . "&src=app\">DELETE</a>]";
    }
    print "<br><span class=\"sub-text\">" . $row['description'] . "<br>" .
  //href the url to the url "<a href=\"" . $row['url'] . "\">" . $row['url'] . "</a></span><br><br><br>";
	 $row['url'] ."</span><br><br><br>";

    $cnt = $cnt + 1;
  endwhile;

  if(mysql_affected_rows() > 20) {
    $pagecnt = ceil(mysql_affected_rows() / 20);
    print "<br><br><br><center>Page " . $page . " of " . $pagecnt . ".&nbsp;&nbsp;&nbsp;&nbsp;";

    if($seo_urls == "Y") {      
      print "<a href=\"" . $dir_root_path . "cat/" . $_GET['cat_id'] . "/page/1/\">First</a>&nbsp;";
      if($page > 1) {
        print " | <a href=\"" . $dir_root_path . "cat/" . $_GET['cat_id'] . "/page/" . ($page - 1) . "/\">Prev</a>&nbsp;";
      }
      if($page < $pagecnt) {
        print " | <a href=\"" . $dir_root_path . "cat/" . $_GET['cat_id'] . "/page/" . ($page + 1) . "/\">Next</a>&nbsp;";
      }
      print " | <a href=\"" . $dir_root_path . "cat/" . $_GET['cat_id'] . "/page/" . $pagecnt . "/\">Last</a></center>";
    }
    else {
      print "<a href=\"". $dir_root_path . "show_cat.php?cat_id=" . $_GET['cat_id'] . "&page=1\">First</a>&nbsp;";
      if($page > 1) {
        print " | <a href=\"" . $dir_root_path . "show_cat.php?cat_id=" . $_GET['cat_id'] . "&page=" . ($page - 1) . "\">Prev</a>&nbsp;";
      }
      if($page < $pagecnt) {
        print " | <a href=\"" . dir_root_path . "show_cat.php?cat_id=" . $_GET['cat_id'] . "&page=" . ($page + 1) . "\">Next</a>&nbsp;";
      }
      print " | <a href=\"" . $dir_root_path . "show_cat.php?cat_id=" . $_GET['cat_id'] . "&page=" . $pagecnt . "\">Last</a></center>";
    }
  }
}

mysql_close();

?>
</div>


</div>
<?php include('./style/footer.php'); ?>
</div>

</body>
</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I use dynamic titles with most of my apps. Why not just change this code...

Code: Select all

<title><?php echo($path); ?> <?php echo($_GET['name']); ?></title>
to something like this...

Code: Select all

<title><?php show_title(); ?></title>
And then develop your function show_title() to display whatever title you want.
neilson
Forum Newbie
Posts: 4
Joined: Fri Oct 28, 2005 5:42 pm

Post by neilson »

And then develop your function show_title() to display whatever title you want.
Would that be a simple function to write?

Im not really a programmer, more of a graghic/web designer
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It's not hard to write, but you should know what you are doing. You will need to know what page you are on and what you want your title to be, but after that it's a snap.

When I started doing this I used to use arrays for page titles. Then I would echo the array value in the title when displaying...

Code: Select all

<?php
$page_titles = array();
$page_titles['index'] = 'Welcome to our site';
$page_titles['contact'] = 'Send us a message';
$page_titles['about'] = 'About our company';

$page_name = basename($_SERVER['PHP_SELF'], '.php');

$page_title = $page_titles[$page_name];
?>
<html>
    <head>
        <title><?php echo $page_title; ?></title>
    </head>
    <body>
    </body>
</html>
neilson
Forum Newbie
Posts: 4
Joined: Fri Oct 28, 2005 5:42 pm

Post by neilson »

Thanks,

You have given me some inspiration. I think I'll be able to hack somthing together.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just wait until you learn how to database stuff and use that data. Hoo baby, you'll be flying high then. Good luck to you.
Post Reply