【问题标题】:Get title to match link title获取标题以匹配链接标题
【发布时间】:2009-03-09 22:53:18
【问题描述】:

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

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->setTitle($title);”对于这样的东西: $article->getTitle();但我尝试了不同的方法,只是显示错误。

我想我不够清楚:页面顶部的标题要更改并与该页面的新闻标题相同(这是网站:kiubbo.com)谢谢

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这个怎么样:

    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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我有点困惑。您只是在寻找属性 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);
              }
          }
      }
      

      【讨论】:

      • 感谢您的回答。我想要的是标题页与新闻文章标题相同。感谢您对标题功能的更正,有人为我做了这个,我正在尝试修复它。
      • 我想我不够清楚:页面顶部的标题要更改并与该页面的新闻标题相同(这是网站:kiubbo.com)谢谢跨度>
      • 如果我理解正确,这将涉及更改 HTML 页面的标题。您提供的代码 sn-p 中不包含该 HTML - 所以目前我无法告诉您在哪里更改它。
      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 2017-08-04
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多