1

我希望有人可以帮助解决难题。我正在使用 substr 来提供文章摘要。现在的问题是,当您单击链接查看整篇文章时,您仍然会看到 substr 版本。现在这显然是由于代码的方式而发生的。但是任何人都可以提供替代方案,因此当您单击链接时,您可以看到完整的文章?

<?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

2 回答 2

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

        }
    }
}

然后是两页

索引.php

//getLinks

文章.php

$id = $_GET['id'];
//get_content($id);
于 2013-06-12T22:59:53.453 回答
0

你为什么不验证你的代码如下?我正在编写的代码具有您当前正在执行的良好结构。

请考虑下面的代码

<?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>';      }


}



}


?>
于 2013-06-13T02:40:30.373 回答