php - 如何改进此输出以不包含太多 html?

标签 php

我对 php 相当陌生,想知道如何改进这段代码。我知道它并不完美,但鼓励任何建设性的批评,因为我正在努力在 PHP 方面做得更好。我所要求的只是,如果您确实以一种改进的方法回答,请稍微扩展一下答案,并让我知道为什么它更好,这样我就可以全面了解改进的情况。

public function displayArticle(){
    //Check to see if we are getting the home page
    if($_GET['page'] == 'home'){
        //Display the results formatted
        $content = "<article class=\"igpPost\">";
            $content .= "<div class=\"igpPost-Divider\"></div>";
            $content .= "<header>";
            $content .= "<h3><a href=\"#\">Ready Up – StarCraft 2, LoL, and Dota 2 pros head to DreamHack Summer</a></h3>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Posted By: </span><a href=\"#\">Cameron Lockhart</a> <span>at 07:44PM on June 15, 2012</span></div>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Tags: </span><a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a> ,<a href=\"#\">TAG HERE</a></div>";
            $content .= "<div class=\"igpPost-Img\"><img src=\"images/news/DreamHack-Summer-2012-logo.jpg\"/></div>";
            $content .= "</header>";
            $content .= "<p>Did last week’s MLG Spring Championship leave you thirsting for more eSports? Then DreamHack Summer 2012 has you covered. With well-attended tournaments for StarCraft 2, League of Legends, and Dota 2, DreamHack should keep you busy throughout the weekend and into the work-week. It starts tomorrow at 11 AM Eastern, and continues through Monday, with the StarCraft 2 Grand Final scheduled for 5:15 PM Eastern.</p>";
            $content .= "<footer class=\"igpPost-Footer\">";
            $content .= "<div class=\"igpPost-ReadMore\">";
            $content .= "<h1><a href=\"#\">Read More..</a></h1>";
            $content .= " </div>";
            $content .= "</footer>";
            $content .= "</article>";
    }
        //If it is not the home page and it is a single article
        if($_GET['article']){
            //Display the article formatted
        }
}

此外,这显然不是一个完整的脚本,但从它的角度来看,它对于 PHP 来说似乎太多了。我读了一些教程,我认为他们让我走上了错误的方向,因为我正确地使用了良好的 PHP。

更新:我仔细检查并尝试修改一些代码,以便提供更具描述性的概述:

$sql = "SELECT * FROM articles LIMIT $number";
        $stmt = $pdo->query($sql);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $stmt->fetch()){
            //Display the results formatted
            $content = "<article class=\"igpPost\">";
            $content .= "<div class=\"igpPost-Divider\"></div>";
            $content .= "<header>";
            $content .= "<h3><a href=\"index.php?article=" . $row['id'] ."\">" . $row['title'] . "</h3>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Posted By: </span><a href=\"#\">" . $row['author'] . "</a> <span>at " . formatDateTime($row['datetime']) . "</span></div>";
            $content .= "<div class=\"igpPost-AuthTime\"><span>Tags: </span>";
            $content .= "<div class=\"igpPost-Img\"><img src=\"" . $row['imglocation'] ."\"/></div>";
            $content .= "</header>";
            $content .= "<p>" . $row['content'] . "</p>";
            $content .= "<footer class=\"igpPost-Footer\">";
            $content .= "<div class=\"igpPost-ReadMore\">";
            $content .= "<h1><a href=\"index.php?article=" . $row['id'] ."\">Read More..</a></h1>";
            $content .= " </div>";
            $content .= "</footer>";
            $content .= "</article>";

            echo $content;
        }

这就是我想要的,我基本上试图将 html 与 PHP 分开,但将动态内容插入到需要的地方。这些都在一个类中。

最佳答案

您可以在这里使用各种东西,包括heredoc语法(与您拥有的直接等效):

            $content = <<<END
<article class="igpPost">
    <!-- ... -->
</article>
END;

(请注意,您不能缩进 END; 部分。)

另一种选择是使用输出缓冲并包含内容脚本:

ob_start();
include 'someViewFile.php';
$content = ob_get_contents();
ob_end_clean();

关于php - 如何改进此输出以不包含太多 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174431/

相关文章:

php - 从android应用程序上传多个图像文件到php服务器

php - 使用php编辑sql表数据

php - 如何从 php 脚本自动将用户添加到 ubuntu 中的 squirrelmail

javascript - 在加载下一页时保持网站事件

php - 为 PHP 选择正确的 MySQL 连接

php - 只有 true 语句的条件运算符

php - 获取一个月中的第一个或最后一个星期五

php - 如果我在 mysql 表中,谷歌如何找到我?

PHP 上传文件时爆炸换行符

php - 如何在 PHP 中将类常量用于类属性?