我对 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 分开,但在需要的地方插入动态内容。这都在一个班级里。