Output the database tables
This lesson is part of an ongoing Foruml tutorial. The first part is here: Build your own Forum, along with all the files you need. The previous lesson is here.
The next thing to do is to print out all the information we stored in our 2D array. We're printing it out in a table.
The technique is the same as the one in for the forumTest.php page - just print out your HTML table headers, and loop round for the table rows and table data tags. Here's just one line from the loop:
print $tdStart . $postData[$i]['member'] . $tdEnd;
We print out the TD start tag, and the TD end tag. In between that, we have this:
$postData[$i]['member']
To print out a value from the 2D array, just refer to its position in the array, and the key you want to print. The position is coming from the loop ($i). The key is member.
Examine the rest of the for loop that prints out the table. See if you can understand what's going on. Especially this rather long line (split into three lines on these pages):
print $tdStart . $hrefStart . "=" . $postData[$i]['threadID'] . "&forum=" . $forum . "&pageID=0" . ">" . $postData[$i]['threadTopic'] . $hrefEnd . $tdEnd;
An important part of the line above is this:
"&pageID=0"
As you may have realised, the whole line prints out the hyperlink. If you hold your mouse over a hyperlink on the pageThread.php page, you'll see something like this:
pageReply.php?rID=1&forum=secWP&pageID=0
This means that we are trying to pass three things to a page called pageReply.php. We're trying to pass the following three variables:
rID
forum
pageID
The values in the variables are 1, secWP, and 0. You'll see how they work in the next section. But the pageID is used to display the links that will take a user to say page 1 of the replies, or page 2 of the replies (if there is a page 2), page 3, page 4, etc. We're passing a value of zero because this is the first page of the replies.
The final part of the pageThread.php code is this:
if ($nonMember == '') {
print "<P align = center>" . $replyHTML . "</P>";
}
else {
print "<P align = center>" . $nonMember
. "</P>";
print "<P align = center>" . "<A HREF = login.php>Login
Here</A>" . "</P>";
}
This just tests what is inside of the variable we set up at the top of the page. If $nonMember is blank, then we can print out the hyperlink to allow the member to post a new topic. If it's not blank, then we can display some HTML asking the user to login in. You can also add a link to register, if you like. We did this in a previous section, so we won't cover it here.
But that's it! That's the code to display all the posts in your forum.
There is however, a problem. Supppose your forum is really large. If so, you'd need to spread the posts over more than one page. That way a user can click onto page 2 of the posts, page 3, etc. You'll see how to do this in the next section, which explains how to write the code for displaying the replies to a post - we'll definitely be needing that pageID!