【问题标题】:PHP: Query to display a linkPHP:查询以显示链接
【发布时间】:2013-06-12 22:40:53
【问题描述】:

大家好,希望有人能帮忙解决这个问题。

我正在使用 substr 来提供文章摘要。现在的问题是,当您单击链接查看整篇文章时,您仍然会看到 substr 版本。现在这显然是由于代码的方式而发生的。

我需要进行另一个查询,如果有人单击链接,然后显示没有 substr 的完整文章。我不知道该怎么做,有人可以帮忙吗?虽然我正在学习我的 PHP 知识是相当有限的。

<?php
class MyCMS 
{
function get_content($id = "")
{
    if ($id != ""):
        $id = mysql_real_escape_string($id);
        $sql = "SELECT * FROM content WHERE blog_id = '$id'";
        $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';

    else:
        $sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
    endif;

$res = mysql_query($sql) or die(mysql_error());
    if(mysql_num_rows($res) !=0):
        while($row = mysql_fetch_assoc($res))
        {
            echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'] . '">' . $row['title'] . ' </a></h2>';
            echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
            echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
        }
        else:
            echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
            echo $return;
        endif;  

}


}
?>

【问题讨论】:

    标签: php sql content-management-system


    【解决方案1】:

    通常您会将文章封装到数据类中(别名为“模型”,请参阅 MVC 架构)。该模型保存了文章的所有数据,并具有返回文章摘要或长版本的方法。要确定客户希望看到的版本,请将另一个参数传递给您的 URL。

    class Article {
        protected $text;
        protected $title;
    
        public __construct ($title, $text) {
            $this->title = $title;
            $this->text = $text;
        }        
    
        /** 
         * Returns the short excerpt of the article
         */
        public getShortAbstract () {
            // your substr() function
        }        
    
        /**
         * Returns the full article
         */
        public getText () {
            return $this->text;
        }
    }
    

    【讨论】:

      【解决方案2】:

      向函数添加另一个参数以确定它返回的是完整文章还是摘录:

      <?php
      class MyCMS 
      {
      function get_content($id = "", $excerpt = FALSE)
      {
          if ($id != ""):
              $id = mysql_real_escape_string($id);
              $sql = "SELECT * FROM content WHERE blog_id = '$id'";
              $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';
      
          else:
              $sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
          endif;
      
      $res = mysql_query($sql) or die(mysql_error());
          if(mysql_num_rows($res) !=0):
              while($row = mysql_fetch_assoc($res))
              {
                  echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'] . '">' . $row['title'] . ' </a></h2>';
                  echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
                  if ($excerpt):
                      echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
                  else:
                      echo '<p>' . $row['body'] . '</p>';
      
              }
              else:
                  echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
                  echo $return;
              endif;  
      
      }
      
      
      }
      ?>
      

      然后,当您调用 get_content() 函数时,将 id 和摘录标志传递给它,如下所示:

      get_content(123, TRUE); //for excerpt
      get_content(123); //for full post
      

      【讨论】:

      • 嘿,感谢您回复我。我应该在我调用类的地方传递这些值吗? get_content($_GET['id']);否则:回声 $obj->get_content();万一; ?>
      • 是的,例如 $obj-&gt;get_content($_GET['id'], TRUE) 摘录,$obj-&gt;get_content($_GET['id']) 完整帖子
      • 或者如果通过 URL 查询字符串传递标志,$obj-&gt;get_content($_GET['id'], $_GET['excerpt']) 并使您的 URL 查询字符串像 http://mydomain.com/myscript.php?id=123&amp;excerpt=TRUE 用于摘录,http://mydomain.com/myscript.php?id=123&amp;excerpt=FALSE 用于完整帖子
      【解决方案3】:

      我不认为你想用 PHP 做这个,因为当你用 PHP 做这个时你必须刷新整个页面。

      我建议您为此使用 Javascript,例如 jQuery 库 slideToggle

      使用此方法,Google 仍会将所有内容编入索引。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 2018-08-18
        相关资源
        最近更新 更多