case "comments":
showblogs = false;
try
{
blogID = Convert.ToInt16(getVariable("blogID", var.GET));
}
catch { }
content += comments(blogID);
break;
showblogs = false;
try
{
blogID = Convert.ToInt16(getVariable("blogID", var.GET));
}
catch { }
content += comments(blogID);
break;
This should look quite familiar by now. The plan is to show the blog and then all the comments below it. I could just put all that in the comments() method, copying the appropriate portion from the showBlogs() method. However, one of the biggest rules to remember is never to code the same thing twice. Always reuse existing code, otherwise if you have to change something, you have to change it in multiple places. In reality, if you have the same code in two places and you have to go back to make a change in 6 months, you'll forget about the second place and may run into some major problems as a result. So instead of duplicating code, I made some changes to the showBlogs() method to accomodate displaying a specific blog.
private string showBlogs(int blogID)
{
string content = null;
string select = "SELECT top 30 blogID, blogTitle, blogText, displayName, displayDate, " +
"CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
"END AS 'email', " +
"(SELECT COUNT(*) FROM comments WHERE blogID=a.blogID and visible=1) as Comments, " +
"b.userID " +
"FROM blogs a " +
"JOIN users b on " +
"a.userID=b.userID " +
"WHERE visible=1 ";
select += (blogID == 0)? "ORDER BY displayDate Desc" : " AND blogID=" + blogID;
{
string content = null;
string select = "SELECT top 30 blogID, blogTitle, blogText, displayName, displayDate, " +
"CASE " +
"WHEN showEmail = 1 THEN ' (<a href="mailto: ' + email + '">' + email + '</a>)' " +
"ELSE '' " +
"END AS 'email', " +
"(SELECT COUNT(*) FROM comments WHERE blogID=a.blogID and visible=1) as Comments, " +
"b.userID " +
"FROM blogs a " +
"JOIN users b on " +
"a.userID=b.userID " +
"WHERE visible=1 ";
select += (blogID == 0)? "ORDER BY displayDate Desc" : " AND blogID=" + blogID;
This is the first few lines of the showBlogs() method. I've added the ability to pass an integer called blogID into the method. Then when I'm building the select statement, I check that variable. If it's 0, I just do what I usually was doing. If it's greater than 0, I pass in the blogID in the WHERE clause. I don't bother to ORDER the result if I'm passing in a blogID... I'll only ever get 1 result that way.