Forum Class
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.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?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;
}
}
?>
/////////////////////////////////////////////////////////\\
// 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!
Gewijzigd op 01/01/1970 01:00:00 door Bas Matthee
Maar de kunst is om in objecten te denken.
Dus je hebt:
- posts (topics zijn de eerste post)
- forums (subforums kunnen gewone forums inheriten)
- gebruikers
- pm's
Zoals jij 't nu gedaan hebt is gewoon een verzameling van functies.
Dus ik zou eigenlijk de code moeten splitsen in meerdere classes. Voor ieder "onderdeel" een class?
Bas Matthee schreef op 31.03.2008 15:37:
Dus ik zou eigenlijk de code moeten splitsen in meerdere classes. Voor ieder "onderdeel" een class?
Warning: Ik ben geen kei in classes/OOP!
Volgens mij wel, want zo kunnen ze onderling aangesproken worden en zijn ze extendable (elk individueel) wat resulteert in meer flexibiliteit. Ik zou nog wat PHP 5 OOP tutorials/boekjes doorlezen zodat je de ware class-gedachte kunt bevatten en zo wat in elkaar kunt zetten.
Edit:
Misschien is het ook netjes dat je dit in PDO doet, dan hoef je niet alles aan te passen indien je dit naar een andere database zou migreren. Ik zou overigens ook geen HTML output in je klassen gebruiken maar gewoon returnen en werken met de Exception class. Dus gewoon zoiets:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
//in je method
if(!iets)
{
throw new Exception('bericht');
return false;
}
else
{
return true;
}
//bij het aanroepen:
try
{
$class->method();
}
catch(Exception $e)
{
$error->write($e->getMessage());
echo 'Er is een fout opgetreden <a href="link.php">inloggen</a>';
}
//even $error als errorhandling class genomen.
?>
//in je method
if(!iets)
{
throw new Exception('bericht');
return false;
}
else
{
return true;
}
//bij het aanroepen:
try
{
$class->method();
}
catch(Exception $e)
{
$error->write($e->getMessage());
echo 'Er is een fout opgetreden <a href="link.php">inloggen</a>';
}
//even $error als errorhandling class genomen.
?>
Gewijzigd op 01/01/1970 01:00:00 door Jurgen assaasas
En waar is dan de datum met tijd? Dit soort ellende heeft niks met datums en tijden in een database te maken, maar met een berg bugs en andere ellende. Ga bv. maar eens sorteren of rekenen met deze datums en tijden, dat gaat je niet lukken. Een datum sla je in MySQL op in een DATE, een tijd in een TIME en een datum met tijd in een DATETIME. Dit vereist dus een andere opmaak.
Ps. MySQL is vanzichzelf nog te stom om een broodje aap van een datum te onderscheiden, in PHP zul je dus wel moeten controleren of jouw datums ook echte datums zijn. 31 februari heeft MySQL geen enkele moeite mee... Gebruik de php-functie checkdate() om e.e.a. te controleren.
En aangezien throw alle code overslaat tot aan catch, heeft het ook geen zin om nog een return false-statement, of wat voor statement dan ook na throw te plaatsen.