I know that with a message board script that each user has a unique id, and threads have ids, so that's how you keep track of what post belongs to who.
When displaying the threads in a forum, how exactly do you display the correct username for each thread? I may be completely wrong about this, but I always figured that in order to display the threads, you pulled them out of the database and then used a PHP foreach loop. But aren't people's usernames going to be represented by a variable such as $username? How do you get $username to show the correct username?
This isn't so much a PHP question as a databasing concept question.
In a normalised database, usernames are stored in one table, threads in another, forum names in another, etc etc - everything has its own table. They are linked together by identifiers called foreign keys. So the threads table might be structed like this in terms of its columns:
What goes into the started_by column above is the primary key ID from the username relating to the guy who started the thread. If you don't know databasing this will be double-dutch to you.
I pretty much know all of that. What I'm saying is user_name_1 and user_name_2 on the page will both probably be represented by a PHP variable such as $username. How will it display user_name_1 when it's supposed to, and how will it display user_name_2 when it's supposed to? There's going to be a lot more than one user on a message board.
OK if I misunderstood the first time I didn't mean to be patronising. If I get you right, you're basically asking how PHP takes the variable, i.e. the ID of the post in question, and turns it into a readable username, right?
In basic terms, it would be something like this.
//first get this thread's posts
$thisThreadPosts = mysql_query("select post_text, posted_when, username from posts, users where users.user_id = posts.user_id && thread_id=[id thread being viewed]");
//post_text and posted_when are columns of the posts table, username is a column of the users table
//output posts
echo "<table>";
for ($f=0; $f<mysql_num_rows($thisThreadPosts); $f++) {
echo "<tr><td>Posted by ".mysql_result($thisThreadsPost, $f, 'username')." at ".mysql_result($thisThreadsPost, $f, 'posted_when')."</td></tr>\n<tr><td>".mysql_result($thisThreadPosts, $f, 'post_text')."</td</tr>";
}
echo "</table>";
It's the mysql query which turns the user_id stored in the posts table into a readable username.
What I'm asking is how will it display the RIGHT username. I can only imagine how many registered users this board has. For example, in this forum, how does it know to display my username next to my thread instead of yours?
It's all about the structure of the various tables that run this forum. Among others, the following three tables for sure exist:
threads
posts
users
Among others, the threads table contains the following columns:
thread_title / started_when / started_by
Started_by is a foreign key to the users table. So in here goes the ID of the user that started the thread. So in your thread's case, the entry in the threads table, is...
thread_title = "displaying usernames from a message board" / started_when = "1980-05-29 12:00:00" (example) / 403
So let's assume your user ID in this database is 403 (users don't know their ID for real, there's no need to). When it processes your thread, it sees in the thread table that it was started by user id 403. it goes off to the users table, looks for the record whose primary key is 403, and hey presto, in the adjacent column named "username", it's you. So the users table looks something like this
Bookmarks