tags:

views:

110

answers:

2

I have a site that has a main page and subpages for the articles comments.

What I want it is for the titles of the pages of the comments to be the same as the article's page title.

So I have this on the page file, the base for all the comments pages:

class Page {

    private $title;
    private $scripts;
    private $css;

    function __construct($title = 'Default Title')
    {
        $this->setTitle($title);
        $this->scripts = array(); 
        $this->css = array();
    }

    function setTitle($title)
    {
        $this->title = $title;
    }

    function getTitle()
    {
        return $this->title;
    }

function header()
    {
        if(!isset($_SESSION)) session_start();
        ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml"&gt;

    <head>
        <title><?php echo $title; ?></title>

And I have this in the comments page:

class CommentsPage extends Page {        
    function __construct($title = '')
    {
        $this->setTitle($title);
    }    
    function showAllComments($article_id, $param)
    {

        $article = Article::getById($article_id);

        if(!empty($article))
        {
            ?>
            <div class="news_item">
                <a href="/index.php?action=vote&param=<?php echo $article->getId(); ?>"><img alt="vote button" class="vote_button" height="10" src="assets/images/vote2.png" width="10" class="border_less" /></a>
                <h2 class="news_item_title"><b><a href = "<?php echo $article->getUrl(); ?>"><?php echo $article->getTitle(); ?></a></b></h2>              
            </div>

Where "$article->getTitle();" its what I want on the title.

Thx

+2  A: 

Last line of your Page class.

<title><?php echo $title; ?></title>

you should instead put:

<title><?php echo $this->title; ?></title>

This way when you do $this->setTitle($article->getTitle()); it will catch the title. You then have to render the page at the very end.

Erick
Thank you Erick, your answer fix my problem for most pages. Its just that if I put $this->setTitle($article->getTitle()) I get:Fatal error: Call to a member function getTitle() on a non-object in /home/mexautos/public_html/kiubbo/pages/commentspage.php on line 13Thx Regards Carlos
Slzr
+1  A: 

I'm not sure I fully understand your question, but you can try adding this after checking if the article is empty:

$this->setTitle($article->getTitle());

Edit: Sorry: I didn't see Erick's post. But our code should solve your problem. His suggestion of using $this->$title instead of $title is a good idea, but not required. It should work anyway as there are no instance variables of the same name.

St. John Johnson
John actually if he only uses "echo $title" it should display an empty title + throw a notice error. There is no $title variable in the head() methods and propreties visibility in PHP is not the same as C++/C#/Java you cannot just call the variable itself, you have to use the $this operator. :-)
Erick
Ahh, I just tested it. It seems that I've been bouncing between C# and PHP too much these days.
St. John Johnson