views:

38

answers:

1

I'm trying to develop web application (php/mysql), sort of blog site where ~10 posts are listed on front page. Only first 4-5 lines of each post are displayed and when user clicks the title it opens new page with full post displayed.

Users will use TinyMCE to post with very limited functionality, so bold, italics, underline, undo/redo and bullet lists only. No HTML view, no links nor images so nothing fancy.

Now, as two displays of the same posts are involved, so the one on front page where part is displayed only, and full version, I'm not sure if I should:

  • when user submits post I should clear it up with something like HTMLPurifier and store one full version in database. Then cut first 4-5 lines of it, and clear it with HTMLPurifier again to make sure all tags are closed properly. This is to make sure the text I cut doesn't finish with html tag cut in half but rather is valid (x)html properly closed. I would store this shorten front-page version somewhere in database again. So I would have two versions in database ready, full post version and shorten front-page version. Then when someone accesses the site and displays front page with 10 posts, these just need to be read from database and displayed. No need for reading full version, cutting the top of it, making sure all tags are properly closed etc. etc. for all 10 posts every time someone opens the site. The obvious problem would seem that part of content is doubled in database so full version and short version.

  • Another option I thought about would be when user submits post, clear it up with HTMLPurifier, and store only full version to database. Then when someone accesses the site, cut top part of each blog post, clear it up with HTML purifier (again!) to make sure its all valid, tags are closed etc. and display on front page. Now the problem seems that HTMLPurifier would have to be run and clean every single time when someone accesses the short version of the post, so e.g. 10x for frontage for each visit and so on.

I used HTMLPurifier above as an example only as I'm sure there are other tools douing same thing so please advice if anything else would be more appropriate in my situation. Also I will probably allow users to display more than 10 posts per page.

Ok, I'm sure there is some common pattern to deal with this sort of site and if so please give me an idea of how it should be done as my thought above seems to have many obvious problems. As always your help is much appreciated.

EDIT: I should have clarified.. Its more like an exercise to me and I wanted to learn how this sort of site should be done. Same concept can be implemented on other sites so advertising, forums etc. anything with posting really. For blogging I sure could use something ready but this time I wanted to learn.

A: 

When a user posts a blog entry, all you need to do is insert the entire post into the database. Before inserting, you need to escape/clean-up the post so that nothing harmful is inserted.

When viewing the post you will have two type of views - the excerpt and the full. The excerpt will only be the first 4-5 lines of the post. To get this you need to TRUNCATE the post before outputting it. To do that, you need to create a truncate function that will limit the number of words/characters.

You do not need to insert the post twice ( excert and full ) into the database.

For instance:

// Excerpt Post 
echo $postTitle;
echo truncate($postBody); // where truncate is the function you used to trim

// Full Post
echo $postTitle;
echo $postBody;

Hope this gets you started!

mtokoly
As HTML tags are involved after truncating I was thinking on using something like HTMLPurifier after excerpt is extracted from full post. All to avoid unclosed tags or even worse something like "<l" instead of "<li>" at the end of excerpt. Is there any other, better way of douing this? Although only few tags are involved in this case I would rather like to use something that's ready and been properly tested.
spirytus
It would be best to strip all of the tags in the excerpt before outputting it b/c it could cause all sorts of problems <- thats what Wordpress does. Something like PHP's strip_tags() function should help, and include that in your truncate function.
mtokoly
hmm.. I initially though it might be an idea, but then what if someone puts list somewhere close to the top of the post? How shall I deal with that?
spirytus
that's honestly up to you. the truncate function (name it whatever you want) could do as mush as you want. for instance it could get the first 50 words and if it was in the middle of tags, it wouldn't trim until it reached the end of the tag. <- which would be a pretty advanced function.
mtokoly
would you happen to know some library that does just that? HTMLPurifier seems like able to do it but wouldn't be overhead and horribly slowing down the application if run 10x every time someone opens the page?
spirytus