Forum Post Replies
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.
We've just used a 2D array to store the row information from our database table. Each postion in the 2D array will hold the threadID, the memberID, the threadTopic, the postText and the datePosted. The next thing to do is find out how many replies there are for each post. The code that does that is as follows:
$cnt = count($postData);
for ($i = 0; $i < $cnt; ++$i) {
$rep = $postData[$i]['threadID'];
$repSQL = getReplySQL($forum, $rep);
$result = mysql_query($repSQL);
$numRows = mysql_num_rows($result);
$postData[$i]['numRows'] = $numRows;
}
First, we get a count of the number of positions in the 2D array. This is so that we can loop through each postion. The first line in the for loop is this:
$rep = $postData[$i]['threadID'];
This returns the threadID from each position. We're using the variable $i to access each postion in the array.
Next, we make a call the other function at the top of the code:
$repSQL = getReplySQL($forum, $rep);
The second function has two arguments, a section code ID (now stored in the ($forum variable), and the threadID from the array. The function is again a series of if statement. Depending on which forum section ID is being passed over, a SQL statement is returned. Look at the SQL, though:
"SELECT * from wpreplies WHERE wpreplies.threadID = '$reply'";
We're saying, Select all the records from the wpreplies table where there is a match on the threadID". The value in $reply is coming from our 2D array, and will be something like pos1, pos2, po3, etc. This is from the Posts table. Because of the way we set up our database, we have a threadID field in both the replies and post tables. This is what allows us to link both tables in the above SQL.
After the SQL executes, it will return the number of rows where the two threadID's are the same. This number is then stored into the variable called $numRows. The final line of the for loop is this:
$postData[$i]['numRows'] = $numRows;
Here's we're adding a new key and a new value to our 2D array. The key is between the second set of square brackets, and is called numRows. The value for this key is whatever is inside of the variable called $numRows. This allows us to store a record of how many replies there are in each posts.
In the next part, you'll see how to find out which member posted.