Gå til innhold

Sammen skal vi lage: Gjestebok


Anbefalte innlegg

La til prepare i editEntry

 

<?php

class Guestbook {
private $numEntriesPerPage = 10;
private $connection;
private $entries;
private $memcacheConnection;
private $mcConnected;

public function __construct(PDO $connection, $memcacheConnection) {
	if ($memcacheConnection instanceof Memcache) {
		$this->memcacheConnection = $memcacheConnection;
		$this->mcConnected = true;
	}
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
	if ( $this->mcConnected ) {
		$this->memcacheConnection->delete('gb-entries');
	}
}

public function deleteEntry( $id ) {
	if ( is_int( $id ) && $id > 0 ) {
		$sql = "UPDATE guestbook SET deleted = NOW() WHERE id=?";
		$stmt = $this->connection->prepare($sql);
		$stmt->execute(array($id));

		if ( $this->mcConnected ) {
			$this->memcacheConnection->delete( 'gb-entries' );
		}
	}
}

public function editEntry($id) {
	if(is_int($id) && $id > 0) {
		$sql = "UPDATE guestbook SET message=? WHERE id=?";
		$stmt = $this->connection->prepare($sql);
		if ( $this->mcConnected ) {
			$this->memcacheConnection->delete( 'gb-entries' );
		}
	}
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}
	$sql = "SELECT /*SQL_CACHE*/ id, author, email, website, message, created FROM guestbook WHERE !deleted ORDER BY created DESC;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
		$this->memcacheConnection->set( 'gb-entries', serialize( $this->entries ) );
	}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO guestbook (author, email, website, message, ip) VALUES (?,?,?,?,INET_ATON(?))";
			$stmt = $this->connection->prepare($sql);
			$stmt->execute(array(
			   $this->entries[$i]->author,
			   $this->entries[$i]->email,
			   $this->entries[$i]->website,
			   $this->entries[$i]->message,
			   $_SERVER['REMOTE_ADDR']
			));
		}
	}
}

}

class GuestbookEntry {
public $id;
public $author;
public $email;
public $website;
public $message;
public $date;
public $ip;

public function __set($key, $value) {
	if (isset($this->$key)) {
		$this->$key = $value;
	}
}
}

 

Lenke til kommentar
  • 2 uker senere...
Videoannonse
Annonse
  • 5 måneder senere...

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...