[英]How can i write this statement better without repeating myself, it calls for values using simple html dom
[英]Can I execute SELECT queries without repeating myself?
是否可以從第二種方法執行查詢而無需重復以上所有內容? 還是應該將所有內容合並為一個函數?
public function selectAd()
{
$query = "
SELECT category,title,content,date_added from ads
";
$stmt = $this->getConnection()->prepare($query);
$stmt->execute();
$count = $stmt->rowCount();
if ($count > 0) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
echo '<div class="result">';
echo "Title: <b>".$row['title']."</b>";
echo "<h6>Category:".ucfirst($row['category'])."</h6>"
.$row['content']."<br>".
"Date: ".$row['date_added'];
echo '</div>';
echo "<br>";
}
} else {
echo "<u><br>Nothing to show yet</u>";
}
}
public function randomAds()
{
$query = "
SELECT * FROM ads ORDER BY RAND() LIMIT 5;
";
// steps from method above
}
考慮一下:
public function WriteOutAds($doShowThemAll)
{
$query = 'SELECT category, title,content, date_added FROM ads';
if ($doShowThemAll==false)
$query .= ' ORDER BY RAND() LIMIT 5'; //Display only 5 random ones
//perform your query
//do emit the result
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.