Dynamic Title

Hello,

I am new to PHP and I have been trying to create a dynamic title by seeing examples in web world and I finally got this code to work:

<head><title>
<?php 
require("includes/connectdb.php");
$nr_tresources=0;
 $count_resources=mysql_query("SELECT * from resources");
 while ($count_my_resources=mysql_fetch_array($count_resources))
 {
 $nr_tresources++;
 }


if (isset($_REQUEST['id_cat']))
   {
   $id_cat=$_REQUEST['id_cat'];
   } else {$id_cat=0;}
  if (is_numeric($id_cat)==false)
   {$id_cat=0;}
 $lista_cat=mysql_query("SELECT * from resource_categories where id=$id_cat");
 $rez_cat=mysql_fetch_array($lista_cat);
 if (empty($rez_cat))
  {
  $id_cat=0;
  }
  
 $get_categories=mysql_query("SELECT * from resource_categories order by name ASC");
  while ($show_categories=mysql_fetch_array($get_categories))
$name=$show_categories['name'];
 
  if ($id_cat!=$show_categories['id'])
	echo "3ds Max Resources&nbsp; >>&nbsp; $name";
  else
        echo "3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed ";

?></title>

You can visit the following page to see yourself: http://www.3dsmaxresources.com/list_resources.php

The problem is that when I visit any category in the resources section, it is showing the same category name instead of the concerned category name that I visit. The main resources page is being displayed properly as per my wish but not the categories section.

The sql table code is as shown below:

Table structure for table `resource_categories`
--

CREATE TABLE IF NOT EXISTS `resource_categories` (
  `id` bigint(15) NOT 

NULL auto_increment,
  `name` varchar(50) NOT NULL,
  `date` date NOT NULL,
  `time` time NOT NULL,
  PRIMARY KEY  (`id`)
) 

ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

--
-- Dumping data for table `resource_categories`
--

INSERT INTO 

`resource_categories` (`id`, `name`, `date`, `time`) VALUES
(2, 'Animation', '2008-08-23', '20:48:34'),
(3, 'Materials', 

'2008-08-23', '20:48:44'),
(4, 'Basics', '2008-08-23', '20:48:52'),
(5, 'Modeling', '2008-08-23', '20:49:01'),
(6, 

'Lighting', '2008-08-23', '20:50:11'),
(7, 'Reactor', '2008-08-23', '20:51:03'),
(8, 'Text Effects', '2008-08-23', 

'20:51:52'),
(9, 'Books', '2008-08-23', '20:52:08'),
(10, 'Freewares', '2008-08-23', '20:53:17'),
(11, ' Rendering', '2008-

08-23', '20:55:12'),
(12, 'Max scripts', '2008-08-23', '20:56:09'),
(13, 'Architectural', '2008-08-23', '21:08:26'),
(14, 

'Hardware', '2009-02-13', '04:55:18'),
(15, 'Other', '2009-02-13', '04:55:32'),
(16, 'Plug-in', '2009-02-13', '05:12:35'),
(17, 'Making of', '2009-02-13', '05:19:17'),
(18, 'Texturing', '2009-02-14', '06:07:24'),
(19, 'Particles', '2009-02-18', 

'04:50:27'),
(20, '3D Models', '2009-02-18', '06:47:13'),
(21, 'RealFlow', '2009-02-18', '07:44:51'),
(22, 'V-Ray', '2009-04

-22', '03:24:41'),
(23, 'Character Modeling', '2009-05-14', '08:57:31'),
(24, 'Special FX', '2009-05-19', '10:43:48'),
(25, 

'Workflow', '2009-06-06', '10:07:33'),
(26, 'Mental Ray', '2009-06-06', '22:38:22');

Can some one help me out with this.

With regards,
Srinivas.

Also doesnt define or retrieve $nr_tresources … so really all you’ve done is remove the sanitization and make a redundant if call.

i’m sure he’s capable of changing $query if he needed to see the error message lol .

except you havent defined or sanitized $cat_id … your error message uses $query but you defined the query string as $ResourceTitleSQL…$pageTitle is a redundant reference…

i wasn’t really trying to write the script up for you but was trying to give you an idea of how to accomplish what you need.

I personally would take out all the crap you did becuase its just absolutely heavy, and redundant for no reason.

The following script grabs the name of the page… by categoryID and assigns the $TITLE the name.



$ResourceTitleSQL = "SELECT * from resource_categories where id=$cat_id";
$ResourceResults = mysql_query($ResourceTitleSQL) or die("Sorry, Query Failed; SQL Query Passed $query");
$RessourceRetrieve = mysql_fetch_array($ResourceResults);

$PageTitle = $RessourceRetrieve['name'];

if(!$cat_id)
{
$SetTitle = "3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed";
} else {
$SetTitle = "3dsmax Resources $PageTitle";
} 


now in your title put

<title> <?= $SetTitle; ?> </title> 

that’s how i handle all my dynamic page titles. you can get more creative with that script too.

well i did it really quick just to get you started and give you and idea…

here’s the script i found 2 mistakes in it

$ResourceTitleSQL = “SELECT * from resource_categories where id=$cat_id”;
$ResourceResults = mysql_query($ResourceTitleSQL) or die(“Sorry, Query Failed; SQL Query Passed $query”);
$RessourceRetrieve = mysql_fetch_array($ResourceResults);

$PageTitle = $RessourceRetrieve[‘name’];

if(!$cat_id)
{
$SetTitle = “3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed”;
} else {
$SetTitle = “3dsmax Resources $PageTitle”;
}

Hello Shaydez,

Thanks for the quick reply, the problem is that the code is not working as an error message : “Sorry, Query Failed; SQL Query Passed” is displayed in the title of the web browser.

Waiting for your reply.

With regards,
Srinivas.

Hello Shaydez,

I tried it but it is still not working, as I am new in PHP I may be doing some mistakes which I am not able to understand. Can you please send the entire header php code that I posted on my first comment as shown below:


<head><title>
<?php 
require("includes/connectdb.php");
$nr_tresources=0;
 $count_resources=mysql_query("SELECT * from resources");
 while ($count_my_resources=mysql_fetch_array($count_resources))
 {
 $nr_tresources++;
 }


if (isset($_REQUEST['id_cat']))
   {
   $id_cat=$_REQUEST['id_cat'];
   } else {$id_cat=0;}
  if (is_numeric($id_cat)==false)
   {$id_cat=0;}
 $lista_cat=mysql_query("SELECT * from resource_categories where id=$id_cat");
 $rez_cat=mysql_fetch_array($lista_cat);
 if (empty($rez_cat))
  {
  $id_cat=0;
  }
  
 $get_categories=mysql_query("SELECT * from resource_categories order by name ASC");
  while ($show_categories=mysql_fetch_array($get_categories))
$name=$show_categories['name'];
 
  if ($id_cat!=$show_categories['id'])
	echo "3ds Max Resources&nbsp; >>&nbsp; $name";
  else
        echo "3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed ";

?></title>

Can you please make necessary changes and send me so that I can check it out. As I feel that I am making some silly mistake and I am not able to rectify it. I hope I am not bothering you. I once again appreciate for all the help that you are doing.

With regards,
Srinivas.

$ResourceTitleSQL = “SELECT * from resource_categories where id=$cat_id”;
$ResourceResults = mysql_query($BTitleSQL) or die(“Sorry, Query Failed; SQL Query Passed $query”);
$RessourceRetrieve = mysql_fetch_array($BTitleResults);

$PageTitle = $RessourceRetrieve[‘name’];

if(!$id_cat)
{
$SetTitle = “3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed”;
} else {
$SetTitle = “3dsmax Resources $PageTitle”;
}

Well, lets start with this:

$nr_tresources=0;
 $count_resources=mysql_query("SELECT * from resources");
 while ($count_my_resources=mysql_fetch_array($count_resources))
 {
 $nr_tresources++;
 }

… Ew. Try this instead.


 $count_resources= mysql_query("SELECT COUNT(*) from resources");
 list($nr_tresources) = mysql_fetch_row($count_resources);

:smiley:

Now. The title.
#1: Dont use _REQUEST. Please. Ever. If it’s going to be on the URL string, use _GET. If it’s going to be form-entered, use _POST. In either case, SANITIZE IT.
#2: you’re running more queries than you need to.


 $lista_cat=mysql_query("SELECT * from resource_categories where id=$id_cat"); //This right here should give you what you need.
 $rez_cat=mysql_fetch_array($lista_cat);
 if (empty($rez_cat)) //This should never be empty.
  {
  $id_cat=0;
  }
 $get_categories=mysql_query("SELECT * from resource_categories order by name ASC"); //Why are we querying every category? Is this used somewhere else?
  while ($show_categories=mysql_fetch_array($get_categories)) //If you're intending to start a code block here, you need a {
$name=$show_categories['name'];
 
  if ($id_cat!=$show_categories['id'])
	echo "3ds Max Resources&nbsp; >>&nbsp; $name";
  else
        echo "3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed "
//And an ending one here.

Now, let me explain a thing here. We’re using Category 0 as the ‘Default’ message (which I am setting to be “Free tutorial directory with over #RESNM# tutorials listed”). Therefore there’s no need to duplicate the default condition.

There should ALWAYS be a name returned (because we’ve made sure that if the category ID is invalid or empty, it’s set to 0). Therefore we dont need to test it again.

Replace that whole section with these 3 lines.


 $lista_cat=mysql_query("SELECT * from resource_categories where id=$id_cat");
 $rez_cat=mysql_fetch_array($lista_cat);
  echo "3ds Max Resources&nbsp; >>&nbsp; ".str_replace('#RESNM#',$nr_tresources,$rez_cat['name']);

working script here http://www.carlosja.com/help.php?cat_id=14




<? include "includes/functions.php"; 

$db=con_mysql(); 

$cat_id = $_GET['cat_id']; //for those who like to argue about security.

$ResourceTitleSQL = "SELECT * from resource_categories where id=$cat_id";
$ResourceResults = mysql_query($ResourceTitleSQL) or die("Sorry, Query Failed; SQL Query Passed $ResourceTitleSQL");
$RessourceRetrieve = mysql_fetch_array($ResourceResults);
 
$PageTitle = $RessourceRetrieve['name'];
 
if(!$cat_id)
{
$SetTitle = "3dsmax Resources | Free tutorial directory with over $nr_tresources tutorials listed";
} else {
$SetTitle = "3dsmax Resources $PageTitle";
}

?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?= $SetTitle; ?></title>
</head>

<body>

Let Grab all the categories <br /><br />

<?

$Get_Model_SQL = "select * from resource_categories";
$result=mysql_query($Get_Model_SQL);
while ($row=mysql_fetch_assoc($result))

{
	echo "- <a href='help.php?cat_id={$row['id']}'>{$row['name']}</a><br>";
}



?>

</body>
</html>



If you want to sanitize it you sanitize it. As i mentioned before, i’m just providing a starting point to get his page going.

I don’t know why the hell you’re getting on my case about my code.