Building a Forum with Dreamweaver – Part 3: Associating Messages to a Thread
Associating Messages to a Thread
At this point you can add messages in any topic you want by logging in and clicking the Post message link. However, records added through the insert form are not complete—they are missing the
idinit_msg fields. (Remember when you removed them from the wizard in the previous section?)
For new messages (which are not replies to other messages), there is no parent message, so the
idmsg_msg column should be
idinit_msg field stores the ID of the first message in a thread and will be used to notify subscribers when a reply is posted. All messages from the same thread have the same value of the
The first message in any thread must have its
idinit_msg field value equal to its ID (its primary key). However, the primary key of each message is only known when the message is inserted in the database; it cannot be known beforehand. In order to set the value of the
idinit_msg field after the message has been posted, you must execute some custom code that retrieves the ID of the message.
ImpAKT enables you to add custom actions and behaviors to any transaction using custom triggers, which are a simple way to extend the default server behaviors that come with ImpAKT. However, custom triggers may require good programming skills and a good understanding of how ImpAKT handles server-side code. That is why we will assist you in creating your first block of code for ImpAKT:
With the post_message.php page open, select Server Behaviors tab > Plus (+) > MX Kollection > Forms > Custom Trigger. In the Custom Trigger dialog box, you have two tabs to configure:
- Basic tab: Where you will write the code
- Advanced tab: Where you will associate the custom action to the insert transaction and assign it a priority
In the Basic tab, enter the following code:
$pkval = $tNG->getColumnValue(‘id_msg’);
$query_update_initmsgid = “UPDATE message_msg
SET id_init_msg = “.$pkval.”
WHERE id_msg = “.$pkval;$result_update = $tNG->connection->execute($query_update_initmsgid);
The code performs an update on the last inserted message and sets the value of the
idinit_msg field equal to the message’s primary key. The Basic tab should look like Figure 16.
In the Advanced tab, you have to fill in the following options:
- Trigger Name: Leave it to its default value
- Trigger Type: This must be an
AFTERtrigger because it will be executed after the insert transaction; this way, the value of the primary key will be available
- Trigger Priority: If you haven’t added other triggers, you can leave it at its default value (50)
The configured Advanced tab should look like Figure 17.
Congratulations! You can now use the behavior to post messages to the forum. All the necessary columns will be filled in correctly by the transaction.
You can save and upload the page to the server. To view it, however, you will need to go to the login page and sign in. Otherwise, you will see a warning message similar to one shown in Figure 18.
After you log in, browse to the topic of interest and click the Post Message link. Figure 19 shows what a new message looks like.
You have now enabled users to post messages to the forum. In Part 4 of this tutorial, you will learn how to build a form for replying to messages and send automatic e-mail notifications when a reply is posted.