【问题标题】:PHP: Alternate DisplayPHP:交替显示
【发布时间】:2013-06-12 21:33:44
【问题描述】:

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

}


}
?>

【问题讨论】:

  • 与其在 php 中解决这个问题,不如在文章容器上使用 css 的 text-overflow:ellispse。
  • 或者使用 2 个查询和一个单独使用 SQL 处理摘要的查询。
  • 感谢大家的两个好主意。欢呼

标签: php sql content-management-system


【解决方案1】:

问题是因为你正在用一个脚本和一个函数做两件不同的事情。您应该制作两个单独的脚本和两个函数。这样会更容易理解正在发生的事情。类似的东西:

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);

【讨论】:

    【解决方案2】:

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

    请考虑下面的代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多