2013-06-12 187 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>';  } 


} 



} 


?>