Gå til innhold

Sammen skal vi lage: Gjestebok


Anbefalte innlegg

<?php

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

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

public function addEntry(GuestbookEntry $entry) {

}

public function fetchEntries(array $options = array()) {
	$stmt = $this->connection->prepare($sql);
}
}

class GuestbookEntry {
public $author		= '';
public $message;
public $createdAt;
public $ip;

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

Lenke til kommentar
Videoannonse
Annonse

Fikset Guestbook::addEntry().

 

<?php

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

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

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

public function fetchEntries(array $options = array()) {

}
}

class GuestbookEntry {
public $author		= '';
public $message;
public $createdAt;
public $ip;

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

 

Edit @ Josh Homme: Gjør ting i riktig rekkefølge? $stmt = $this->connection->prepare($sql); - Hvor kommer $sql fra? Hva har koden med resten av klassen å gjøre?

Endret av Lokaltog
Lenke til kommentar

Ryddet opp.

<?php

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

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

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

public function fetchEntries(array $options = array()) {
	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
}
}

class GuestbookEntry {
public $author		= '';
public $message;
public $createdAt;
public $ip;

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

 

EDIT: glemte et semikolon.

Endret av LarsAndre
Lenke til kommentar
CREATE  TABLE `guestbook` ( 
  `id` INT NOT  NULL  AUTO_INCREMENT ,
   `name` VARCHAR( 255  )  NOT  NULL ,
   `email` VARCHAR( 255  )  NOT  NULL ,
   `website` VARCHAR( 255  )  NOT  NULL ,
   `comment` TEXT NOT  NULL ,
   `date` VARCHAR( 255  )  NOT  NULL ,
   `ip` VARCHAR( 255  )  NOT  NULL ,
PRIMARY  KEY (  `id`  ) ) ENGINE  = InnoDB

Lenke til kommentar

Endret Guestbook::fetchEntries().

<?php

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

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

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

public function fetchEntries(array $options = array()) {
	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
}
}

class GuestbookEntry {
public $author		= '';
public $message;
public $createdAt;
public $ip;

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

 

Langt fra noen ekspert i mysql, men her er et forslag til tabellen:

CREATE TABLE `guestbook` (
 `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
 `author` VARCHAR(100) NOT NULL,
 `email` VARCHAR(100) NOT NULL,
 `website` VARCHAR(100),
 `message` TEXT NOT NULL,
 `date` INT UNSIGNED NOT NULL,
 `ip` INT UNSIGNED NOT NULL,
 PRIMARY KEY (`id`)
)
CHARACTER SET utf8;

PS! Bruker INET_ATON og INET_NTOA for lagre IP i tabellen.

Endret av LarsAndre
Lenke til kommentar

Har alltid brukt å lagre UNIX timestamp i databasen, men når en bruker TIMESTAMP-datatypen blir det lagret i dette formatet "1970-01-01 00:00:01" i stedet for sekunder etter 1.1.1970. UNIX-timestampet, altså sekunder etter 1.1.1970, brukes for så vidt i alle datofunksjonene til PHP. Mulig jeg tar feil her, men er ganske sikker på dette.

Lenke til kommentar

Endret Guestbook::fetchEntries()

<?php

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

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

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

public function fetchEntries(array $options = array()) {
	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll();
}
}

class GuestbookEntry {
public $author		= '';
public $message;
public $createdAt;
public $ip;

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

Endret av Eirikkkkkk
Lenke til kommentar

Endret: La til PDO::FETCH_CLASS.

<?php

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

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

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

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

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

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

Endret av LarsAndre
Lenke til kommentar

For å svare deg, LarsAndre:

 

Du kan enkelt hente ut UNIX-timestampet fra et TIMESTAMP-felt i en MySQL-tabell ved å bruke funksjonen UNIX_TIMESTAMP. Du kan sette inn timestamps ved å bruke FROM_UNIXTIME(). Eksempler:

SELECT UNIX_TIMESTAMP(date) timestamp FROM table

INSERT INTO table SET date = FROM_UNIXTIME(4372548957)

 

Fordelen med å bruke et TIMESTAMP-felt eller et annet felt som er beregnet på tid er at det funker fint sammen med tids-funksjonene i MySQL.

Lenke til kommentar

La til $mail.

<?php

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

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

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

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

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

La til __destruct. (Ideen er å lagre entries som kan ha blitt lagt til, noe man f.eks. kan se ved at elementet ikke har en ID)

 

 

<?php

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

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

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

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

public function __destruct () {}

}

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
Langt fra noen ekspert i mysql, men her er et forslag til tabellen:

Jeg har ingen aning hva en guestbook er engang (er det slik som var vanlig på hjemmesider på nittitallet som bare er en lang rekke oppføringer listet kronologisk?), så jeg vet ikke hvordan strukturen bør være, jeg bare endret LarsAndres litt. Jeg regner med at registrerte brukere og søking utgår?

 

CREATE DATABASE `guestbook` DEFAULT CHARACTER SET utf8 COLLATE utf8_swedish_ci;

CREATE TABLE `entries` (
 `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
 `author` VARCHAR(100) NOT NULL,
 `email` VARCHAR(100) NOT NULL,
 `website` VARCHAR(100),
 `message` TEXT NOT NULL,
 `created` datetime NOT NULL default '0000-00-00 00:00:00',
 `deleted` datetime NOT NULL default '0000-00-00 00:00:00',
 `ip` INT UNSIGNED NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB;

Lenke til kommentar

Jeg tar meg friheten til å poste en gang til siden forige var SQL og bare en endring av eksisterende kode. :)

 

Siden denne galmannsgjesteboken objektifiserer hver oppføring vil jeg legge til litt cache slik at serveren ikke brekker etter et par titalls requests i sekundet.

 

 

<?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()) {
	$sql = "SELECT * FROM guestbook;";
	$stmt = $this->connection->prepare($sql);
	$stmt->execute();
	$this->entries = $stmt->fetchAll(PDO::FETCH_CLASS, 'GuestbookEntry');
}

public function __destruct () {}

}

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

La til en for-løkke i __destruct.

 

<?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()) {
	$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; $i < count ( $this->entries ); $i++ ) {}
}

}

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

Forbedret for-løkken i __destruct.

 

<?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()) {
	$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++ ) {}
}

}

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

La til en linje under for-løkken i __destruct.

 

<?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()) {
	$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 ) ) {}
	}
}

}

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 Jonas
Lenke til kommentar

La til en MC-relatert test.

 

<?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 ) ) {}
	}
}

}

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

Har egentlig alltid brukt min egen databaseklasse, er dette korrekt utførelse med PDO?

 

 

<?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 -> connection -> quote ( $this -> entries[$i] -> author ),
							 $this -> connection -> quote ( $this -> entries[$i] -> email ),
							 $this -> connection -> quote ( $this -> entries[$i] -> website ),
							 $this -> connection -> quote ( $this -> entries[$i] -> message ),
							 $this -> connection -> quote ( $_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

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