Example #1
0
/**
 * Print Latest newsItems 
 *	
 * @param int $offset 
 * @param int $numberOfPosts
 * @return
 */
function retrievePostsWithTag($tag, $offset, $numberOfPosts)
{
    include_once 'blogpost.inc.php';
    include_once 'event.inc.php';
    include_once 'db.inc.php';
    //Open a database connection and store it
    $db = new PDO(DB_INFO, DB_USER, DB_PASS);
    //initialize array to store retrieved ids
    $results = array();
    //search blogposts
    $sql = "SELECT id, sortdate, created\n\t\t\tFROM blogposts \n\t\t\tWHERE tags LIKE '%" . $tag . "%'";
    //LIMIT ".$numberOfPosts . " OFFSET " . $offset;
    $stmt = $db->prepare($sql);
    $stmt->execute();
    //add results to results array
    while ($row = $stmt->fetch()) {
        array_push($results, array("id" => $row['id'], "created" => $row['created'], "sortdate" => $row['sortdate'], "posttype" => "blogpost"));
        flush();
    }
    $stmt->closeCursor();
    //search events
    $sql = "SELECT id, sortdate, created\n\t\t\tFROM events \n\t\t\tWHERE tags LIKE '%" . $tag . "%'";
    //LIMIT ".$numberOfPosts . " OFFSET " . $offset;
    $stmt = $db->prepare($sql);
    $stmt->execute();
    //add results to results array
    while ($row = $stmt->fetch()) {
        array_push($results, array("id" => $row['id'], "created" => $row['created'], "sortdate" => $row['sortdate'], "posttype" => "event"));
        flush();
    }
    $stmt->closeCursor();
    //if we found results, print them
    if (count($results) > 0) {
        //sort the results array by sortdate and created
        foreach ($results as $key => $row) {
            // Obtain a list of columns
            $sortdate[$key] = $row['sortdate'];
            $created[$key] = $row['created'];
        }
        array_multisort($sortdate, SORT_DESC, $created, SORT_DESC, $results);
        //echo posts
        foreach ($results as $result) {
            switch ($result['posttype']) {
                case 'blogpost':
                    $blogpost = new Blogpost(FALSE);
                    $blogpost->updateParameters($result['id']);
                    echo $blogpost->formatNewspage();
                    flush();
                    break;
                case 'event':
                    $event = new Event(FALSE);
                    $event->updateParameters($result['id']);
                    echo $event->formatEventpage();
                    flush();
                    break;
            }
        }
    } else {
        echo "<h4>Sorry, we found no news articles or events with this tag</h4><br>\n\t\t<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><!-- sorry, I was lazy -->";
    }
}