function getAllPosts($orderBy = "desc")
{
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
$settings = new settings();
if ($settings::db_driver == "xml") {
// XML
$posts = array();
$blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "blogPosts.xml");
foreach ($blogPosts as $blogPost) {
$posts[] = array("title" => $blogPost->title, "date" => $blogPost->date, "author" => $blogPost->author, "contents" => $blogPost->contents);
}
// Sort the results by date either desc or asc.
if (strtolower($orderBy) == "desc") {
usort($posts, function ($a, $b) {
return strtotime($b["date"]) - strtotime($a["date"]);
});
} else {
usort($posts, function ($a, $b) {
return strtotime($a["date"]) - strtotime($b["date"]);
});
}
} else {
// PDO
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
$common = new common();
$dbh = $common->pdoOpen();
$sql = "SELECT * FROM " . $settings::db_prefix . "blogPosts ORDER BY date " . $orderBy;
$sth = $dbh->prepare($sql);
$sth->execute();
$posts = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth = NULL;
$dbh = NULL;
return $posts;
}
return $posts;
}