Results 1 to 7 of 7

Thread: Displaying Usernames from a Message Board

  1. #1
    Join Date
    Dec 2007

    Displaying Usernames from a Message Board

    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?

  2. #2
    Join Date
    Oct 2003
    Andover, UK
    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:

    thread_id (primary key) / thread_name / date_started / started_by (username) etc

    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.

  3. #3
    Join Date
    Dec 2007
    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.

  4. #4
    Join Date
    Oct 2003
    Andover, UK
    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.

    Am I getting warm to what you're after? lol

  5. #5
    Join Date
    Dec 2007
    Closer lol

    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?

  6. #6
    Join Date
    Oct 2003
    Andover, UK
    It's all about the structure of the various tables that run this forum. Among others, the following three tables for sure exist:


    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

    user_id (primary key) / username / first_name / last_name / lives / etc

    I'm not sure how well I'm doing of explaining this but if you look up primary and secondary keys you'll get it.

  7. #7
    Join Date
    Dec 2007
    I think I get it now Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12284 seconds
  • Memory Usage 2,896KB
  • Queries Executed 13 (?)
More Information
Template Usage (32):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates