PHP:替代显示

时间:2013-06-12 21:33:44

标签: php sql content-management-system

我希望有人可以帮助解决难题。我正在使用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;  

}


}
?>

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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>';      }


}



}


?>