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!
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;
}
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?
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
$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;
}
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
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.
<?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");
?>
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:
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.
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.
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:
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.