Ik zeg hallo,
Ik ben momenteel bezig met het maken van een (simpel) forum. Het is niet de bedoeling dat er straks een forum staat, maar dat ik straks het principe van classes onder de knie heb. (Dit is dus mijn eerste class).
Ik ben niet helemaal (helemaal niet :) ) thuis in het gebruik van dingen als Private, Public, en Protected e.d. Graag zou ik dan ook even wat feedback hebben over mijn gebruik van functies e.d.
Niet naar de functionaliteit van het forum kijken, het is slechts bijzaak, persoonlijke ontwikkeling staat voorop.
<?php
/////////////////////////////////////////////////////////\\
// Forum class //
// Author: Bas Matthee \\
// Date: March 2008 //
// Version: 1.0 \\
///////////////////////////////////////////////////////////
class Forum {
// Configuration variables
var $CatTable = 'cathegories';
var $ThreadTable = 'threads';
var $PostTable = 'posts';
var $Username = 'root';
var $Password = '';
var $Database = 'forum';
var $Server = 'localhost';
var $ForumName = 'PHP Forum';
// Class variables (DO NOT CHANGE!)
var $Member;
var $Connection;
var $Result;
var $QueryID;
var $NumRows;
var $Width;
var $Postcount;
function __construct () {
$this->Connect(); // Automatically connects to the database
$this->Member = 1; // Sets the Author of the posts (now static value, will be the userID from session value when logged in!
$this->Width = '100%';
$this->Postcount = $this->GetUserPosts($this->Member);
}
function GetUserPosts ($UserID) {
$Query = "SELECT COUNT(id) AS postcount FROM posts WHERE author = {$UserID}";
$Count = mysql_fetch_object(mysql_query($Query));
return $Count->postcount;
}
function GetUsername ($UserID) {
$Query = "SELECT username FROM users WHERE id = {$UserID} LIMIT 1";
$Result_Set = mysql_query($Query);
$Result = mysql_fetch_object($Result_Set);
return $Result->username;
}
function Connect () {
if (!$connection) {
// Connect to Database
$this->Connection = mysql_connect($this->Server,$this->Username,$this->Password);
if (!$this->Connection) {
die("Database connection failed: " . mysql_error());
}
// Select Database
$db_select = mysql_select_db($this->Database,$this->Connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
function Execute ($Query) {
$this->Result = mysql_query($Query);
//$this->QueryID = mysql_result_id($this->Result);
echo mysql_num_rows($this->Result);
}
function AddCat ($CatName, $CatDesc) {
echo $Query = "INSERT INTO {$this->CatTable} (catname, description )
VALUES ('{$CatName}', '{$CatDesc}' )";
$this->Execute($Query);
}
function AddThread ($ThreadName, $Message, $CatID) {
$Query = "INSERT INTO {$this->ThreadTable} (title, message, author, date, cat_id)
VALUES ('{$ThreadName}', '{$Message}', '{$this->Member}', '" . date('d-m-y', time()) . "', {$CatID} )";
$this->Execute($Query);
}
function AddPost ($PostName, $Message, $ThreadID) {
$PostName = addslashes($PostName);
$Message = addslashes($Message);
$Query = "INSERT INTO {$this->PostTable} (title, message, author, date, thread_id)
VALUES ('{$PostName}', '{$Message}', '{$this->Member}', '" . date('d-m-y', time()) . "', {$ThreadID} )";
$this->Execute($Query);
}
function PostReply ($PostName, $Message, $MessageID) {
$PostName = addslashes($PostName);
$Message = addslashes($Message);
$Query = "INSERT INTO {$this->PostTable} (title, message, author, date, message_id)
VALUES ('{$PostName}', '{$Message}', '{$this->Member}', '" . date('d-m-y', time()) . "', {$MessageID} )";
$this->Execute($Query);
}
function SelectThreads ($cat_id) {
$Query = "SELECT * FROM threads WHERE cat_id = {$cat_id}";
$this->Execute($Query);
}
function SelectPosts ($thread_id) {
$Query = "SELECT * FROM posts WHERE thread_id = {$thread_id}";
return $this->Execute($Query);
}
function Count () {
echo $this->NumRows;
}
function OutputCats(){
$Query = "SELECT * FROM cathegories";
$this->Result = mysql_query($Query);
$html = $this->ForumHeader('Cats');
while ($Data = mysql_fetch_object($this->Result)) {
$html .= '<tr><td colspan="2"><a href="threads.php?cat_id='. $Data->id .'">'. $Data->catname .'</a></td></tr>';
$html .= '<tr><td colspan="2">'. $Data->description .'</td></tr>';
}
$html .= $this->ForumFooter('Cats');
echo $html;
}
function OutputThreads($cat_id){
$Query = "SELECT * FROM threads WHERE cat_id = {$cat_id}";
$this->Result = mysql_query($Query);
$html = $this->ForumHeader('Threads');
while ($Data = mysql_fetch_object($this->Result)) {
$Query = "SELECT COUNT(id) AS threads FROM posts WHERE thread_id = {$Data->id}";
$Count = mysql_fetch_object(mysql_query($Query));
$Author = $this->GetUsername($Data->author);
$html .= '<tr><td><a href="posts.php?thread_id='. $Data->id .'">'. $Data->title .'</a></td><td rowspan="2">'. $Count->threads .' Threads</td></tr>';
$html .= '<tr><td>'. $Data->message .'</td></tr>';
}
$html .= $this->ForumFooter('Threads');
echo $html;
}
function OutputPosts($thread_id){
$Query = "SELECT * FROM posts WHERE thread_id = {$thread_id}";
$this->Result = mysql_query($Query);
$html = $this->ForumHeader('Posts');
while ($Data = mysql_fetch_object($this->Result)) {
$Query = "SELECT COUNT(id) AS replies FROM posts WHERE message_id = {$Data->id}";
$Count = mysql_fetch_object(mysql_query($Query));
$Author = $this->GetUsername($Data->author);
$html .= '<tr><td><a href="viewtopic.php?post_id='.$Data->id.'">'. $Data->title .'</a></td><td rowspan="2">'. $Author .'<br>'. $Count->replies .' Replies</td></tr>';
$html .= '<tr><td>By: '. $Author .' @ '. $Data->date .'</td></tr>';
}
$html .= $this->ForumFooter('Posts',$thread_id);
echo $html;
}
function OutputMessage($post_id){
$Query = "SELECT * FROM posts WHERE id = {$post_id} LIMIT 1";
$this->Result = mysql_query($Query);
$html = $this->ForumHeader('Message');
$Data = mysql_fetch_object($this->Result);
$Author = $this->GetUsername($Data->author);
$html .= '<tr><td height="20px;">'. $Data->title .'</a> - '. $Data->date .'</td><td rowspan="2">'.
$Author . '<br />Admin<br /><br />'.$this->Postcount.' Posts<br /></td></tr>';
$html .= '<tr><td>'. $Data->message . '</td></tr>';
$Query = "SELECT * FROM posts WHERE message_id = {$post_id}";
$this->Result = mysql_query($Query);
if ($this->Result) {
while ($Data = mysql_fetch_object($this->Result)) {
$html .= '<tr><td height="20px;">'. $Data->title .'</a> - '. $Data->date .'</td><td rowspan="2">'.
$Author . '<br />Admin<br /><br />'.$this->Postcount.' Posts<br /></td></tr>';
$html .= '<tr><td>'. $Data->message . '</td></tr>';
}
}
$html .= $this->ForumFooter('Messages',$post_id);
echo $html;
}
function ForumHeader ($Section) {
$html = '<table border="1" width="'.$this->Width.'">';
$html .= '<tr><td width="85%"><a href="index.php"><h3>'.$this->ForumName.'</h3></a></td><td>'.date('D d, M Y',time()).'</td></tr>';
if ($Section != 'Cats' && $Section != 'Threads') {
$html .= '<tr><td width="85%">Decription</td><td>Author</td></tr>';
} else {
$html .= '<tr><td colspan="2">Decription</td></tr>';
}
return $html;
}
function ForumFooter($Section,$id=0) {
//if ($Section != 'Cats') {
// $html = '<tr><td colspan="2"><a href="'.$_SERVER['HTTP_REFERER'].'"><< Go Back</a></td></tr>';
//} else
if ($Section == 'Posts') {
$html = '<tr><td><a href="'.$_SERVER['HTTP_REFERER'].'"><< Go Back</a></td><td><a href="AddPost.php?thread_id='.$id.'">Start new discussion</a></td></tr>';
} elseif ($Section == 'Threads') {
$html = '<tr><td><a href="index.php"><< Go Back</a></td></tr>';
} elseif ($Section == 'Messages') {
$html = '<tr><td><a href="'.$_SERVER['HTTP_REFERER'].'"><< Go Back</a></td><td><a href="PostReply.php?message_id='.$id.'">Post Reply</a></td></tr>';
}
$html .= '</table>';
return $html;
}
}
?>
Nogmaals het is nog niet af, maar ik wil weten of ik goed bezig ben en zo nee, gelieve feedback te plaatsen. DANK!
Ik zeg tot ziens!
969 views