如何才能将此输出改进为不包含那么多html?

时间:2012-06-24 01:53:31

标签: 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并使用了良好的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分开但是将动态内容插入到它需要的位置。这都属于一个类。

3 个答案:

答案 0 :(得分:2)

你可以在这里使用各种各样的东西,包括heredoc语法(直接相当于你拥有的东西):

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

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

另一种选择是使用输出缓冲和include内容脚本:

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

答案 1 :(得分:0)

尽可能退出PHP,除非您需要使用$content变量执行其他操作。我不确定你在何时/何地/如何调用你的功能,但是我在假设经过一些检查后才会假设

<?php 
  //Do any checks/validation etc here before you send something out to the server

?>
<html><head><!--HEADER STUFF HERE--></head>
<body>
<!-- anything you want before your content here -->

<?php
if($_GET['page'] == 'home'){
?>
<div class="igpPost-Divider"></div>
<header>
 ....
</footer>
</article>
<?php
}
  //If it is not the home page and it is a single article
    if($_GET['article']){ ?>
       <!-- HTML for article here --> 
    <?php
    }
?>
</body>
</html>

答案 2 :(得分:0)

由于我不了解您的具体需求,我不知道如何减少HTML,但是,您可以使用HEREDOC大大简化PHP代码。

HEREDOC(PHP Manual HEREDOC)是一种输出字符串并输出变量的方法,如同双引号一样。不同之处在于无需转义任何引号。

您的代码可以像这样使用HEREDOC:

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

    while($row = $stmt->fetch()){
        //Display the results formatted
        $dateTime = formatDateTime($row['datetime']) // functions cannot be used in HEREDOCS, so I replace it with the variable $dateTime declared here
        $content = <<<HERE
<article class="igpPost">
<div class="igpPost-Divider"></div>
<header>
    <h3><a href="index.php?article="$row['id']">$row['title']</h3>
    <div class="igpPost-AuthTime"><span>Posted By: </span><a href="#">$row['author']</a> <span>at $dateTime</span></div>
    <div class="igpPost-AuthTime"><span>Tags: </span>
    <div class="igpPost-Img"><img src="$row['imglocation']"/></div>
    </header>
    <p>$row['content']</p>
    <footer class="igpPost-Footer">
    <div class="igpPost-ReadMore">
    <h1><a href="index.php?article="$row['id']">Read More..</a></h1></div>
    </footer>
</article>
HERE;
echo $content;
}