Gå til innhold

Sammen skal vi lage: Gjestebok


Anbefalte innlegg

Tror ikke du trenger $this -> connection -> quote()

 

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = sprintf ( "INSERT INTO entries (author, email, website, message, ip) VALUES('%s', '%s', '%s', '%s', INET_ATON('%s'))",
							 $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 $mail;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar
Videoannonse
Annonse

Bruk heller prepared statements

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $message;
public $date;
public $ip;

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

?>

Endret av Josh Homme
Lenke til kommentar

<?php

(...)

class GuestbookEntry {
public $id;
public $author;
public $mail;
public $message;
public $date;
public $ip;

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

?>

 

Hva er meningen med __set?

Endret av LarsAndre
Lenke til kommentar

__set

 

La til $website;

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

La til GET fra MC.

 

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

Er det sånn det skal være?

private $memcacheConnection = new Memcache;

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar
Er det sånn det skal være?

private $memcacheConnection = new Memcache;

Det er mulig ja. Muligens den bør settes i __construct på samme måte som $connection, og at databaseconnection blir opprettet on demand, men det kan endres siden.

 

 

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

La til $mcConnected;

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Endret av Eirikkkkkk
Lenke til kommentar

La til returnering av objekter fra MC. Egentlig vil jeg bruke json_decode med stdObject og duck typing, og egentlig-egentlig vil jeg ikke bruke objekter, men den krangelen kan vi ta siden. :)

 

<?php

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

public function __construct(PDO $connection) {
	$this->connection = $connection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

Serialisering i PHP med serialize() og unserialize() er ofte tregt, så jeg pleier å bruke json_encode og json_decode (native fra PHP 5.2, og tilgjengelig som en C-kompilert PECL-modul for eldre versjoner) for serialisering. Problemet er bare at JSON bare forteller at noe er et objekt, ikke hvilken klasse objektet er, så du må enten manuelt caste det deserialiserte objektet tilbake til orginal klasse eller bare bruke det som stdClass og duck type når du bruker det. stdObject er PHPs basisklasse eller noe slikt.

 

<?php
header( 'Content-type: text/plain' );
class elg {}
$fisk = new elg();
$fisk->ulv = 'geit';

echo 'Original class name: ', get_class( $fisk ), "\n\n";
$jsonFisk = json_encode( $fisk );
echo 'JSON serialized: ', $jsonFisk, "\n";
$jsonFisk = json_decode( $jsonFisk );
echo 'JSON unserialized class name: ', get_class( $jsonFisk ), "\n\n";
$serFisk = serialize( $fisk );
echo 'serialize() serialized: ', $serFisk, "\n";
$serFisk = unserialize( $serFisk );
echo 'unserialize() unserialized class name: ', get_class( $serFisk ), "\n";
?>

Output :

Original class name: elg

JSON serialized: {"ulv":"geit"}
JSON unserialized class name: stdClass

serialize() serialized: O:3:"elg":1:{s:3:"ulv";s:4:"geit";}
unserialize() unserialized class name: elg

 

Les mer om JSON her:

http://en.wikipedia.org/wiki/JSON

Endret av JohndoeMAKT
Lenke til kommentar

Ulempen viser seg dersom du har et array med objekter av typen elg, hund, katt og rev og json_encode/json_dekoder arrayet. Bruker du serialize() som bevarer klassetypen kan du bruke get_class() for å skille typene fra hverandre, men det kan du ikke dersom alle er av type stdClass, så da må duck typing eller en jukseattributt med klassenavn i hvert objekt til.

Lenke til kommentar

Ok

 

 

<?php

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

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

La til blokk for innskriving i MC.

 

<?php

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

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {}
}

public function __destruct () {
	for ( $i = 0, $iMax = count ( $this -> entries ); $i < $iMax; $i++ ) {
		if ( empty ( $this -> entries[$i] -> id ) ) {
			$sql = "INSERT INTO entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

$entries = serialize($this->entries);

 

 

<?php

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

public function __construct(PDO $connection, $memcacheConnection) {
	$this->connection = $connection;
	$this->memcacheConnection = $memcacheConnection;
}

public function addEntry(GuestbookEntry $entry) {
	$this->entries[] = $entry;
}

public function fetchEntries(array $options = array()) {
	if ( $this->mcConnected ) {
		$entries = $this->memcacheConnection->get( 'gb-entries' );
		if ( $entries !== false ) {
			return unserialize( $entries );
		}
	}

	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
	if ( $this->mcConnected ) {
				$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 entries (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 $mail;
public $website;
public $message;
public $date;
public $ip;

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

?>

 

Lenke til kommentar

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...