2013-06-12 59 views
-1

嘿,大家希望有人可以帮助。PHP:查询显示链接

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

} 


} 
?> 

回答

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和摘录标志如下:

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

嘿感谢回到我身边。我应该在这里把这些价值传递给我的班级吗? (isset($ _ GET ['id'])): echo $ obj-> get_content($ _ GET ['id']);其他: echo $ obj-> get_content(); endif; ?> – user2480085

+0

yes,像'$ obj-> get_content($ _ GET ['id'],TRUE)'摘录,'$ obj-> get_content($ _ GET ['id'])'完整发帖 –

+0

或if通过URL查询字符串'$ obj-> get_content($ _ GET ['id'],$ _GET ['excerpt'])'传递标志,并使URL查询字符串像'http://mydomain.com/myscript。 php?id = 123&excerpt = TRUE'摘录,'http://mydomain.com/myscript.php?id = 123&excerpt = FALSE' for full post –

0

我不认为你想用PHP来做到这一点,因为当你这样做是用PHP,你必须刷新整个页面。

我建议你为此使用Javascript,比如jQuery库slideToggle

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

1

通常情况下,您会将文章封装到数据类(别名“Model”,请参阅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; 
    } 
}