获取标题以匹配链接标题

时间:2009-03-09 22:53:19

标签: php mysql

我正在开发类似Digg的网站,我希望评论页面的标题与链接标题相匹配,这里是评论文件代码:

class CommentsPage extends Page {

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

    function header()
    {
        parent::header();
    }

    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>
                <span class="news_item_url">(<?php echo $article->getUrlDomain(); ?>)</span>
                <div class="news_item_info"><?php $points = $article->getPoints(); echo $points > 1 ? "$points points" : "$points point"; ?> by <a href="/index.php?action=user&param=<?php echo $article->getUsername(); ?>"><?php echo $article->getUsername(); ?></a> <?php echo $article->getElapsedDateTime(); ?></div>
                <p class="news_item_content"><?php echo $article->getDescription(); ?></p>
            </div>
            <?php
            $this->showSubmitForm($article);
        }

我知道我必须更改“$ this-&gt; setTitle($ title);”对于这样的东西:$ article-&gt; getTitle();但我尝试过不同的东西,只是表现出错误。

我认为我不够清楚:页面顶部的标题要更改,并且与该页面的新闻项目标题相同(这是网站:kiubbo.com)Thx

2 个答案:

答案 0 :(得分:2)

这个怎么样:

class CommentsPage extends Page
{
    private $article;

    function __construct($article_id)
    {
        $this->article = Article::getById($article_id);
        if(!empty($article))
        {
            $this->setTitle($article->getTitle());
        }
        else
        {
            //Throw an exception
        }
    }

    function header()
    {
        parent::header();
    }

    function showAllComments($param)
    {
        if(!empty($this->article))
        {
        ?>
            <div class="news_item">
                <a href="/index.php?action=vote&param=<?php echo $this->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 $this->article->getUrl(); ?>"><?php echo $this->article->getTitle(); ?></a></b></h2>
                <span class="news_item_url">(<?php echo $this->article->getUrlDomain(); ?>)</span>
                <div class="news_item_info"><?php $points = $this->article->getPoints(); echo $points > 1 ? "$points points" : "$points point"; ?> by <a href="/index.php?action=user&param=<?php echo $this->article->getUsername(); ?>"><?php echo $this->article->getUsername(); ?></a> <?php echo $this->article->getElapsedDateTime(); ?></div>
                <p class="news_item_content"><?php echo $this->article->getDescription(); ?></p>
            </div>
        <?php
        $this->showSubmitForm($this->article);
    }
}

答案 1 :(得分:1)

我有点困惑。你只是在寻找一个属性getter / setter吗?

<?php
class CommentsPage extends Page {

    protected $title;

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

    //  This function does nothing, by the way
    function header()
    {
        parent::header();
    }

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

    public function getTitle()
    {
        return $this->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 $this->getTitle(); ?></a></b></h2>
                <span class="news_item_url">(<?php echo $article->getUrlDomain(); ?>)</span>
                <div class="news_item_info"><?php $points = $article->getPoints(); echo $points > 1 ? "$points points" : "$points point"; ?> by <a href="/index.php?action=user&param=<?php echo $article->getUsername(); ?>"><?php echo $article->getUsername(); ?></a> <?php echo $article->getElapsedDateTime(); ?></div>
                <p class="news_item_content"><?php echo $article->getDescription(); ?></p>
            </div>
            <?php
            $this->showSubmitForm($article);
        }
    }
}