Newer
Older
class Blog_Entry_Table {
//Establish connection to database with PDO
private $db;
public function __construct ( $db ) {
$this->db = $db;
}
//Insert new entries
public function saveEntry ( $title, $text ) {
$entrySQL = "INSERT INTO blog_entry (title, text) VALUES (?, ?)";
$entryStatement = $this->db->prepare( $entrySQL );
$formData = array( $title, $text );
try {
$entryStatement->execute( $formData );
// echo "Entry successfully added. Go to All Entries to view.";
} catch (Exception $e){
$msg = "<p>You tried to run this sql: $entrySQL<p>
<p>Exception: $e</p>";
trigger_error($msg);
}
}
//Get all entries
public function getAllEntries () {
$entrySQL = "SELECT id, title, SUBSTRING(text, 1, 140) AS intro FROM blog_entry";
//SELECT 'columnName' AS 'aliasName'
//this is SQL Aliases: temporarily rename a table or column heading
//so that it's more readable.
$statement = $this->makeStatement($sql);
return $statement;
}
public function getEntry($id) {
$sql = "SELECT id, title, text, date_created FROM blog_entry WHERE entry_id = ?";
$data = $array($id);
$statement = $this->makeStatement($sql, $data);
$model = $statement->fetchObject();
return $model;
}
private function makeStatement($sql, $data=NULL) {
//the default $sql will be used if nothing else is passed to $data
$statement = $this->db->prepare( $entrySQL );
try{
$exceptionMsg = "<p>You tried to run this sql: $entrySQL<p>