Hallo zusammen,
ich bin zwar leider etwas zu spät und konnte aus zeitlichen Gründen auch nicht alles fertig stellen. Trotzdem möchte ich euch meinen Ansatz ebenfalls zeigen. Bin gespannt was ihr dazu sagt. Absolut daneben oder i.O?
und
Das Wichtigste ist hier eigentlich schon zu sehen.
Leider habe ich keine 2. Datenbank umgesetzt. Was ich mir bei der Geschichte so überlegt habe sieht man aber denk ich auch so
Soll ich das ganze Drumherum noch als zip-File anhängen?
Verwendungsbeispiel (greift auf ein paar andere kleine Klassen zu):
Weitere Datenbanken wären in der Form GB_Data_XML usw. geplant. Bei einem Datenbankwechsel wäre somit nur die Zeile
anzupassen.
ich bin zwar leider etwas zu spät und konnte aus zeitlichen Gründen auch nicht alles fertig stellen. Trotzdem möchte ich euch meinen Ansatz ebenfalls zeigen. Bin gespannt was ihr dazu sagt. Absolut daneben oder i.O?

PHP-Code:
abstract class GB_Data {
private $ugc;
public function __construct()
{
$this->ugc = new UGC();
}
abstract protected function _get_comments();
abstract protected function _write_comment($comment);
public function write_comment($comment = false)
{
if($comment === false || !is_array($comment))
{
throw new GB_Exception("Kein Kommentar oder im falschen Format");
}
$comment = $this->ugc->standardize_new_lines_deep($comment);
$comment['date'] = $this->_timestamp_to_mysqldatetime();
$this->_write_comment($comment);
}
public function get_all_comments()
{
$comments = $this->_get_comments();
if(!$comments)
{
return false;
}
$comments = $this->ugc->prepare_for_output_array($comments);
$comments = $this->_format_dates($comments);
return $comments;
}
private function _format_dates($c)
{
foreach($c as $key => $comment)
{
$c[$key]['date'] = $this->_mysqldatetime_to_date($comment['date']);
}
return $c;
}
private function _timestamp_to_mysqldatetime($timestamp = "", $datetime = true)
{
if(empty($timestamp) || !is_numeric($timestamp))
{
$timestamp = time();
}
return ($datetime) ? date("Y-m-d H:i:s", $timestamp) : date("Y-m-d", $timestamp);
}
private function _mysqldatetime_to_date($datetime = "", $format = " \a\m d.m.Y \u\m H:i")
{
return date($format, $this->_mysqldatetime_to_timestamp($datetime));
}
private function _mysqldatetime_to_timestamp($datetime = "")
{
$l = strlen($datetime);
if(!($l == 10 || $l == 19))
return 0;
$date = $datetime;
$hours = 0;
$minutes = 0;
$seconds = 0;
if($l == 19)
{
list($date, $time) = explode(" ", $datetime);
list($hours, $minutes, $seconds) = explode(":", $time);
}
list($year, $month, $day) = explode("-", $date);
return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
}
PHP-Code:
class GB_Data_MySQL extends GB_Data
{
private $db = array();
public function __construct($db_parameter = false)
{
if(!$db_parameter || !is_array($db_parameter))
{
throw new GB_Exception("Verbindungsdaten für MySQL müssen definiert werden");
}
if( !$this->db = mysql_connect($db_parameter['host'], $db_parameter['user'], $db_parameter['pwd']))
{
throw new GB_Exception("Konnte keine Verbindung zur Datenbank herstellen");
}
if(!mysql_select_db($db_parameter['db_name'], $this->db))
{
throw new GB_Exception("Konnte Datenbank nicht auswählen");
}
parent::__construct();
}
protected function _get_comments()
{
$query = "SELECT * FROM entries ORDER BY id DESC";
if(!$result = mysql_query($query, $this->db))
{
throw new GB_Exception("Konnte Kommentare nicht auslesen.");
}
$comments = array();
if(mysql_num_rows($result) == 0)
{
return false;
}
while ($c = mysql_fetch_array($result, MYSQL_ASSOC))
{
$comments[] = array('name' => $c['name'], 'text' => $c['text'], 'date' => $c['date']);
}
return $comments;
}
protected function _write_comment($comment)
{
$name = mysql_real_escape_string($comment['name']);
$text = mysql_real_escape_string($comment['text']);
$time = mysql_real_escape_string($comment['date']);
$query = "INSERT INTO entries (name, text, date)
VALUES ('" . $name . "', '" . $text . "', '" . $time . "')";
if(!mysql_query($query, $this->db))
{
throw new GB_Exception("Konnte Kommentar nicht hinzufügen");
}
return true;
}
}
Leider habe ich keine 2. Datenbank umgesetzt. Was ich mir bei der Geschichte so überlegt habe sieht man aber denk ich auch so

Soll ich das ganze Drumherum noch als zip-File anhängen?
Verwendungsbeispiel (greift auf ein paar andere kleine Klassen zu):
PHP-Code:
$start = new StartUp();
$start->init();
$input = array();
$error_main = '';
$is_valid = false;
$database = array( 'host' => 'localhost',
'user' => 'root',
'pwd' => '',
'db_name' => 'php_de'
);
try
{
$data = new GB_Data_MySQL($database);
}
catch(GB_Exception $e)
{
echo $e->show_message();
exit;
}
if(isset($_POST['submit']))
{
$ugc = new UGC();
try
{
$validation = new Validate();
$validation->set_rule('e_name', 'Name', 'trim|required');
$validation->set_rule('e_text', 'Nachricht', 'trim|required');
$is_valid = $validation->Run();
}
catch (GB_Exception $e)
{
$error_main .= $e->show_message();
}
if( !$is_valid )
{
$input = $_POST;
$input = $ugc->entities($input);
$validation->set_error_delimiter('<li>', '</li>');
$input['validation'] = '<ul class="error">' . $validation->get_errors() .'</ul>';
}
else
{
$insert['name']= $_POST['e_name'];
$insert['text'] = $_POST['e_text'];
$insert = $ugc->standardize_new_lines_deep($insert);
try
{
$data->write_comment($insert);
}
catch(GB_Exception $e)
{
$error_main .= $e->show_message();
}
$input['validation'] = '<ul class="success"><li>Eintrag gespeichert.</li></ul>';
}
}
$input['comments'] = $data->get_all_comments();
try
{
$input['error_main'] = $error_main;
$view = new View('template', $input);
$view->render();
$view->show();
}
catch (GB_Exception $e)
{
echo $e->show_message();
}
Code:
$data = new GB_Data_MySQL($database);
Kommentar