<?
include('db.class.php');

class 
comments{
    private 
$comments;
    private 
$db;
    
    function 
__construct(db $db){
        
$this->db $db;
    }
    
    public function 
load_comments(){
        
$comments = array();
        
$comms $this->db->sql_select('comments');
        if (
mysql_num_rows($comms) > 0){
            while(
$com mysql_fetch_assoc($comms)){
                
$comments[] = $com;
            }
        }
        
$this->comments $comments;
    }
    
    public function 
add_comment($name$text){
        
$this->db->raw_query('INSERT INTO comments (name, time, text) VALUES (\''.$name.'\', '.time().', \''.$text.'\')');
    }
    
    public function 
display_comment_form(){
        if (isset(
$_POST['submit']) && $_POST['submit'] == 'Add Comment'){
            
$messages = array();
            if (empty(
$_POST['name']))
                
$messages[] = 'You need to enter a name';
            if (empty(
$_POST['comment']) || $_POST['comment'] == 'Comment')
                
$messages[] = 'You need to enter a comment';
            if (empty(
$messages)){
                
self::add_comment($_POST['name'], $_POST['comment']);
            } else {
                print(
'<ul><li>'.implode('</li><li>'$messages).'</li></ul>');
            }
        }
        
?>
        <form method="post" action="<?=$_SERVER['PHP_SELF']?>">
        Name: <input type="text" name="name" /><br />
        <textarea name="comment">Comment</textarea><br />
        <input type="submit" name="submit" value="Add Comment" />
        </form>
        <?
    
}
    
    public function 
display_comments(){
        if (!
is_array($this->comments))
            
self::load_comments();
        if (!empty(
$this->comments)){
            foreach(
$this->comments as $comment){
                print(
'<div style="border:thin solid #000000; width:200px;"><div style="border:thin solid #000000; width:200px;">By: '.$comment['name'].' on '.date('m-d-Y'$comment['time']).'</div><div style="border:thin solid #999999; width:200px;">Comment:<br />'.$comment['text'].'</div></div>');
            }
        } else {
            print(
'there are no comments yet.');
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?
$comment 
= new comments($db);
$comment->display_comment_form();
$comment->display_comments();
$db->print_log();
?>
</body>
</html>