Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:交替显示_Php_Sql_Content Management System - Fatal编程技术网

PHP:交替显示

PHP:交替显示,php,sql,content-management-system,Php,Sql,Content Management System,我希望有人能帮我解决这个难题。我正在使用substr提供文章摘要。现在的问题是,当您单击链接查看整篇文章时,仍然会看到substr版本。这显然是因为代码的方式。但是有人能帮你找到一个替代方法吗?这样当你点击链接时,你就可以看到完整的文章了 <?php class MyCMS { function get_content($id = "") { if ($id != ""): $id = mysql_real_escape_string($id);

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

}


}
?>

问题在于,您使用一个脚本和一个函数做两件不同的事情。您应该创建两个单独的脚本和两个函数。这样就更容易理解发生了什么。比如:

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

        }
    }
}
article.php

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

为什么不像下面那样验证代码??我要写的代码有一个很好的结构,你目前正在做的事情

请考虑下面的代码

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


}



}


?>

与其在php中解决这个问题,不如在文章容器上使用css的文本溢出:ellispse。或者使用两个查询和一个单独使用SQL处理摘要。谢谢大家两个好主意。干杯