Loops within Loops: Coding an ASP-based Bulletin Board, Part 2By Zola
You will notice that there is a little extra code here, and that I explicitly closed the sql when the loop was done. This is because, although I am only showing one nested loop for this article, you will see in the full code that there are actually 12 loops nested in that original loop. What I found was that if one post had 3 responses and another only two, the second post would show the third response from the first post because there was an appropriately named variable still floating around out there. Once the nested loops have run, they need to be closed and set to nothing so when the set runs again we are starting fresh. If we traced the steps, they would go something like this:
1.) Display the first main post
2.) Display the first response to the first main post. If there are none, go to Step 5.
3.) Display the second response to the first main post. If there are none, go to Step 5
4.) Display the third response to the first main post (and so on-for this example we will end it here)
5.) Display the second main post
6.) Display the first response to the second main post. If there are none, go to Step 9.
7.) Display the second response to the second main post. If there are none, go to Step 9
8.) Display the third response to the second main post (and so on)
9.) Display the third main post.....
This neatly brings us to using that
eof command right in the SQL . This is because there is no
need to have the server run any of the other loops if there are no responses to the post being looked at. By
if eof = false, my code will bypass the rest of the loops unless there is
something there. Because I have added an
if statement, I have to tell it what to do
eof was indeed True. In this case, end the
if and close the loop, at which point
it jumps to the previous loop. This also helps keep the code running quickly and eases the burden on the
Once it runs through all the nested loops, it goes back to the original loop of main posts and the whole cycle begins anew.
Why did I choose 12 as the number of steps possible? Because we are a talky bunch. :) Actually the
real reason it because that is the point at where the title is about 3/4 of the way across the page, and most
people will start a new thread. In the rare case that someone has more to say, it is a simple matter to go
into the database and change the
ResponseTo field to 0 so that the discussion could keep going.
The thing I like most of all is the adaptability of the script. Right now, my most active bulletin board displays the last 60 days worth of posts. If we become more active and the page starts getting long, it is the work of two seconds to limit the display further. For a big site like ASP Messageboard, which receives in the neighborhood of 250 - 350 posts a day, I would probably limit the display to one or two days! I would also change the archive page so that it displayed according to a date requested rather than having several fixed archive pages. It depends on your needs-I like to keep things simple.
And voila! My looks-just-like-a-cgi-script bulletin board is up and running! To see a couple examples of this code in action, visit: http://www.zolaweb.com/heart/heart.asp
I look forward to feedback!
Zola - firstname.lastname@example.org
Disclaimer: As with any code, there may be bugs. If you would like to report a bug, please send full details and a correction if at all possible so that we can pass the correction on to everyone.
If you would prefer to use another type of database, you would create a table with the following fields:
|PostID - the Primary key , an autonumber field|
Don't forget to change the date information and connection properties in accordance with your database.