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 idmsg_msg
and 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 NULL
. The 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 idinit_msg
field.
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.Figure 16. Configuring the Basic tab of the Custom Trigger dialog box
-
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
AFTER
trigger 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)
- When you’re done configuring the Custom Trigger, click the OK button to close the dialog box and return to Dreamweaver. You can edit the code at any time by double-clicking the Custom Trigger server behavior in the Application panel.
The configured Advanced tab should look like Figure 17.
Figure 17. Configuring the Advanced tab of the Custom Trigger dialog box
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.
Figure 18. You cannot access the post_message.php page, if you’re not logged in
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.
Figure 19. Posting new messages
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.
Comments