Jako, ze moja propozycja przeszla niezauwazona oto jej implementacja:
CREATE TABLE `test_comments` (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`docId` mediumint(8) NOT NULL,
`date` int(11) NOT NULL,
`topic` varchar(30) NOT NULL,
`author` varchar(20) NOT NULL,
`authorId` mediumint(8) NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`),
INDEX ( `docId` )
)
<?php
// Przyklad dzialania /////////////////////////////
define( 'TABLE_PREFIX', 'test_' );
//Dodawanie komentarza
$comment = new Comment();
$comment->docId = 1;
$comment->topic = 'Temat1';
$comment->author = 'Seth';
$comment->authorId = 42;
$comment->content = 'Tresc komentarza 1';
$comment->Insert();
// Tworzenie instancji listy komentarzy z docId = 1
$comments = new Comments( 1 );
print \"---- Lista komentarzy:rn\"; // Wyswietlanie tekstowej reprezentacji listy komentarzy
print $comments->ToString(); print \"---- koniec listyrn\";
//Dodawanie komentarza
$comment = new Comment();
$comment->docId = 1;
$comment->topic = 'Temat2';
$comment->author = 'Seth';
$comment->authorId = 42;
$comment->content = 'Tresc komentarza';
$comment->Insert();
// Odswiezenie listy komentarzy
$comments->Refresh();
print \"---- Lista komentarzy:rn\"; // Wyswietlanie tekstowej reprezentacji listy komentarzy
print $comments->ToString(); print \"---- koniec listyrn\";
$comment = null;
if ( $comment =& $comments->GetById( 1 ) )
{
$comment->content = 'zmieniona tresc';
$comment->Update();
}
print \"---- Lista komentarzy:rn\"; // Wyswietlanie tekstowej reprezentacji listy komentarzy
print $comments->ToString(); print \"---- koniec listyrn\";
// Usuwanie wszyskich komentarzy z danego docId
// $comments->RemoveAll();
// Koniec przykladu ///////////////////////////////
class Comment
{
var
$id = null,
$docId = null,
$date = null,
$topic = '',
$author = '',
$authorId = 0,
$content = '';
function Comment()
{
}
function Remove()
{
$sql = \"DELETE FROM \".TABLE_PREFIX.\"comments WHERE id = \".$this->id;
{
return true;
}
else
{
return false;
}
}
function Update()
{
$sql = \"UPDATE \".TABLE_PREFIX.\"comments SET
docId = \".$this->docId.\",
date = \".$this->date.\", topic = '\".$this->topic.\"',
author = '\".$this->author.\"',
authorId = \".$this->authorId.\",
content = '\".$this->content.\"'
WHERE id = \".$this->id;
{
return true;
}
else
{
return false;
}
}
function Insert()
{
$sql = \"INSERT INTO \".TABLE_PREFIX.\"comments
( docId
, date, topic
, author
, authorId
, content
) VALUES (
\".$this->docId.\",
\".$this->date.\",
'\".$this->topic.\"',
'\".$this->author.\"',
\".$this->authorId.\",
'\".$this->content.\"' )\";
{
return true;
}
else
{
return false;
}
}
function ToString()
{
$toString = '';
$varsList = get_object_vars( $this );
foreach( $varsList as $key => $val )
{
$toString .= $val.\"rn\";
}
return $toString;
}
}
class Comments
{
var
$docId = 0,
function Comments( $docId )
{
$this->docId = $docId;
$sql = \"SELECT * FROM \".TABLE_PREFIX.\"comments WHERE docId = \".$this->docId.\" ORDER BY date DESC\";
{
{
$this->list[$row['id']] = new Comment();
$this->list[$row['id']]->id = $row['id'];
$this->list[$row['id']]->docId = $row['docId'];
$this->list[$row['id']]->date = $row['date'];
$this->list[$row['id']]->topic = $row['topic'];
$this->list[$row['id']]->author = $row['author'];
$this->list[$row['id']]->authorId = $row['authorId'];
$this->list[$row['id']]->content = $row['content'];
}
return true;
}
else
{
return false;
}
}
function Refresh()
{
unset( $this->list ); // update return $this->Comments( $this->docId );
}
function RemoveAll()
{
foreach ( $this->list as $key => $obj )
{
$obj->Remove();
unset( $this->list[$key] ); }
}
function &GetById( $id )
{
if ( isset( $this->list[$id] ) ) {
return $this->list[$id];
}
else
{
return false;
}
}
function ToString()
{
$toString = '';
foreach ( $this->list as $key => $obj )
{
$toString .= $obj->ToString().\"rn\";
}
return $toString;
}
}
?>
Wynik powyzszego przykladu:
Cytat
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
Tresc komentarza 1
---- koniec listy
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
Tresc komentarza 1
2
1
1073938210
Temat2
Seth
42
Tresc komentarza
---- koniec listy
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
zmieniona tresc
2
1
1073938210
Temat2
Seth
42
Tresc komentarza
---- koniec listy
Edit: mozna by jeszcze pokusic sie o min. dodawanie komenatarza przez przekazanie obiektu komentarza do listy i tam dopiero wykonanie Insert'a oraz uaktualnienie lsty bez potrzeby Refresha.