2013-06-12 138 views
1

我希望有人能帮助解决难题。我使用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

代码:本文容器上ellispse。 – Orangepill

+1

或者使用2个查询和一个仅使用SQL处理抽象的查询。 – nickhar

+0

谢谢你们两个好主意。干杯 – user2480085

回答

0

问题是因为你正在做一个脚本和一个功能的两个不同的事情。你应该制作两个独立的脚本和两个功能。这样可以更容易理解发生的事情。喜欢的东西:

class MyCMS 
{ 
    function get_content($id) 
    { 

     $id = mysql_real_escape_string($id); 
     $sql = "SELECT * FROM content WHERE blog_id = '$id'"; 
     $res = mysql_query($sql) or die(mysql_error()); 
     if(mysql_num_rows($res) !=0) { 
      //display the content of the article 
     } else { 
      //ooops that article does not exist, link to index.php 
     } 
    } 


    function getLinks() 
    { 
     $sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3"; 
     if(mysql_num_rows($res) !=0){ 
      while($row = mysql_fetch_assoc($res)) { 
       //see : href="article.php ... ! 
       echo '<div id="roundedbox"><h2><a href="article.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>"; 
      } 

     } 
    } 
} 

,然后两页

的index.php

//getLinks 

article.php

$id = $_GET['id']; 
//get_content($id); 
0

你为什么不验证码像下面一样?我想写的代码有一个很好的结构,你现在正在做什么。

请考虑相反的PHP解决这个可能是更好的使用CSS的文本溢出以下

<?php 

class MYCMS{ 


function get_content($id=""){ 


if(is_numeric($id) && ($id!=""){ 

//no validation required because id can only be a number and could not be blank // 

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



if(mysql_num_rows($sql)< 1){ 

//show message if no article is found // 

echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
      echo $return; 


} 




else{ 

//If article is found // 


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


} 

// 




} 

// If id is not numeric or blank // 

else{ $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';  } 


} 



} 


?>