Welcome, Guest!  |   or   |  Furl

Thursday, January 10th, 2013

Better Now: Comment Display


After recovering from the euphoria of getting my first real comment yesterday, I realized that 500benz.com was doing something that has always annoyed me on the interwebs—displaying comments as one big block of text, as opposed to letting the user create a new paragraph block in the comment by pressing enter.

So I went ahead and FTFY.

Here's what the comments look like now:

Ah. Much better.

In case you're curious, and also for posterity, and also as potential comic relief for any experienced programmer who happens to be reading this, I'll try to explain what was going on.

Okay, so the way a "dynamic" website with a database works is by pulling database content as requested and putting it into the right places on each web page. For example, the page you're reading right now is called "details.php," but if you looked at the code for that page, you wouldn't find any of this text. That's because all of the article-specific content—the title, the article itself, the date it was created, etc.—actually lives far away in a MySQL database table. By clicking a link to this article, you triggered a database query that caused the relevant content to be inserted into the page.

Here, take a look at the code for the text of this article in "details.php":

That's it. The "$row['article']" part refers to a database query earlier in the code that—based on the article you asked for—had already pulled all the article's content from the database. All I had to do was tell the browser where to put it. Same goes for the comments, which are told via "$row['comment']" where to go.

Ah, but there's one more thing the browser needs to know: what the text is supposed to look like. Otherwise, it's going to come out as one big unformatted chunk. That's what the "convertToParas" function does in the snippet above—it takes the text from the database and converts it into paragraph blocks (which also get separate font/style attributes via another technique that I won't bore you with here).

And that's what was missing from the code for displaying the comments: a similar function that would start a new paragraph wherever the user had pressed enter. (Interestingly, the database records these enter-presses—I can see them in the comments table—but the browser needs a separate command to implement them, because it starts from scratch on the formatting front.)

So I looked at the convertToParas function and realized that it would work just fine for the comments, too, with minimal hacking. Here's the amended code for displaying the comments:

That did the trick.

And now you know what I think was going on.

posted in: How This Site Works  

0 comments

or to comment.

No comments yet. Come on, say it, baby, just say it!

Benzito Gateway

Welcome, new Benzito!

No. Spam. Ever.