 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-18-2007, 07:21 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Having an issue with the forum..
Yea, here we go again ;)
I've come a good bit. I already have a user system that I've built before, that I'm planning on implanting, so I'm mainly concentrating on getting the forum functions right now.
For example, categories, forums in different categories, and threads.
Now I have a question.
My database structure looks like this:
Code:
forums =>
forum_id tinynt(10)
forum_name varchar(25)
forum_desc varchar(255)
forum_parent tinyint(10)
forum_order tinyint(10)
forum_category tinyint(1)
threads =>
thread_id int(10)
thread_name varchar(25)
thread_forumId tinyint(10)
thread_author varchar(25)
thread_sticky tinyint(10)
It's working quite well right now.
I think you understand the basics about the forum table.
If a forum is a category, the row is the value 1, if it's a simple forum it's a 0. If it's a regular forum, the "parent" row is set with the forum_id of the category forum that it's supposed to be under.
The thread table is quite easy aswell.
My question concerns the POSTS in the threads.
Should I store them inside the thread table(?? :S), or should I create a seperate table for the posts, like this?
Code:
posts =>
post_id
post_threadId
post_author
post_time
post_content
??
|
|
|
|
12-18-2007, 07:45 PM
|
#2 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
|
In my personal opinion, the answer to your question is yes. Forum software like Invision Power Board and phpBB (any maybe even vBulliten) do this method. I also plan on doing it this way when I start to build my own forum.
Hope that helps!
__________________
Not quite a n00b...
|
|
|
|
12-19-2007, 07:51 AM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Okey, thank you! Do you have any suggestions about the forum and thread db structure? Does it look good? Can it be done better?
|
|
|
|
12-19-2007, 12:05 PM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
I'm confused!
You have tinyint(10) but the highest tinyint you can get is an unsigned tinyint which is 225 digits max. :\
|
|
|
|
12-19-2007, 12:37 PM
|
#5 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
In extension to bdm's comment, you should never use tinyint for an ID unless you know there will never be more than 255 (unsigned) IDs. It's also important to understand unsigned, by making a number "unsigned" you prohibit negative values and effectively double the amount of valid IDs that you can store in a single field.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
12-19-2007, 12:39 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Karl
In extension to bdm's comment, you should never use tinyint for an ID unless you know there will never be more than 255 (unsigned) IDs. It's also important to understand unsigned, by making a number "unsigned" you prohibit negative values and effectively double the amount of valid IDs that you can store in a single field.
|
Okey, so how should it be then?  
|
|
|
|
12-19-2007, 12:46 PM
|
#7 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
Make any ID fields unsigned, for example, consider your current thread_forumId field, which is of type tinyint.
As it currently stands, that field can hold 255 values: -128 to 127. However, by making that specific field unsigned, we change that range to 0-255, thus giving us another 128 possible IDs. The same applies for most (if not all?) numeric data types.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
|
|
|
|
12-19-2007, 01:12 PM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
So in your case, I'd say it's safe to use unsigned tinyint.
Now that I think about, I have yet to see a forums have anywhere close to 255 forums+categories.
Just another decision you must make.
Also, in your threads table, you should replace thread_author with author_id or something along those lines. Where author_id will link to your users table.
threads
...
author_id (FK)
|
|
users
|
|
user_id (PK)
...
We do this to normalize your data. So for example, you may wish to include a feature where the administrator can change someones username. What would have happened if you hard coded their username to each thread/forums post they made? What are the advantages of linking the threads table to the users table?
Also, shouldn't the thread_sticky column from the threads table be a bit or even tinyint(1)?
|
|
|
|
12-19-2007, 01:59 PM
|
#9 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
What's unsigned? :S
|
|
|
|
12-19-2007, 02:01 PM
|
#10 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Quote:
Originally Posted by Tanax
What's unsigned? :S
|
Taken from Karl: However, by making that specific field unsigned, we change that range to 0-255, thus giving us another 128 possible IDs. The same applies for most (if not all?) numeric data types.
It's also important to understand unsigned, by making a number "unsigned" you prohibit negative values and effectively double the amount of valid IDs that you can store in a single field.
|
|
|
|
12-19-2007, 02:59 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Oh, you edited your post above my last post :D
Anyways, thanks for the tip about the author, much better, so big thanks :D
And yes, sticky could be only 1, since it's only 1 number.. :)
And the "unsigned", I change that in phpmyadmin huh?? 
|
|
|
|
12-19-2007, 05:58 PM
|
#12 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Quote:
Originally Posted by Tanax
And the "unsigned", I change that in phpmyadmin huh?? 
|
Yep, it's the 'Attributes' field.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
12-19-2007, 08:15 PM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by xenon
Yep, it's the 'Attributes' field.
|
Yea hehe, I found it :) Thanks 
|
|
|
|
12-19-2007, 08:23 PM
|
#14 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Mhm, now I'm having another problem...
php Code:
public function newPost ($details, $thread) { $sql = sprintf(" INSERT INTO `%s` SET `%s` = NOW(), `%s` = '%s', `%s` = '%s', `%s` = '%s'", $this-> db-> table[ 'posts'], $this-> db-> col[ 'post_time'], $this-> db-> col[ 'post_content'], $details[ 'content'], $this-> db-> col[ 'post_author'], $details[ 'author'], $this-> db-> col[ 'post_thread'], $thread); $query = $this-> db-> query($sql); if($query) { return true; } return false; }
And then this:
php Code:
$t = $_GET[ 't']; $newpost = array(); $newpost[ 'author'] = $_POST[ 'author']; $newpost[ 'content'] = $_POST[ 'post']; if($tanaxia[ 'forum']-> newPost($newpost, $t)) { echo 'Reply posted!'; } else { echo 'Couldn\'t post reply..'; }
It inserts the data, and it echoes out that "reply posted".
I check the thread, but the post isn't there!
So I check the database, and sure enough, the post EXISTS in the posts table. But the "post_thread" row value is set to 0 for some reason.
So it's.. yea, no idea what's wrong :S
|
|
|
|
12-20-2007, 11:47 AM
|
#15 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Anyone? =//
|
|
|
|
12-22-2007, 11:22 PM
|
#16 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
Quote:
Originally Posted by Tanax
Anyone? =//
|
I'd have to see the code that pulls the post from the DB
|
|
|
|
12-22-2007, 11:40 PM
|
#17 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
Like dschreck said, we'd need to see all of the code that's being used to insert a new post.
|
|
|
|
12-23-2007, 12:35 PM
|
#18 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
You already see all the code? :S
newpost.php
php Code:
<?php/** * @author Tanax * @copyright 2007 */ include('includes/config.php'); $t = $_GET[ 't']; if(! $_POST[ 'submit'] ) { ?> <form action= "newpost.php" method= "POST"> Author:<br /> <input type= "text" name= "author" /><br /> Post:<br /> <input type= "text" name= "post" /><br /> <input type= "submit" value= "Post" name= "submit" /> </form> <?php } else { $newpost = array(); $newpost[ 'author'] = $_POST[ 'author']; $newpost[ 'content'] = $_POST[ 'post']; if($tanaxia[ 'forum']-> newPost($newpost, $t)) { echo 'Reply posted!'; } else { echo 'Couldn\'t post reply..'; } }?>
forum.php (the class)
php Code:
public function newPost ($details, $thread) { $sql = sprintf(" INSERT INTO `%s` SET `%s` = NOW(), `%s` = '%s', `%s` = '%s', `%s` = '%s'", $this-> db-> table[ 'posts'], $this-> db-> col[ 'post_time'], $this-> db-> col[ 'post_content'], $details[ 'content'], $this-> db-> col[ 'post_author'], $details[ 'author'], $this-> db-> col[ 'post_thread'], $thread); $query = $this-> db-> query($sql); if($query) { return true; } return false; }
showthread.php
php Code:
<?php/** * @author Tanax * @copyright 2007 */ include('includes/config.php'); $t = $_GET[ 't']; if(! isset($t)) { echo 'Please specify a valid thread'; } else { $thread = $tanaxia[ 'forum']-> getThreadInfo($t); $forum = $tanaxia[ 'forum']-> getForumInfo($thread[ 'thread_forum'] ); $category = $tanaxia[ 'forum']-> getForumInfo($forum[ 'forum_parent'] ); $posts = $tanaxia[ 'forum']-> getPosts($thread[ 'thread_id'] ); echo '<a href="index.php">Index</a> -> <a href="forumdisplay.php?f='. $category[ 'forum_id']. '">'. $category[ 'forum_name']. '</a> '; echo '-> <a href="forumdisplay.php?f='. $forum[ 'forum_id']. '">'. $forum[ 'forum_name']. '</a> '; echo '-> <a href="showthread.php?t='. $thread[ 'thread_id']. '">'. $thread[ 'thread_name']. '</a>'; echo '<center>'; foreach($posts as $post) { echo '<table width="900" border="1">'; echo '<tr>'; echo '<td colspan="3"><font size="2">Posted '. $post[ 'post_time']. '</font></td>'; echo '</tr>'; echo '<tr>'; echo '<td colspan="2" width="30%">'. $post[ 'post_author']. '</td>'; echo '<td>'. $post[ 'post_content']. '</td>'; echo '</tr></table><br />'; } echo '<a href="newpost.php?t='. $t. '">Post reply</a>'; echo '</center>'; }?>
|
|
|
|
12-23-2007, 11:33 PM
|
#19 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
Oh.
Code:
<form action="newpost.php" method="POST">
try changing that to:
Code:
<form action="newpost.php?t=<?=$_GET['t'];?>" method="POST">
(Using PHP short tags)
Last edited by dschreck : 12-23-2007 at 11:37 PM.
Reason: bleh - new answer
|
|
|
|
|
The Following User Says Thank You to dschreck For This Useful Post:
|
|
12-24-2007, 08:29 AM
|
#20 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by dschreck
Oh.
Code:
<form action="newpost.php" method="POST">
try changing that to:
Code:
<form action="newpost.php?t=<?=$_GET['t'];?>" method="POST">
(Using PHP short tags)
|
Oh lol!
I didn't think I needed that number, but thanks :D
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|