I am fairly new to php and was wondering how I would go about improving this code. I know its not perfect but any constructive criticism is encouraged as I am trying to better myself in PHP. Please all I ask is that if you do answer with a way to improve it you expand upon the answer a little and let me know why it is better so I can get the full over view of the improvement.
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
}
}
Also this is obviously not a completed script but looking at it it looks like to much for PHP. I read some tutorials and I think they sent me in the wrong direction with being correct and using good PHP.
Update: I went through and tried to revise some of the code so that it gave a more descriptive overview:
$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;
}
This is what im going for, im basically trying to separate the html from the PHP but insert the dynamic content in the places it needs to be. This is all within a class.
You can use a variety of things here, including heredoc syntax (the direct equivalent of what you have):
(Note that you can’t indent the
END;part.)Another option is to use output buffering and
includea content script: