views:

23

answers:

1

Hi There,

Just wondering if anyone has got any ideas regarding sneak-peaking the content of a mysql table?

Ie. say there are 10 posts with 500 words each. I want to list all the posts with a content sneak peak... i.e display a couple of lines or 200 characters etc. for each post

The thing i'm wondering is how to do that while keeping server workload as low as possible. I could load the entire content and then remove what I dont want with the use of substr, but I have a feeling thats not the best way.

Thanks in advance

+4  A: 

You could use the MySQL function LEFT to get the first 200 characters from each post from the database:

SELECT LEFT(content, 200) FROM post

If you want a specific part of the content and not just the first 200 characters you could store that separately in the database, for example a column called sneak_peak. You can calculate the content of this column once using whatever method you like and persist the result. Fetching it from the database will then be very fast as no string manipulations will need to be done.

Mark Byers
@OMG Ponies: I think he meant that he didn't want to load the entire content into PHP and then use the PHP function `substr`. At least that's how I understood the question.
Mark Byers
@Mark Byers: thanks, thats exactly what I was after
Stevanicus