-1

嘿伙计们希望有人可以帮助解决这个问题。

我正在使用 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;  

}


}
?>
4

3 回答 3

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;
    }
}
于 2013-06-12T22:53:32.243 回答
0

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

<?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 和 excerpt 标志传递给它,如下所示:

get_content(123, TRUE); //for excerpt
get_content(123); //for full post
于 2013-06-12T22:47:19.803 回答
0

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

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

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

于 2013-06-12T22:47:38.897 回答