Page 1 of 1
[SOLVED] dynamic title help needed
Posted: Mon Aug 30, 2004 4:23 am
by no_memories
OK, some code first.
Code: Select all
class Title {
var $name = '';
function name ($newname = NULL) {
if (! is_null ($newname)) {
$this->name = $newname;
}
return $this->name;
}
}
$t1 = new Title;
$t1->name('Page Desc 1');
$t2 = new Title;
$t2->name('Page Desc 2');
switch($_GET['id']){
case 'h':
include 'http://www.mysite.com/includes/home.php';
break;
case 'd':
include 'http://www.mysite.com/includes/desinations.php';
break;
default:
include 'http://www.mysite.com/includes/home.php';
break;
}
<title>MySite ::
</title>
As you can see this is a class that will bridge the variables and the case of the switch. As of now, this only prints the one variable. What I am trying to do is connect the variable to the corresponding case, then print it to the <title>. I think I need a looping array with another function, but as of right now, I'm not sure how to go about that. Anyone have any ideas or solutions to this?
Thanks in Advance
Posted: Mon Aug 30, 2004 7:20 am
by jakobdoppler
mhhh I am not exactly sure what you mean or what exactly these 2 pages do. But if you include the second page in the first you could do the following.
Use a second GET parameter besides 'id' to transmit the name of the object (e.g. ?obj = t1)
and than in the include file call it like this,
Code: Select all
<?php
printf("%s\n",$_GET['obj']->name);
?>
because you already created your object with
Code: Select all
<?php
$t1 = new Title;
$t1->name('Page Desc 1');
?>
*hth* _yak
Posted: Mon Aug 30, 2004 11:01 am
by no_memories
ok,
the first page wlill be part of an initial index.php that will call a page from the switch with an inserted linked - something like :<a href="index.pnp?id=h">
The object created should be tied to the case that is going to load so that the <title></title> will print the object that is tied to the loaded 'case' value of the switch. This way, as the dynamic page is loaded via the index.php, a corresponding title will load with it, without using loading a basic title that is related to the $_get id alone.
what needs to be done is associate the two together then use the
Code: Select all
<?php
printf("%s\n",$_GET['obj']->name);
?>
that loads a nice tidy title with the correct page.
So for instance:
$t1 should = the first case value in the swtich or 'h'
$t2 should = the second case value of 'd', Understand?
Posted: Mon Aug 30, 2004 11:13 am
by feyd
what about:
Code: Select all
$page_title = new Title;
switch($_GET['id']){
case 'h':
$page_title->name('Page 1 Desc');
include 'includes/home.php';
break;
case 'd':
$page_title->name('Page 2 Desc');
include 'includes/desinations.php';
break;
default:
$page_title->name('Default Page');
include 'includes/home.php';
break;
}
Posted: Mon Aug 30, 2004 12:50 pm
by no_memories
I'll play around this afternoon and see what happens. But this has issue has been a major problem for many folks who want a dynamic title that are not using a data base. I have a modular way of dealing with this issue right now, but I would rather combine this into once php file for redundancy purposes and potentially help others in the same boat.
feyd: I tried a variation of that, but didn't use the $_GET['obj']. I'll try your suggestion and see how it works
Thanks
Posted: Mon Aug 30, 2004 1:32 pm
by markl999
You could always just have a map of page names => titles instead of using a class.
Code: Select all
$titlemap = array(
'home.php' => 'The Home Page',
'destinations.php' => 'Destinations'
);
Posted: Mon Aug 30, 2004 1:44 pm
by no_memories
Nope, no go.
The problem, I least I believe, is that the object being called into the title from the switch function needs to be looped back to the class function of "name". Like I said, this is a common problem for many folks not using a database setup. I could use the ($_GET['id']) to load the id of the called page, but this is limited for a nice title. Then I have to arrange my links so they jive with the title, not acceptable.
What about running the class and variables before the header.php and connecting the switch function to the variables? The variables and title descriptions would run before the page loads.
I'm at a loss here and very new to writing my own code.
Code: Select all
<?php
include("includes/header.php"); /* Problem 1, the header with the title loads first so even if I put the class function before the include/header.php, the switch function is still run after the title loads */
class Title {
var $name = '';
function name ($newname = NULL) {
if (! is_null ($newname)) {
$this->name = $newname;
}
return $this->name;
}
}
$t1 = new Title;
$t2 = new Title;
switch($_GET['id']){
case 'h':
$t1->name('Page Desc 1'); /* This must somehow be looped back to the <title> perhaps a for trick would work? */
include 'http://www.mysite.com/includes/home.php';
break;
case 'd':
$t2->name('Page Desc 2');
include 'http://www.mysite.com/includes/desinations.php';
break;
default:
include 'http://www.mysite.com/includes/home.php';
break;
}
include("includes/footer.php");
?>
Posted: Mon Aug 30, 2004 1:49 pm
by markl999
I can't see what the problem is exactly, using that class seems over complicated and the class serves no useful purpose. I'd just keep it simple and map page name => page titles, for example:
Code: Select all
$titlemap = array(
'home.php' => 'The Home Page',
'destinations.php' => 'Destinations'
);
$title = (isset($titlemap[basename($_SERVER['PHP_SELF'])])) ? $titlemap[basename($_SERVER['PHP_SELF'])] : 'Default Title';
echo $title; //use this in your pages <title</title> tags
?>
Posted: Mon Aug 30, 2004 2:22 pm
by no_memories
still no go, it only loads the default title. This is why the array should be tied into the switch, if the array doesn't know what to do, it does nothing at all besides loading the default title. Like I said, this is a common problem with smaller websites. I'm using a modular workaround for this problem right now, but it separates the titles from the header.php making another unnecessary file.
index.php
Code: Select all
<?php
echo("<?xml version="1.0" encoding="iso-8859-1"?>\n");
include("includes/upper_header.php");
include("includes/titles.php");
include("includes/lower_header.php");
include("includes/body.php");
include("includes/footer.php");
?>
Right now this is how I have it set up, I use two switch function in the titles.php and the body.php This way loads nice pretty titles but is overly complicated and wouldn't be usefull for a larger site.
I've search the web, read books on the issues, but no one has come up with a solution to this seamingly easy problem as of yet.
Posted: Mon Aug 30, 2004 2:31 pm
by markl999
Well, i'll admit i'm still confused and can't see a problem. The usual way this 'sort' of thing is done is to set up everything you need before including the header, you appear to be loading the header then trying to set the title. Why not just do something like:
Code: Select all
switch($_GET['id']){
case 'h':
$title = 'Title1';
$incpage = 'home';
break;
case 'd':
$title = 'Title2';
$incpage = 'desinations';
break;
default:
$title = 'Whatever';
$incpage = 'home';
break;
}
include_once 'includes/header.php';
include_once 'includes/'.$incpage.'.php';
include_once 'includes/footer.php';
Anyway, if i've still got the wrong end of the stick, i'll give up now

Posted: Mon Aug 30, 2004 2:43 pm
by no_memories
BRAVO! Mark, you have done it. I thank you very much kind sir.

It's always something so simple it boggles the mind. (Usually)
Posted: Mon Aug 30, 2004 3:59 pm
by no_memories
One last note:
You can also add other variables to this setup. such as a dynamic meta tag information.
Code: Select all
<?php
switch($_GET['id']){
case 'h':
$meta = 'meta stuff here';
$title = 'Title1';
$incpage = 'home';
break;
case 'd':
$meta = 'meta stuff here';
$title = 'Title2';
$incpage = 'desinations';
break;
default:
$meta = 'meta stuff here';
$title = 'Whatever';
$incpage = 'home';
break;
}
include_once 'includes/header.php';
include_once 'includes/'.$incpage.'.php';
include_once 'includes/footer.php';
?>
Works beautifully. Thanks again. I'll post this on other forums as well, this is a very common problem with smaller to medium non-database driven sites.